|
| 1 | +using ConsoleGUI.Space; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace ConsoleGUI.MouseExample |
| 10 | +{ |
| 11 | + public static class MouseHandler |
| 12 | + { |
| 13 | + private static IntPtr _inputHandle = IntPtr.Zero; |
| 14 | + private static INPUT_RECORD[] _inputBuffer; |
| 15 | + |
| 16 | + public static Position MousePosition { get; private set; } |
| 17 | + |
| 18 | + public static void Initialize() |
| 19 | + { |
| 20 | + _inputHandle = GetStdHandle(unchecked((uint)-10)); |
| 21 | + _inputBuffer = new INPUT_RECORD[100]; |
| 22 | + } |
| 23 | + |
| 24 | + public static void Read() |
| 25 | + { |
| 26 | + if (_inputHandle == IntPtr.Zero) |
| 27 | + throw new InvalidOperationException("First call the Initialize method of the MouseHandler"); |
| 28 | + |
| 29 | + if (!ReadConsoleInput(_inputHandle, _inputBuffer, (uint)_inputBuffer.Length, out var eventsRead)) return; |
| 30 | + |
| 31 | + for (int i = 0; i < eventsRead; i++) |
| 32 | + { |
| 33 | + var inputEvent = _inputBuffer[i]; |
| 34 | + |
| 35 | + if (inputEvent.EventType == INPUT_RECORD.MOUSE_EVENT) |
| 36 | + ProcessMouseEvent(inputEvent.MouseEvent); |
| 37 | + else |
| 38 | + WriteConsoleInput(_inputHandle, new[] { inputEvent }, 1, out var eventsWritten); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private static void ProcessMouseEvent(in MOUSE_EVENT_RECORD mouseEvent) |
| 43 | + { |
| 44 | + MousePosition = new Position(mouseEvent.dwMousePosition.X, mouseEvent.dwMousePosition.Y); |
| 45 | + } |
| 46 | + |
| 47 | + private struct COORD |
| 48 | + { |
| 49 | + public short X; |
| 50 | + public short Y; |
| 51 | + } |
| 52 | + |
| 53 | + [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)] |
| 54 | + private struct KEY_EVENT_RECORD |
| 55 | + { |
| 56 | + [FieldOffset(0)] |
| 57 | + public bool bKeyDown; |
| 58 | + [FieldOffset(4)] |
| 59 | + public ushort wRepeatCount; |
| 60 | + [FieldOffset(6)] |
| 61 | + public ushort wVirtualKeyCode; |
| 62 | + [FieldOffset(8)] |
| 63 | + public ushort wVirtualScanCode; |
| 64 | + [FieldOffset(10)] |
| 65 | + public char UnicodeChar; |
| 66 | + [FieldOffset(10)] |
| 67 | + public byte AsciiChar; |
| 68 | + [FieldOffset(12)] |
| 69 | + public uint dwControlKeyState; |
| 70 | + } |
| 71 | + |
| 72 | + private struct MOUSE_EVENT_RECORD |
| 73 | + { |
| 74 | + public COORD dwMousePosition; |
| 75 | + |
| 76 | + public uint dwButtonState; |
| 77 | + public const uint |
| 78 | + FROM_LEFT_1ST_BUTTON_PRESSED = 0x0001, |
| 79 | + FROM_LEFT_2ND_BUTTON_PRESSED = 0x0004, |
| 80 | + FROM_LEFT_3RD_BUTTON_PRESSED = 0x0008, |
| 81 | + FROM_LEFT_4TH_BUTTON_PRESSED = 0x0010, |
| 82 | + RIGHTMOST_BUTTON_PRESSED = 0x0002; |
| 83 | + |
| 84 | + public uint dwControlKeyState; |
| 85 | + public const int |
| 86 | + CAPSLOCK_ON = 0x0080, |
| 87 | + ENHANCED_KEY = 0x0100, |
| 88 | + LEFT_ALT_PRESSED = 0x0002, |
| 89 | + LEFT_CTRL_PRESSED = 0x0008, |
| 90 | + NUMLOCK_ON = 0x0020, |
| 91 | + RIGHT_ALT_PRESSED = 0x0001, |
| 92 | + RIGHT_CTRL_PRESSED = 0x0004, |
| 93 | + SCROLLLOCK_ON = 0x0040, |
| 94 | + SHIFT_PRESSED = 0x0010; |
| 95 | + |
| 96 | + public uint dwEventFlags; |
| 97 | + public const int |
| 98 | + DOUBLE_CLICK = 0x0002, |
| 99 | + MOUSE_HWHEELED = 0x0008, |
| 100 | + MOUSE_MOVED = 0x0001, |
| 101 | + MOUSE_WHEELED = 0x0004; |
| 102 | + } |
| 103 | + |
| 104 | + [StructLayout(LayoutKind.Explicit)] |
| 105 | + private struct INPUT_RECORD |
| 106 | + { |
| 107 | + public const ushort MOUSE_EVENT = 0x0002; |
| 108 | + |
| 109 | + [FieldOffset(0)] |
| 110 | + public ushort EventType; |
| 111 | + [FieldOffset(4)] |
| 112 | + public KEY_EVENT_RECORD KeyEvent; |
| 113 | + [FieldOffset(4)] |
| 114 | + public MOUSE_EVENT_RECORD MouseEvent; |
| 115 | + } |
| 116 | + |
| 117 | + [DllImport("kernel32.dll")] |
| 118 | + public static extern IntPtr GetStdHandle(uint nStdHandle); |
| 119 | + |
| 120 | + [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] |
| 121 | + private static extern bool ReadConsoleInput(IntPtr hConsoleInput, [Out] INPUT_RECORD[] lpBuffer, uint nLength, out uint lpNumberOfEventsRead); |
| 122 | + |
| 123 | + [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] |
| 124 | + private static extern bool WriteConsoleInput(IntPtr hConsoleInput, INPUT_RECORD[] lpBuffer, uint nLength, out uint lpNumberOfEventsWritten); |
| 125 | + } |
| 126 | +} |
0 commit comments