This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Input.cs
102 lines (89 loc) · 3.38 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using static Engine.Kernel32;
namespace Engine;
public partial class Input {
public static readonly IntPtr stdInputHandle = GetStdHandle(-10);
public static readonly IntPtr stdOutputHandle = GetStdHandle(-11);
public static readonly IntPtr stdErrorHandle = GetStdHandle(-12);
/// <summary>Checks to see if the console is in focus </summary>
/// <returns>True if Console is in focus</returns>
private static bool ConsoleFocused()
{
return GetConsoleWindow() == GetForegroundWindow();
}
/// <summary> Checks if specified key is pressed. </summary>
/// <param name="key">The key to check.</param>
/// <returns>True if key is pressed</returns>
public static bool GetKey(ConsoleKey key) {
short s = GetAsyncKeyState((int)key);
return (s & 0x8000) > 0 && ConsoleFocused();
}
/// <summary> Checks if specified keyCode is pressed. </summary>
/// <param name="virtualkeyCode">keycode to check</param>
/// <returns>True if key is pressed</returns>
public static bool GetKey(int virtualkeyCode)
{
short s = GetAsyncKeyState(virtualkeyCode);
return (s & 0x8000) > 0 && ConsoleFocused();
}
/// <summary> Checks if specified key is pressed down. </summary>
/// <param name="key">The key to check.</param>
/// <returns>True if key is down</returns>
public static bool GetKeyDown(ConsoleKey key) {
int s = Convert.ToInt32(GetAsyncKeyState((int)key));
return (s == -32767) && ConsoleFocused();
}
/// <summary> Checks if specified keyCode is pressed down. </summary>
/// <param name="virtualkeyCode">keycode to check</param>
/// <returns>True if key is down</returns>
public static bool GetKeyDown(int virtualkeyCode)
{
int s = Convert.ToInt32(GetAsyncKeyState(virtualkeyCode));
return (s == -32767) && ConsoleFocused();
}
/// <summary> Checks if left mouse button is pressed down. </summary>
/// <returns>True if left mouse button is down</returns>
public static bool GetMouseLeft() {
short s = GetAsyncKeyState(0x01);
return (s & 0x8000) > 0 && ConsoleFocused();
}
/// <summary> Checks if right mouse button is pressed down. </summary>
/// <returns>True if right mouse button is down</returns>
public static bool GetMouseRight()
{
short s = GetAsyncKeyState(0x02);
return (s & 0x8000) > 0 && ConsoleFocused();
}
/// <summary> Checks if middle mouse button is pressed down. </summary>
/// <returns>True if middle mouse button is down</returns>
public static bool GetMouseMiddle()
{
short s = GetAsyncKeyState(0x04);
return (s & 0x8000) > 0 && ConsoleFocused();
}
// UNCOMPLINTED!!!!!!!!!!!!!!!
/// <summary> Gets the mouse position. </summary>
/// <returns>The mouse's position in character-space.</returns>
/// <exception cref="Exception"/>
public static Point GetMousePos() {
Rect r = new Rect();
GetWindowRect(GetConsoleWindow(), ref r);
if (GetCursorPos(out Point p)) {
Point point = new Point();
if (!Screen.IsBorderless) {
p.Y -= 29*2;
point = new Point(
(int)Math.Floor(((p.X - r.Left) / (float)Screen.FontSize.X)),
(int)Math.Floor(((p.Y - r.Top) / (float)Screen.FontSize.Y))
);
} else {
point = new Point(
(int)Math.Floor(((p.X - r.Left) / (float)Screen.FontSize.X)),
(int)Math.Floor(((p.Y - r.Top) / (float)Screen.FontSize.Y))
);
}
return new Point(Utility.Clamp(point.X, 0, Screen.WindowSize.X - 1), Utility.Clamp(point.Y, 0, Screen.WindowSize.Y - 1));
}
throw new Exception();
}
}