python-xlib icon indicating copy to clipboard operation
python-xlib copied to clipboard

Question regarding KeyPress

Open miniskipper opened this issue 6 years ago • 3 comments

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?

miniskipper avatar Oct 31 '19 11:10 miniskipper

What you "get" depends on the window (application) receiving the event ...

matanox avatar Sep 28 '20 10:09 matanox

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.

david672orford avatar Jan 17 '21 03:01 david672orford

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()

vasily-v-ryabov avatar Apr 22 '21 13:04 vasily-v-ryabov