Description
I've encountered an issue with evdev in my application, which stopped working as expected. The problem is that evdev is not capturing LEFTSHIFT key presses correctly. Adding a small delay after the press and release of the LEFTSHIFT key resolves the issue. Below is a minimal example that reproduces the problem:
# This code does not work as expected. It emits a lowercase 'a', whereas it should emit an uppercase 'A'.
from evdev import UInput, InputDevice, ecodes as e
import time
keybd = '/dev/input/event18'
ui = UInput.from_device(keybd, name='keyboard-device')
ui.capabilities(verbose=True).keys()
ui.write(e.EV_KEY, e.KEY_LEFTSHIFT, 1) # KEY_LEFTSHIFT down
ui.syn()
ui.write(e.EV_KEY, e.KEY_A, 1) # KEY_A down
ui.write(e.EV_KEY, e.KEY_A, 0) # KEY_A up
ui.syn()
ui.write(e.EV_KEY, e.KEY_LEFTSHIFT, 0) # KEY_LEFTSHIFT up
ui.syn()
ui.close()
Adding small delays after the LEFTSHIFT press and release fixes the issue:
# This code works as expected. It emits an uppercase 'A'.
from evdev import UInput, InputDevice, ecodes as e
import time
keybd = '/dev/input/event18'
ui = UInput.from_device(keybd, name='keyboard-device')
ui.capabilities(verbose=True).keys()
ui.write(e.EV_KEY, e.KEY_LEFTSHIFT, 1) # KEY_LEFTSHIFT down
time.sleep(0.05) # <-- FIXES THE PROBLEM
ui.syn()
ui.write(e.EV_KEY, e.KEY_A, 1) # KEY_A down
ui.write(e.EV_KEY, e.KEY_A, 0) # KEY_A up
ui.syn()
ui.write(e.EV_KEY, e.KEY_LEFTSHIFT, 0) # KEY_LEFTSHIFT up
time.sleep(0.05) # <-- FIXES THE PROBLEM
ui.syn()
ui.close()
This behavior suggests that evdev may require a brief period to correctly register the LEFTSHIFT key state change, which is not documented or expected. Has anyone else experienced this issue, or is there a known workaround or fix?
Specifications of my machine
OS: Arch Linux x86_64
Host: XPS 17 9720
Kernel: 6.7.5-arch1-1
Uptime: 3 hours, 47 mins
Packages: 1369 (pacman)
Shell: zsh 5.9
Resolution: 1920x1200, 1920x1080
WM: dwm
Theme: Raleigh [GTK2/3]
Icons: Adwaita [GTK2/3]
Terminal: tmux
CPU: 12th Gen Intel i7-12700H (20) @ 4.600GHz
GPU: Intel Alder Lake-P GT2 [Iris Xe Graphics]
GPU: NVIDIA GeForce RTX 3050 Mobile
Memory: 10185MiB / 15678MiB