Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

evdev Not Correctly Capturing LEFTSHIFT Key Presses #209

Open
mroavi opened this issue Feb 22, 2024 · 0 comments
Open

evdev Not Correctly Capturing LEFTSHIFT Key Presses #209

mroavi opened this issue Feb 22, 2024 · 0 comments

Comments

@mroavi
Copy link

mroavi commented Feb 22, 2024

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

mroavi added a commit to mroavi/onthefly that referenced this issue Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant