我目前正在使用Python中的一个库来记录所有用户输入。源代码可以在这里找到:https://github.com/JeffHoogland/pyxhook/blob/master/pyxhook.py
我已经看了一段时间的源代码,但无法弄清楚它是如何工作的。我在整个源代码中跟踪了函数processevents(self, reply)
,并且不了解vairble event
的来源。确切的行是:
def processevents(self, reply):
if reply.category != record.FromServer:
return
if reply.client_swapped:
print("* received swapped protocol data, cowardly ignored")
return
try:
# Get int value, python2.
intval = ord(reply.data[0])
except TypeError:
# Already bytes/ints, python3.
intval = reply.data[0]
if (not reply.data) or (intval < 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).
hookevent = self.mousemoveevent(event)
self.MouseMovement(hookevent)
# print("processing events...", event.type)