i want to catch keydown and keyup events with python xlib, but keyup events disappear when some keys are pressed simultaneously.
if 2 or more keyes are released simultaneously then there will be 2 or more keypress events, but only 1 keyrelease event.
for this to happen the keys don't even have to be released simultaneously, for example if you enter this sequence fast:
- press A
- press B
- release A
- release B
will yield only 1 keyrelease for A
- press A
- press B
- release B
- release A
will yield 2 keyreleases
from Xlib import X,XK
from Xlib.display import Display
import signal,sys
root = None
display = None
def grab_keyname(n):
global root
keysym = XK.string_to_keysym(n)
keycode = display.keysym_to_keycode(keysym)
root.grab_key(keycode, X.AnyModifier, False,X.GrabModeSync, X.GrabModeAsync)
def main():
# current display
global display,root
display = Display()
root = display.screen().root
root.change_attributes(event_mask = X.KeyPressMask|X.KeyReleaseMask)
grab_keyname("j")
grab_keyname("k")
grab_keyname("l")
signal.signal(signal.SIGALRM, lambda a,b:sys.exit(1))
signal.alarm(4)
while True:
event = display.next_event()
print event.type
main()