From 0307ae524891d87e5286d766859b12c22b315651 Mon Sep 17 00:00:00 2001 From: Gerben Date: Mon, 4 Mar 2019 10:47:56 +0100 Subject: [PATCH 1/2] Fixes issue #27, all ascii values are now correct --- pyxhook.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pyxhook.py b/pyxhook.py index ac043a2..367332e 100644 --- a/pyxhook.py +++ b/pyxhook.py @@ -102,7 +102,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 = {} @@ -175,7 +175,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) @@ -305,12 +305,12 @@ def mousemoveevent(self, event): def lookup_keysym(self, keysym): for name in dir(XK): if name.startswith("XK_") and getattr(XK, name) == keysym: - return name.lstrip("XK_") + return name[3:] return "[{}]".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() From 2cc8e68cb3f619b858bc4a687752c8fdfba88a88 Mon Sep 17 00:00:00 2001 From: Gerben Date: Mon, 4 Mar 2019 11:14:32 +0100 Subject: [PATCH 2/2] Added a lookup table to remove string comparisions at every key event --- pyxhook.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pyxhook.py b/pyxhook.py index 367332e..b01821d 100644 --- a/pyxhook.py +++ b/pyxhook.py @@ -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: @@ -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"): @@ -303,10 +309,9 @@ 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[3:] - return "[{}]".format(keysym) + return self.lookuptable[keysym] \ + if keysym in self.lookuptable \ + else "[{}]".format(keysym) def asciivalue(self, keysym): number = XK.string_to_keysym(self.lookup_keysym(keysym))