-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.cs
72 lines (60 loc) · 2.08 KB
/
Input.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Collections.Generic;
using Map;
namespace Input
{
public static class KeyInput
{
private static List<Tuple<ConsoleKey, ConsoleModifiers, bool>> CurrentKeys = new List<Tuple<ConsoleKey, ConsoleModifiers, bool>> ();
public static void Read()
{
/*
Function to be run in main loop every cycle to read key presses.
*/
for (int x = 0; x < CurrentKeys.Count; x++)
CurrentKeys.RemoveAt(x);
while (Console.KeyAvailable)
{
ConsoleKeyInfo KeyPress = Console.ReadKey(true);
bool alreadyLogged = false;
foreach (var key in CurrentKeys)
{
if (key.Item1 == KeyPress.Key && key.Item2 == KeyPress.Modifiers)
alreadyLogged = true;
}
if (!alreadyLogged)
CurrentKeys.Add(Tuple.Create(KeyPress.Key, KeyPress.Modifiers, false));
}
}
public static bool IsPressed (ConsoleKey Key, ConsoleModifiers Mod = 0)
{
foreach (Tuple<ConsoleKey, ConsoleModifiers, bool> KeyPress in CurrentKeys)
if (KeyPress.Item1 == Key && KeyPress.Item2 == Mod)
return true;
return false;
}
}
public static class MouseInput
{
public static Coord GetPos()
{
POINT p;
GetCursorPos(out p);
return new Coord (p.X, p.Y);
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
public static implicit operator Point(POINT point)
{
return new Point(point.X, point.Y);
}
}
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);
}
}