Releasing all keys after disabling the keyboard in X11/Linux using xinput?

两盒软妹~` 提交于 2019-12-04 18:23:20

"I am writing a programme that needs to disable they keyboard, and want to avoid this problem"

What's wrong with grabbing the keyboard focus and discarding all input?

xinput is a pretty blunt instrument and you're hitting a bug that's more than two years old which means it isn't likely to get fixed. Indeed, the bug is probably far older than that, and could be considered "expected behavior" by someone making use of it.

What you experience is a peculiarity of how keyboard events are processed by terminal emulators and the shell reacts. When you press enter the shell will execute the command given to it, and your command xinput ... will finish before enter is even depressed. Since the keyboard gets disabled no key release event will be even entering the event processing.

It's not a bug, it's a feature.

How to work around it: Either wait for all keys to be depressed before actually executing the detach, or just add a sleep before the xinput command (those are both race conditions, so it's not 100% reliable).

I.e. putting this on the shell command line

sleep 1 ; xinput set-prop $ID "Device Enabled" 0

Will first sleep for a second before actually doing the xinput disable. If you don't keep the enter key pressed you should come out in the desired state.

just simulate any key click after xinput set-prop $ID "Device Enabled" 0, no sleep time before command needed

xinput set-prop $ID "Device Enabled" 0 ; xdotool key z
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!