Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions pyxhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def __init__(self,parameters=False):
)))
self.logrelease = re.compile('.*')
self.isspace = re.compile('^space$')
self.lookuptable = self.create_lookup_dict()
# Choose which type of function use
self.parameters=parameters
if parameters:
Expand All @@ -102,7 +103,7 @@ def __init__(self,parameters=False):
self.MouseAllButtonsDown = self.lambda_function
self.MouseAllButtonsUp = self.lambda_function
self.MouseMovement = self.lambda_function

self.KeyDownParameters = {}
self.KeyUpParameters = {}
self.MouseAllButtonsDownParameters = {}
Expand All @@ -115,6 +116,11 @@ def __init__(self,parameters=False):
self.local_dpy = display.Display()
self.record_dpy = display.Display()

def create_lookup_dict(self):
return {getattr(XK, name): name[3:] \
for name in dir(XK) \
if name.startswith("XK_")}

def run(self):
# Check if the extension is present
if not self.record_dpy.has_extension("RECORD"):
Expand Down Expand Up @@ -175,7 +181,7 @@ def HookMouse(self):

def processhookevents(self,action_type,action_parameters,events):
# In order to avoid duplicate code, i wrote a function that takes the
# input value of the action function and, depending on the initialization,
# input value of the action function and, depending on the initialization,
# launches it or only with the event or passes the parameter
if self.parameters:
action_type(events,action_parameters)
Expand Down Expand Up @@ -303,14 +309,13 @@ def mousemoveevent(self, event):
# 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):
for name in dir(XK):
if name.startswith("XK_") and getattr(XK, name) == keysym:
return name.lstrip("XK_")
return "[{}]".format(keysym)
return self.lookuptable[keysym] \
if keysym in self.lookuptable \
else "[{}]".format(keysym)

def asciivalue(self, keysym):
asciinum = XK.string_to_keysym(self.lookup_keysym(keysym))
return asciinum % 256
number = XK.string_to_keysym(self.lookup_keysym(keysym))
return number if number < 256 else 0

def makekeyhookevent(self, keysym, event):
storewm = self.xwindowinfo()
Expand Down