-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathLowLevelHooks.cs
164 lines (139 loc) · 5.47 KB
/
LowLevelHooks.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* Copyright (C) 2021 Dylan Cheng (https://github.com/newlooper)
This file is part of VirtualSpace.
VirtualSpace is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
VirtualSpace is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with VirtualSpace. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace VirtualSpace.Helpers
{
public static class LowLevelHooks
{
public static readonly IntPtr Handled = (IntPtr)1;
}
public static class LowLevelKeyboardHook
{
public const int WM_KEYDOWN = 0x0100;
public const int WM_KEYUP = 0x0101;
public const int DUMMY_KEY = 0xFF;
private const int WH_KEYBOARD_LL = 13;
private static User32.HookProc _hookProc;
public static IntPtr HookId { get; private set; } = IntPtr.Zero;
public static void SetHook( User32.HookProc proc )
{
_hookProc = proc;
HookId = User32.SetWindowsHookEx( WH_KEYBOARD_LL, _hookProc, Kernel32.GetModuleHandle( null ), 0 );
}
public static void MultipleKeyDown( List<Keys> keys )
{
SendKeys( keys, 0 );
}
public static void MultipleKeyUp( List<Keys> keys )
{
SendKeys( keys, KEYEVENTF.KEYUP );
}
public static void MultipleKeyPress( List<Keys> keys )
{
SendKeysCombine( keys );
}
private static void SendKeys( List<Keys> keys, KEYEVENTF flags )
{
var inputs = new INPUT[keys.Count];
for ( var pos = 0; pos < keys.Count; pos++ )
{
inputs[pos].Type = InputType.INPUT_KEYBOARD;
inputs[pos].Data.Keyboard = new KEYBDINPUT
{
Vk = (ushort)keys[pos],
Scan = 0,
Flags = flags,
Time = 0,
ExtraInfo = IntPtr.Zero
};
}
var result = User32.SendInput( Convert.ToUInt32( inputs.Length ), inputs, Marshal.SizeOf( typeof( INPUT ) ) );
if ( result == 0 )
throw new Exception();
}
private static void SendKeysCombine( List<Keys> keys )
{
var inputs = new INPUT[keys.Count * 2];
for ( var i = 0; i < keys.Count; i++ )
{
inputs[i].Type = InputType.INPUT_KEYBOARD;
inputs[i].Data.Keyboard = new KEYBDINPUT
{
Vk = (ushort)keys[i],
Scan = 0,
Flags = 0,
Time = 0,
ExtraInfo = IntPtr.Zero
};
inputs[inputs.Length - i - 1].Type = InputType.INPUT_KEYBOARD;
inputs[inputs.Length - i - 1].Data.Keyboard = new KEYBDINPUT
{
Vk = (ushort)keys[i],
Scan = 0,
Flags = KEYEVENTF.KEYUP,
Time = 0,
ExtraInfo = IntPtr.Zero
};
}
var result = User32.SendInput( Convert.ToUInt32( inputs.Length ), inputs, Marshal.SizeOf( typeof( INPUT ) ) );
if ( result == 0 )
throw new Exception();
}
public static bool IsKeyHold( Keys key )
{
return User32.GetAsyncKeyState( (int)key ) < 0;
}
public static void UnHook()
{
User32.UnhookWindowsHookEx( HookId );
}
public struct KBDLLHOOKSTRUCT
{
public int vkCode;
private int scanCode;
public KBDLLHOOKSTRUCTFlags flags;
private int time;
private int dwExtraInfo;
}
[Flags]
public enum KBDLLHOOKSTRUCTFlags : uint
{
LLKHF_EXTENDED = 0x01,
LLKHF_INJECTED = 0x10,
LLKHF_ALTDOWN = 0x20,
LLKHF_UP = 0x80,
}
}
public static class LowLevelMouseHook
{
private const int WH_MOUSE_LL = 14;
public const int WM_MOUSEWHEEL = 0x020A;
private static User32.HookProc _hookProc;
public static IntPtr HookId { get; private set; } = IntPtr.Zero;
public static void SetHook( User32.HookProc proc )
{
_hookProc = proc;
HookId = User32.SetWindowsHookEx( WH_MOUSE_LL, _hookProc, Kernel32.GetModuleHandle( null ), 0 );
}
public static void UnHook()
{
User32.UnhookWindowsHookEx( HookId );
}
[StructLayout( LayoutKind.Sequential )]
public struct MSLLHOOKSTRUCT
{
public POINT pt;
public int mouseData; // be careful, this must be ints, not uints.
public int flags;
public int time;
public UIntPtr dwExtraInfo;
}
}
}