-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcpyHook.i
334 lines (275 loc) · 9.07 KB
/
cpyHook.i
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
%module cpyHook
%include typemaps.i
%{
#define _WIN32_WINNT 0x400
#include "windows.h"
#if PY_MAJOR_VERSION >= 3
#define PY3K
#endif
PyObject* callback_funcs[WH_MAX];
HHOOK hHooks[WH_MAX];
BYTE key_state[256];
%}
#ifdef SWIGPYTHON
%typemap(in) PyObject *pyfunc {
if (!PyCallable_Check($input)) {
PyErr_SetString(PyExc_TypeError, "Need a callable object");
return NULL;
}
$1 = $input;
}
#endif
%init %{
memset(key_state, 0, 256);
memset(callback_funcs, 0, WH_MAX);
memset(hHooks, 0, WH_MAX);
PyEval_InitThreads();
// get initial key state
Py_BEGIN_ALLOW_THREADS
key_state[VK_NUMLOCK] = (GetKeyState(VK_NUMLOCK)&0x0001) ? 0x01 : 0x00;
key_state[VK_CAPITAL] = (GetKeyState(VK_CAPITAL)&0x0001) ? 0x01 : 0x00;
key_state[VK_SCROLL] = (GetKeyState(VK_SCROLL)&0x0001) ? 0x01 : 0x00;
Py_END_ALLOW_THREADS
%}
%wrapper %{
unsigned short ConvertToASCII(unsigned int keycode, unsigned int scancode);
void UpdateKeyState(unsigned int vkey, int msg);
LRESULT CALLBACK cLLKeyboardCallback(int code, WPARAM wParam, LPARAM lParam) {
PyObject *arglist, *r;
PKBDLLHOOKSTRUCT kbd;
HWND hwnd;
PSTR win_name = NULL;
unsigned short ascii = 0;
static int win_len;
static long result;
long pass = 1;
PyGILState_STATE gil;
#ifdef PY3K
PyObject *win_name_decoded = NULL;
#endif
// uncomment this next bit if you do not want to process events like "ctl-alt-del"
// and other events that are not supposed to be processed
// as per msdn documentation:
// http://msdn.microsoft.com/en-us/library/ms644985(VS.85).aspx
// if message code < 0, return immediately
//if(code<0)
// CallNextHookEx(hHooks[WH_KEYBOARD_LL], code, wParam, lParam);
// get the GIL
gil = PyGILState_Ensure();
// cast to a keyboard event struct
kbd = (PKBDLLHOOKSTRUCT)lParam;
// get the current foreground window (might not be the real window that received the event)
hwnd = GetForegroundWindow();
// grab the window name if possible
win_len = GetWindowTextLength(hwnd);
if(win_len > 0) {
win_name = (PSTR) malloc(sizeof(char) * win_len + 1);
GetWindowText(hwnd, win_name, win_len + 1);
}
#ifdef PY3K
win_name_decoded = PyUnicode_DecodeFSDefault(win_name);
#endif
// convert to an ASCII code if possible
ascii = ConvertToASCII(kbd->vkCode, kbd->scanCode);
// pass the message on to the Python function
#ifdef PY3K
arglist = Py_BuildValue("(iiiiiiiu)", wParam, kbd->vkCode, kbd->scanCode, ascii,
kbd->flags, kbd->time, hwnd, win_name_decoded);
#else
arglist = Py_BuildValue("(iiiiiiiz)", wParam, kbd->vkCode, kbd->scanCode, ascii,
kbd->flags, kbd->time, hwnd, win_name);
#endif
if(arglist == NULL)
PyErr_Print();
r = PyObject_CallObject(callback_funcs[WH_KEYBOARD_LL], arglist);
#ifdef PY3K
// release unicode object
Py_DECREF(win_name_decoded);
#endif
// check if we should pass the event on or not
if(r == NULL)
PyErr_Print();
else
pass = PyInt_AsLong(r);
Py_XDECREF(r);
Py_DECREF(arglist);
// release the GIL
PyGILState_Release(gil);
// free the memory for the window name
if(win_name != NULL)
free(win_name);
// decide whether or not to call the next hook
if(code < 0 || pass) {
UpdateKeyState(kbd->vkCode, wParam);
result = CallNextHookEx(hHooks[WH_KEYBOARD_LL], code, wParam, lParam);
} else {
// return a non-zero to prevent further processing
result = 42;
}
return result;
}
LRESULT CALLBACK cLLMouseCallback(int code, WPARAM wParam, LPARAM lParam) {
PyObject *arglist, *r;
PMSLLHOOKSTRUCT ms;
HWND hwnd;
PSTR win_name = NULL;
static int win_len;
static long result;
long pass = 1;
PyGILState_STATE gil;
#ifdef PY3K
PyObject *win_name_decoded = NULL;
#endif
// get the GIL
gil = PyGILState_Ensure();
//pass the message on to the Python function
ms = (PMSLLHOOKSTRUCT)lParam;
hwnd = WindowFromPoint(ms->pt);
//grab the window name if possible
win_len = GetWindowTextLength(hwnd);
if(win_len > 0) {
win_name = (PSTR) malloc(sizeof(char) * win_len + 1);
GetWindowText(hwnd, win_name, win_len + 1);
}
#ifdef PY3K
win_name_decoded = PyUnicode_DecodeFSDefault(win_name);
#endif
//build the argument list to the callback function
#ifdef PY3K
arglist = Py_BuildValue("(iiiiiiiu)", wParam, ms->pt.x, ms->pt.y, ms->mouseData,
ms->flags, ms->time, hwnd, win_name_decoded);
#else
arglist = Py_BuildValue("(iiiiiiiz)", wParam, ms->pt.x, ms->pt.y, ms->mouseData,
ms->flags, ms->time, hwnd, win_name);
#endif
if(arglist == NULL)
PyErr_Print();
r = PyObject_CallObject(callback_funcs[WH_MOUSE_LL], arglist);
#ifdef PY3K
// release unicode object
Py_DECREF(win_name_decoded);
#endif
// check if we should pass the event on or not
if(r == NULL)
PyErr_Print();
else
pass = PyInt_AsLong(r);
Py_XDECREF(r);
Py_DECREF(arglist);
// release the GIL
PyGILState_Release(gil);
//free the memory for the window name
if(win_name != NULL)
free(win_name);
// decide whether or not to call the next hook
if(code < 0 || pass)
result = CallNextHookEx(hHooks[WH_MOUSE_LL], code, wParam, lParam);
else {
// return non-zero to prevent further processing
result = 42;
}
return result;
}
int cSetHook(int idHook, PyObject *pyfunc) {
HINSTANCE hMod;
//make sure we have a valid hook number
if(idHook > WH_MAX || idHook < WH_MIN) {
PyErr_SetString(PyExc_ValueError, "Hooking error: invalid hook ID");
}
//get the module handle
Py_BEGIN_ALLOW_THREADS
// try to get handle for current file - will succeed if called from a compiled .exe
hMod = GetModuleHandle(NULL);
if(NULL == hMod) // otherwise use name for DLL
hMod = GetModuleHandle("_cpyHook.pyd");
Py_END_ALLOW_THREADS
//switch on the type of hook so we point to the right C callback
switch(idHook) {
case WH_MOUSE_LL:
if(callback_funcs[idHook] != NULL)
break;
callback_funcs[idHook] = pyfunc;
Py_INCREF(callback_funcs[idHook]);
Py_BEGIN_ALLOW_THREADS
hHooks[idHook] = SetWindowsHookEx(WH_MOUSE_LL, cLLMouseCallback, (HINSTANCE) hMod, 0);
Py_END_ALLOW_THREADS
break;
case WH_KEYBOARD_LL:
if(callback_funcs[idHook] != NULL)
break;
callback_funcs[idHook] = pyfunc;
Py_INCREF(callback_funcs[idHook]);
Py_BEGIN_ALLOW_THREADS
hHooks[idHook] = SetWindowsHookEx(WH_KEYBOARD_LL, cLLKeyboardCallback, (HINSTANCE) hMod, 0);
Py_END_ALLOW_THREADS
break;
default:
return 0;
}
if(!hHooks[idHook]) {
PyErr_SetString(PyExc_TypeError, "Could not set hook");
}
return 1;
}
int cUnhook(int idHook) {
BOOL result;
//make sure we have a valid hook number
if(idHook > WH_MAX || idHook < WH_MIN) {
PyErr_SetString(PyExc_ValueError, "Invalid hook ID");
}
//unhook the callback
Py_BEGIN_ALLOW_THREADS
result = UnhookWindowsHookEx(hHooks[idHook]);
Py_END_ALLOW_THREADS
if(result) {
//decrease the ref to the Python callback
Py_DECREF(callback_funcs[idHook]);
callback_funcs[idHook] = NULL;
}
return result;
}
void SetKeyState(unsigned int vkey, int down) {
// (1 > 0) ? True : False
if (vkey == VK_MENU || vkey == VK_LMENU || vkey == VK_RMENU) {
key_state[vkey] = (down) ? 0x80 : 0x00;
key_state[VK_MENU] = key_state[VK_LMENU] | key_state[VK_RMENU];
} else if (vkey == VK_SHIFT || vkey == VK_LSHIFT || vkey == VK_RSHIFT) {
key_state[vkey] = (down) ? 0x80 : 0x00;
key_state[VK_SHIFT] = key_state[VK_LSHIFT] | key_state[VK_RSHIFT];
} else if (vkey == VK_CONTROL || vkey == VK_LCONTROL || vkey == VK_RCONTROL) {
key_state[vkey] = (down) ? 0x80 : 0x00;
key_state[VK_CONTROL] = key_state[VK_LCONTROL] | key_state[VK_RCONTROL];
} else if (vkey == VK_NUMLOCK && !down) {
key_state[VK_NUMLOCK] = !key_state[VK_NUMLOCK];
} else if (vkey == VK_CAPITAL && !down) {
key_state[VK_CAPITAL] = !key_state[VK_CAPITAL];
} else if (vkey == VK_SCROLL && !down) {
key_state[VK_SCROLL] = !key_state[VK_SCROLL];
}
}
void UpdateKeyState(unsigned int vkey, int msg) {
if (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN) {
SetKeyState(vkey, 1);
} else if (msg == WM_KEYUP || msg == WM_SYSKEYUP) {
SetKeyState(vkey, 0);
}
}
unsigned int cGetKeyState(unsigned int vkey) {
return key_state[vkey];
}
unsigned short ConvertToASCII(unsigned int keycode, unsigned int scancode) {
int r;
unsigned short c = 0;
Py_BEGIN_ALLOW_THREADS
r = ToAscii(keycode, scancode, key_state, &c, 0);
Py_END_ALLOW_THREADS
if(r < 0) {
//PyErr_SetString(PyExc_ValueError, "Could not convert to ASCII");
return 0;
}
return c;
}
%}
unsigned int cGetKeyState(unsigned int vkey);
int cSetHook(int idHook, PyObject *pyfunc);
int cUnhook(int idHook);