forked from roxana-lafuente/ResearchLogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyxhook.py
402 lines (356 loc) · 15.8 KB
/
pyxhook.py
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# -*- coding: utf-8 -*-
# !/usr/bin/python
#
# pyxhook -- an extension to emulate some of the PyHook library on linux.
#
# Copyright (C) 2008 Tim Alexander <[email protected]>
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307uSA
#
# Thanks to Alex Badea <[email protected]> for writing the Record
# demo for the xlib libraries. It helped me immensely working with these
# in this library.
#
# Thanks to the python-xlib team. This wouldn't have been possible without
# your code.
# This requires:
# at least python-xlib 1.4
# xwindows must have the "record" extension present, and active.
#
# This file has now been somewhat extensively modified by
# Daniel Folkinshteyn <[email protected]>
# So if there are any bugs, they are probably my fault. :)
#
# 2015 Modifications by Roxana Lafuente <[email protected]>
import sys
import re
import time
import threading
from Xlib import X, XK, display
from Xlib.ext import record
from Xlib.protocol import rq
from constants import KDOWN, KUP, MLEFT, MMIDDLE, MRIGHT, MWHEELUP, MWHEELDOWN, KMAP
#########################################################################
# #######################START CLASS DEF############################### #
#########################################################################
class HookManager(threading.Thread):
"""
This is the main class. Instantiate it, and you can hand it KeyDown and
KeyUp (functions in your own code) which execute to parse the
PyXHookKeyEvent class that is returned.
This simply takes these two values for now:
- KeyDown = The function to execute when a key is pressed, if it
returns anything. It hands the function an argument that is the
PyXHookKeyEvent class.
- KeyUp = The function to execute when a key is released, if it
returns anything. It hands the function an argument that is the
PyXHookKeyEvent class.
"""
def __init__(self):
threading.Thread.__init__(self)
self.finished = threading.Event()
# Give these some initial values
self.mouse_position_x = 0
self.mouse_position_y = 0
self.ison = {"shift": False, "caps": False}
# Compile our regex statements.
self.isshift = re.compile('^Shift')
self.iscaps = re.compile('^Caps_Lock')
self.shiftablechar = re.compile('^[a-z0-9]$|^minus$|^equal$|'
'^bracketleft$|^bracketright$|'
'^semicolon$|^backslash$|^apostrophe$|'
'^comma$|^period$|^slash$|^grave$')
self.logrelease = re.compile('.*')
self.isspace = re.compile('^space$')
# Assign default function actions (do nothing).
self.KeyDown = lambda x: True
self.KeyUp = lambda x: True
self.MouseAllButtonsDown = lambda x: True
self.MouseAllButtonsUp = lambda x: True
self.contextEventMask = [X.KeyPress, X.MotionNotify]
# Hook to our display.
self.local_dpy = display.Display()
self.record_dpy = display.Display()
def run(self):
# Check if the extension is present.
if not self.record_dpy.has_extension("RECORD"):
print "RECORD extension not found"
sys.exit(1)
r = self.record_dpy.record_get_version(0, 0)
print "RECORD extension version %d.%d" % \
(r.major_version, r.minor_version)
# Create a recording context; we only want key and mouse events.
self.ctx = self.record_dpy.record_create_context(
0,
[record.AllClients],
[{
'core_requests': (0, 0),
'core_replies': (0, 0),
'ext_requests': (0, 0, 0, 0),
'ext_replies': (0, 0, 0, 0),
'delivered_events': (0, 0),
# (X.KeyPress, X.ButtonPress),
'device_events': tuple(self.contextEventMask),
'errors': (0, 0),
'client_started': False,
'client_died': False,
}])
# Enable the context; this only returns after a call to
# record_disable_context, while calling the callback function in the
# meantime.
self.record_dpy.record_enable_context(self.ctx, self.processevents)
# Finally free the context.
self.record_dpy.record_free_context(self.ctx)
def cancel(self):
self.finished.set()
self.local_dpy.record_disable_context(self.ctx)
self.local_dpy.flush()
def printevent(self, event):
print event
def HookKeyboard(self):
pass
# We don't need to do anything here anymore, since the default mask
# is now set to contain X.KeyPress
# self.contextEventMask[0] = X.KeyPress
def HookMouse(self):
pass
# We don't need to do anything here anymore, since the default mask
# is now set to contain X.MotionNotify
# need mouse motion to track pointer position, since ButtonPress events
# don't carry that info.
# self.contextEventMask[1] = X.MotionNotify
def processevents(self, reply):
if reply.category != record.FromServer:
return
if reply.client_swapped:
print "* received swapped protocol data, cowardly ignored"
return
if not len(reply.data) or ord(reply.data[0]) < 2:
# not an event
return
data = reply.data
while len(data):
event, data = rq.EventField(None).parse_binary_value(
data, self.record_dpy.display, None, None)
if event.type == X.KeyPress:
hookevent = self.keypressevent(event)
self.KeyDown(hookevent)
elif event.type == X.KeyRelease:
hookevent = self.keyreleaseevent(event)
self.KeyUp(hookevent)
elif event.type == X.ButtonPress:
hookevent = self.buttonpressevent(event)
self.MouseAllButtonsDown(hookevent)
elif event.type == X.ButtonRelease:
hookevent = self.buttonreleaseevent(event)
self.MouseAllButtonsUp(hookevent)
elif event.type == X.MotionNotify:
# use mouse moves to record mouse position, since press and
# release events do not give mouse position info (event.root_x
# and event.root_y have bogus info).
self.mousemoveevent(event)
def keypressevent(self, event):
matchto = self.lookup_keysym(self.local_dpy.keycode_to_keysym(
event.detail, 0))
# This is a character that can be typed.
if self.shiftablechar.match(self.lookup_keysym(
self.local_dpy.keycode_to_keysym(event.detail, 0))):
if self.ison["shift"] is False:
keysym = self.local_dpy.keycode_to_keysym(event.detail, 0)
return self.makekeyhookevent(keysym, event)
else:
keysym = self.local_dpy.keycode_to_keysym(event.detail, 1)
return self.makekeyhookevent(keysym, event)
# Not a typable character.
else:
keysym = self.local_dpy.keycode_to_keysym(event.detail, 0)
if self.isshift.match(matchto):
self.ison["shift"] = self.ison["shift"] + 1
elif self.iscaps.match(matchto):
if self.ison["caps"] is False:
self.ison["shift"] = self.ison["shift"] + 1
self.ison["caps"] = True
if self.ison["caps"] is True:
self.ison["shift"] = self.ison["shift"] - 1
self.ison["caps"] = False
return self.makekeyhookevent(keysym, event)
def keyreleaseevent(self, event):
if self.shiftablechar.match(self.lookup_keysym(
self.local_dpy.keycode_to_keysym(event.detail, 0))):
if self.ison["shift"] is False:
keysym = self.local_dpy.keycode_to_keysym(event.detail, 0)
else:
keysym = self.local_dpy.keycode_to_keysym(event.detail, 1)
else:
keysym = self.local_dpy.keycode_to_keysym(event.detail, 0)
matchto = self.lookup_keysym(keysym)
if self.isshift.match(matchto):
self.ison["shift"] = self.ison["shift"] - 1
return self.makekeyhookevent(keysym, event)
def buttonpressevent(self, event):
return self.makemousehookevent(event)
def buttonreleaseevent(self, event):
return self.makemousehookevent(event)
def mousemoveevent(self, event):
self.mouse_position_x = event.root_x
self.mouse_position_y = event.root_y
# Need the following because XK.keysym_to_string() only does printable
# chars rather than being the correct inverse of XK.string_to_keysym().
def lookup_keysym(self, keysym):
try:
return KMAP[keysym]
except KeyError:
return str(keysym)
def asciivalue(self, keysym):
asciinum = XK.string_to_keysym(self.lookup_keysym(keysym))
if asciinum < 256:
return asciinum
else:
return 0
def makekeyhookevent(self, keysym, event):
storewm = self.xwindowinfo()
if event.type == X.KeyPress:
MessageName = KDOWN
elif event.type == X.KeyRelease:
MessageName = KUP
return PyXHookKeyEvent(storewm["handle"], storewm["name"],
storewm["class"], self.lookup_keysym(keysym),
self.asciivalue(keysym), False, event.detail,
MessageName, event.root_x, event.root_y,
event.time)
def makemousehookevent(self, event):
storewm = self.xwindowinfo()
if event.detail == 1:
MessageName = MLEFT
elif event.detail == 3:
MessageName = MRIGHT
elif event.detail == 2:
MessageName = MMIDDLE
elif event.detail == 5:
MessageName = MWHEELDOWN
elif event.detail == 4:
MessageName = MWHEELUP
else:
MessageName = "mouse " + str(event.detail) + " "
if event.type == X.ButtonPress:
MessageName = MessageName + "_down"
elif event.type == X.ButtonRelease:
MessageName = MessageName + "_up"
return PyXHookMouseEvent(storewm["handle"], storewm["name"],
storewm["class"], (self.mouse_position_x,
self.mouse_position_y), MessageName,
event.time)
def xwindowinfo(self):
try:
windowvar = self.local_dpy.get_input_focus().focus
wmname = windowvar.get_wm_name()
wmclass = windowvar.get_wm_class()
wmhandle = str(windowvar)[20:30]
except:
# This is to keep things running smoothly. It almost never happens,
# but still...
return {"name": None, "class": None, "handle": None}
if (wmname is None) and (wmclass is None):
try:
windowvar = windowvar.query_tree().parent
wmname = windowvar.get_wm_name()
wmclass = windowvar.get_wm_class()
wmhandle = str(windowvar)[20:30]
except:
# This is to keep things running smoothly. It almost never
# happens, but still...
return {"name": None, "class": None, "handle": None}
if wmclass is None:
return {"name": wmname, "class": wmclass, "handle": wmhandle}
else:
return {"name": wmname, "class": wmclass[0], "handle": wmhandle}
class PyXHookKeyEvent:
"""
This is the class that is returned with each key event.f
It simply creates the variables below in the class.
- window = The handle of the window.
- window_name = The name of the window.
- window_proc_name = The backend process for the window.
- key = The key pressed, shifted to the correct caps value.
- ascii = An ascii representation of the key. It returns 0 if the ascii
value is not between 31 and 256.
- key_id = This is just False for now. Under windows, it is the Virtual
Key Code, but that's a windows-only thing.
- scan_code = Please don't use this. It differs for pretty much every
type of keyboard. X11 abstracts this information anyway.
- message_name = KDOWN, KUP.
"""
def __init__(self, window, window_name, window_proc_name, key, ascii,
key_id, scan_code, message_name, mx, my, time):
self.Window = window
self.WindowName = window_name
self.WindowProcName = window_proc_name
self.Key = key
self.Ascii = ascii
self.KeyID = key_id
self.ScanCode = scan_code
self.MessageName = message_name
self.MouseX = mx
self.MouseY = my
self.Time = time
def __str__(self):
return "Window Handle: " + str(self.Window) + "\nWindow Name: " + \
str(self.WindowName) + "\nWindow's Process Name: " + \
str(self.WindowProcName) + "\nKey Pressed: " + str(self.Key) + \
"\nAscii Value: " + str(self.Ascii) + "\nKeyID: " + \
str(self.KeyID) + "\nScanCode: " + str(self.ScanCode) + \
"\nMessageName: " + str(self.MessageName) + \
"\nMouseX: " + str(self.MouseX) + \
"\nMouseY: " + str(self.MouseY) + \
"\nTime: " + str(self.Time) + "\n"
class PyXHookMouseEvent:
"""
This is the class that is returned with each key event.f
It simply creates the variables below in the class.
Window = The handle of the window.
WindowName = The name of the window.
WindowProcName = The backend process for the window.
Position = 2-tuple (x,y) coordinates of the mouse click
MessageName = "MLEFT|MRIGHT|MMIDDLE",
"mouse left|right|middle up".
"""
def __init__(self, window, window_name, window_proc_name, position,
message_name, time):
self.Window = window
self.WindowName = window_name
self.WindowProcName = window_proc_name
self.Position = position
self.MessageName = message_name
self.Time = time
def __str__(self):
return "Window Handle: " + str(self.Window) + "\nWindow Name: " + \
str(self.WindowName) + "\nWindow's Process Name: " \
+ str(self.WindowProcName) + "\nPosition: " + \
str(self.Position) + "\nMessageName: " + str(self.MessageName) + \
"\nTime: " + str(self.Time) + "\n"
#########################################################################
# ########################END CLASS DEF################################ #
#########################################################################
if __name__ == '__main__':
hm = HookManager()
hm.HookKeyboard()
hm.HookMouse()
hm.KeyDown = hm.printevent
hm.KeyUp = hm.printevent
hm.MouseAllButtonsDown = hm.printevent
hm.MouseAllButtonsUp = hm.printevent
hm.start()
time.sleep(10)
hm.cancel()