Skip to content

Instantly share code, notes, and snippets.

@Neoklosch
Created July 15, 2015 15:31
Show Gist options
  • Save Neoklosch/843fbd34cd50e53216b4 to your computer and use it in GitHub Desktop.
Save Neoklosch/843fbd34cd50e53216b4 to your computer and use it in GitHub Desktop.
Basic implementation of keybinding with python xlib
from Xlib.display import Display
from Xlib import X
class KeyBinder(object):
def __init__(self):
self.cancel = False
self.xdisp = Display()
self.xroot = self.xdisp.screen().root
self.key_code = 71 # F5
def handle_event(self, event):
keycode = event.detail
if event.type == X.KeyPress:
print ">>> event captured"
self.cancel = True
def start(self):
self.xroot.change_attributes(event_mask = X.KeyPressMask)
self.xroot.grab_key(self.key_code, X.AnyModifier, 1, X.GrabModeAsync, X.GrabModeAsync)
while not self.cancel:
event = self.xroot.display.next_event()
self.handle_event(event)
if __name__ == '__main__':
keybinder = KeyBinder()
keybinder.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment