Skip to content

Instantly share code, notes, and snippets.

@RdlP
Created March 30, 2017 17:37
Show Gist options
  • Select an option

  • Save RdlP/a26d0a72d62c80214885af7cfd03aae6 to your computer and use it in GitHub Desktop.

Select an option

Save RdlP/a26d0a72d62c80214885af7cfd03aae6 to your computer and use it in GitHub Desktop.
from Xlib.display import Display
import Xlib
from Xlib import X, XK
import Xlib.XK
import sys
import signal
import Queue
display = None
root = None
queue = []
isBlock = False
def handle_event(aEvent):
global queue, isBlock
keysym = display.keycode_to_keysym(aEvent.detail, 0)
if aEvent.type == X.KeyReleaseMask:
if len(queue) == 6:
queue = queue[1:6]
queue.append(lookup_keysym(keysym))
st = "".join([str(x) for x in queue])
if "hello" in st:
if isBlock == False:
queue = []
block()
else:
queue = []
unBlock()
def lookup_keysym(keysym):
for name in dir(XK):
if name[:3] == "XK_" and getattr(XK, name) == keysym:
return name[3:]
return "[%d]" % keysym
def block():
global isBlock, root
isBlock = True
root.grab_pointer(True, X.ButtonPressMask | X.ButtonReleaseMask, X.GrabModeAsync, X.GrabModeAsync, 0, 0, X.CurrentTime)
root.grab_key(Xlib.X.AnyKey, X.AnyModifier, True,X.GrabModeSync, X.GrabModeSync)
while 1:
event = display.next_event()
print "You can't do anything"
#if i dont call this, the whole thing freezes
display.allow_events(X.AsyncKeyboard, X.CurrentTime)
display.allow_events(X.AsyncPointer, X.CurrentTime)
handle_event(event)
def unBlock():
global isBlock, root, display
isBlock = False
display.ungrab_pointer(X.CurrentTime)
root.grab_key(Xlib.X.AnyKey, X.AnyModifier, True,X.GrabModeSync, X.GrabModeSync)
while 1:
event = display.next_event()
print "Now, you can do"
#if i dont call this, the whole thing freezes
display.allow_events(X.ReplayKeyboard, X.CurrentTime)
#display.allow_events(X.ReplayPointer, X.CurrentTime)
handle_event(event)
def main():
# current display
global display,root,isBlock
display = Display()
root = display.screen().root
# we tell the X server we want to catch keyPress event
root.change_attributes(event_mask = X.KeyPressMask | X.KeyReleaseMask)
if isBlock == False:
block()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment