-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGlobalInput.cs
More file actions
380 lines (331 loc) · 7.75 KB
/
Copy pathGlobalInput.cs
File metadata and controls
380 lines (331 loc) · 7.75 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
Title: GlobalInput
Description: Uses low level C# to hook into hook events and replicate unity's input manager but even when the application doesn't have focus.
This script should work the same as unity's Input class
eg: Input.GetKeyDown( KeyCode.Left ) becomes GlobalInput.GetKeyDown( GlobalKeyCode.LEFT )
Auther: Kalvin Pearce
Github: https://github.com/kalvinpearce/
*/
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public class GlobalInput : MonoBehaviour
{
#region SINGLETON
static GlobalInput _instance;
public static GlobalInput Instance
{
get
{
if( _instance == null )
{
_instance = GameObject.FindObjectOfType<GlobalInput>();
if( _instance == null )
{
GameObject container = new GameObject("GlobalInputManager");
_instance = container.AddComponent<GlobalInput>();
}
}
return _instance;
}
}
#endregion
/* Variables */
// Hook types
private enum HookType : int
{
WH_JOURNALRECORD = 0,
WH_JOURNALPLAYBACK = 1,
WH_KEYBOARD = 2,
WH_GETMESSAGE = 3,
WH_CALLWNDPROC = 4,
WH_CBT = 5,
WH_SYSMSGFILTER = 6,
WH_MOUSE = 7,
WH_HARDWARE = 8,
WH_DEBUG = 9,
WH_SHELL = 10,
WH_FOREGROUNDIDLE = 11,
WH_CALLWNDPROCRET = 12,
WH_KEYBOARD_LL = 13,
WH_MOUSE_LL = 14
}
public static bool[] keyStates = new bool[255];
public static bool[] keyDownStates = new bool[255];
public static bool[] keyUpStates = new bool[255];
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private static IntPtr windowHandle;
private static LowLevelKeyboardProc llKbProc;
private static IntPtr hookID = IntPtr.Zero;
// Set the hook
public static bool SetHook()
{
// If no process linked, link hook process
if( llKbProc == null )
llKbProc = HookCallback;
// If already hooked, unhook
if( hookID != IntPtr.Zero )
UnsetHook();
// Hook new process
Process curProcess = Process.GetCurrentProcess();
ProcessModule curModule = curProcess.MainModule;
windowHandle = GetModuleHandle(curModule.ModuleName);
hookID = SetWindowsHookEx( (int)HookType.WH_KEYBOARD_LL, llKbProc, windowHandle, 0);
// If hooking failed, return false
if( hookID == IntPtr.Zero )
{
UnityEngine.Debug.Log( "Failed to hook" );
return false;
}
// Hooked successfully
UnityEngine.Debug.Log( "Hooked successfully" );
return true;
}
// Unhook process
public static void UnsetHook()
{
UnhookWindowsHookEx( hookID );
hookID = IntPtr.Zero;
}
// Hook function for windows to call on keystroke events
private static IntPtr HookCallback( int nCode, IntPtr wParam, IntPtr lParam )
{
if ( nCode >= 0 )
{
// Read vkCode from lParam
int vkCode = Marshal.ReadInt32(lParam);
// If vkCode key is down, set the key state to true
if( wParam == (IntPtr)WM_KEYDOWN )
SetKeyState( vkCode, true );
// If vkCode key is up, set the key state to false
if( wParam == (IntPtr)WM_KEYUP )
SetKeyState( vkCode, false );
}
// Call next hook
return CallNextHookEx(hookID, nCode, wParam, lParam);
}
static void SetKeyState( int key, bool state )
{
if( !keyStates[ key] )
keyDownStates[ key ] = state;
keyStates[ key ] = state;
keyUpStates[ key ] = !state;
}
public static bool GetKey( GlobalKeyCode key )
{
return keyStates[ (int)key ];
}
public static bool GetKeyDown( GlobalKeyCode key )
{
if( keyDownStates[ (int)key ] )
{
keyDownStates[ (int)key ] = false;
return true;
}
return false;
}
public static bool GetKeyUp( GlobalKeyCode key )
{
if( keyUpStates[ (int)key ] )
{
keyUpStates[ (int)key ] = false;
return true;
}
return false;
}
static GlobalInput()
{
// Hook process
SetHook();
GameObject container = new GameObject("GlobalInputManager");
_instance = container.AddComponent<GlobalInput>();
DontDestroyOnLoad( _instance );
}
void OnDisable()
{
UnityEngine.Debug.Log("Application ending after " + Time.time + " seconds");
UnityEngine.Debug.Log("Uninstall hook");
UnsetHook();
}
/* Extern Functions */
[DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = true )]
private static extern IntPtr SetWindowsHookEx( int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId );
[DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = true )]
[return: MarshalAs( UnmanagedType.Bool )]
private static extern bool UnhookWindowsHookEx( IntPtr hhk );
[DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = true )]
private static extern IntPtr CallNextHookEx( IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam );
[DllImport( "kernel32.dll", CharSet = CharSet.Auto, SetLastError = true )]
private static extern IntPtr GetModuleHandle( string lpModuleName );
[DllImport( "user32.dll" )]
private static extern IntPtr GetForegroundWindow();
// LowLevelKeyboardProc template
private delegate IntPtr LowLevelKeyboardProc( int nCode, IntPtr wParam, IntPtr lParam );
}
public enum GlobalKeyCode : int
{
LBUTTON = 0x01,
RBUTTON = 0x02,
CANCEL = 0x03,
MBUTTON = 0x04,
BACK = 0x08,
TAB = 0x09,
CLEAR = 0x0C,
RETURN = 0x0D,
SHIFT = 0x10,
CONTROL = 0x11,
MENU = 0x12,
PAUSE = 0x13,
CAPITAL = 0x14,
ESCAPE = 0x1B,
SPACE = 0x20,
PRIOR = 0x21,
NEXT = 0x22,
END = 0x23,
HOME = 0x24,
LEFT = 0x25,
UP = 0x26,
RIGHT = 0x27,
DOWN = 0x28,
SELECT = 0x29,
PRINT = 0x2A,
EXECUTE = 0x2B,
SNAPSHOT = 0x2C,
INSERT = 0x2D,
DELETE = 0x2E,
HELP = 0x2F,
// Number keys
N0 = 0x30,
N1 = 0x31,
N2 = 0x32,
N3 = 0x33,
N4 = 0x34,
N5 = 0x35,
N6 = 0x36,
N7 = 0x37,
N8 = 0x38,
N9 = 0x39,
// Letter keys
A = 0x41,
B = 0x42,
C = 0x43,
D = 0x44,
E = 0x45,
F = 0x46,
G = 0x47,
H = 0x48,
I = 0x49,
J = 0x4A,
K = 0x4B,
L = 0x4C,
M = 0x4D,
N = 0x4E,
O = 0x4F,
P = 0x50,
Q = 0x51,
R = 0x52,
S = 0x53,
T = 0x54,
U = 0x55,
V = 0x56,
W = 0x57,
X = 0x58,
Y = 0x59,
Z = 0x5A,
LWIN = 0x5B,
RWIN = 0x5C,
APPS = 0x5D,
SLEEP = 0x5F,
// Numpad keys
NUMPAD0 = 0x60,
NUMPAD1 = 0x61,
NUMPAD2 = 0x62,
NUMPAD3 = 0x63,
NUMPAD4 = 0x64,
NUMPAD5 = 0x65,
NUMPAD6 = 0x66,
NUMPAD7 = 0x67,
NUMPAD8 = 0x68,
NUMPAD9 = 0x69,
MULTIPLY = 0x6A,
ADD = 0x6B,
SEPARATOR = 0x6C,
SUBTRACT = 0x6D,
DECIMAL = 0x6E,
DIVIDE = 0x6F,
// Function keys
F1 = 0x70,
F2 = 0x71,
F3 = 0x72,
F4 = 0x73,
F5 = 0x74,
F6 = 0x75,
F7 = 0x76,
F8 = 0x77,
F9 = 0x78,
F10 = 0x79,
F11 = 0x7A,
F12 = 0x7B,
F13 = 0x7C,
F14 = 0x7D,
F15 = 0x7E,
F16 = 0x7F,
F17 = 0x80,
F18 = 0x81,
F19 = 0x82,
F20 = 0x83,
F21 = 0x84,
F22 = 0x85,
F23 = 0x86,
F24 = 0x87,
// Lock keys
NUMLOCK = 0x90,
SCROLL = 0x91,
// Modifiers
LSHIFT = 0xA0,
RSHIFT = 0xA1,
LCONTROL = 0xA2,
RCONTROL = 0xA3,
LMENU = 0xA4,
RMENU = 0xA5,
// Media keys
BROWSER_BACK = 0xA6,
BROWSER_FORWARD = 0xA7,
BROWSER_REFRESH = 0xA8,
BROWSER_STOP = 0xA9,
BROWSER_SEARCH = 0xAA,
BROWSER_FAVORITES = 0xAB,
BROWSER_HOME = 0xAC,
VOLUME_MUTE = 0xAD,
VOLUME_DOWN = 0xAE,
VOLUME_UP = 0xAF,
MEDIA_NEXT_TRACK = 0xB0,
MEDIA_PREV_TRACK = 0xB1,
MEDIA_STOP = 0xB2,
MEDIA_PLAY_PAUSE = 0xB3,
LAUNCH_MAIL = 0xB4,
LAUNCH_MEDIA_SELECT = 0xB5,
LAUNCH_APP1 = 0xB6,
LAUNCH_APP2 = 0xB7,
// Symbols
COLON = 0xBA,
PLUS = 0xBB,
COMMA = 0xBC,
HYPHEN = 0xBD,
PERIOD = 0xBE,
FSLASH = 0xBF,
TILDE = 0xC0,
BACKQUOTE = 0xC0, // duplicate of tilde
LSQUAREBRACKET = 0xDB,
BSLASH = 0xDC,
PIPE = 0xDC, // duplicate of back slash
RSQUAREBRACKET = 0xDD,
QUOTE = 0xDE,
// ???
PLAY = 0xFA,
ZOOM = 0xFB,
}