diff --git a/pyxhook.py b/pyxhook.py index ac043a2..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: @@ -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 = {} @@ -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"): @@ -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) @@ -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()