Question regarding KeyPress
When sending a KeyPress event with below code I would assume the key stays pressed until ... well ... forever. But all I get is a single "a".
`from Xlib import display,protocol,X,XK import time
display = display.Display() focus= display.get_input_focus() window = focus.focus keysym=XK.string_to_keysym("a") keycode=display.keysym_to_keycode(keysym) send_event = protocol.event.KeyPress( time=int(time.time()), root=display.screen().root, window=window, child=X.NONE, same_screen=0, root_x=0, root_y=0, event_x=0, event_y=0, state=0, detail=keycode ) window.send_event(send_event) display.sync()`
How to achieve a held down button?
What you "get" depends on the window (application) receiving the event ...
It won't repeat, if that is what you are expecting. The repeating is done at a low level, perhaps in the keyboard driver. The application receives keydown and keyup for each repetition. The only way your one event would generate multiple letters would be if key repetition were implemented in the application, and it isn't. To prove this to yourself, run xev and watch what happens when you hold down a key.
In the GUI automation library we use fake_input function from XTest extension. You can take a look here: https://github.com/pywinauto/pywinauto/blob/atspi/pywinauto/linux/keyboard.py
if self.down:
fake_input(_display, X.KeyPress, self.key)
_display.sync()
if self.up:
fake_input(_display, X.KeyRelease, self.key)
_display.sync()