Releasing All Keys After Disabling The Keyboard in X11/Linux Using Xinput

How do I 'lock the keyboard' to prevent any more keypresses being sent on X11/Linux/Gnome?

This can be done easily with a shell script using xinput :

 #!/bin/sh

do_it() {
# need error checking there. We should also restrict which device gets
# deactivated, by checking other properties.
keyboard_ids="$(xinput list | sed -rn 's/.*id=([0-9]+).*slave\s+keyboard.*/\1/p')"

for keyboard_id in $keyboard_ids; do
# 121 is "Device Active".
# use xinput watch-props $device_id to see some properties.
xinput set-int-prop $keyboard_id 121 8 $1;
done;
}
# you maybe don't want to exit in case of failure there.
do_it 0 ; sleep 5; do_it 1

This logic is easily rewritable in Python. If installing xinput is problematic, it might be a good idea to fetch the source of xinput and try to reimplement it in Python using a library like python-xlib.

Excluding some keys from XGrabKeyboard

I don't think there's a way to do it. None of the mechanisms work quite how you would need them to.

Approach 1 is sort of what the window manager does if it decides not to intercept a click or key for example. However, the WM is using "passive" grabs on particular keys (XGrabKey=passive XGrabKeyboard=active) and then XAllowEvents(). XAllowEvents() does not work with XGrabKeyboard(). also, when you XAllowEvents with one of the Replay modes, the replayed event bypasses all passive grabs on the window that had the original grab and on all its parent windows. The WM's grabs will be on the root window which will always be a parent so there is no way to replay to the root window, best I can tell. Doing XGrabKey on every possible key would be sort of psycho anyhow.

Approach 2 would have bad race condition problems, because other key and mouse events could be processed before you could resend, so you'd reorder keys and send events to destroyed windows and other confusion. Also, there is no good way to send a key event. XSendEvent() is ignored by many clients (it sets a send_event flag in the event allowing this). XTest extension can be used but may be disabled on production X servers and still has race condition issues.

What you probably would need is a protocol extension that let you do an AllowEvents(mode=ReplayKeyboard) after a GrabKeyboard and without bypassing passive grabs on parent windows.

One caveat is that I don't know all the wild stuff that can be done with XKB and XInput2, so maybe there's something in those extensions.

Anyway, as far as I know you have to settle for the "escape key," though it might be nice eventually for the X server and/or the window manager specs to have "VMWare/VNC-type-thing awareness," that won't help you in the short term. An EWMH spec extension could be as simple as a new _NET_WM_WINDOW_TYPE for vnc/vmware/stuff-like-that and the window manager could reduce its keybindings or add an extra modifier to them or something when that window was focused, for example.

C/C++: Disable mouse in Linux (X11 - xinput)

You do realize that xinput is open source, right? :) Here's the source tree.

It seems to boil down to a call to XIChangeProperty().

You can probably read the code a bit more closely than I did, and the manual page of course, to figure out the required arguments.



Related Topics



Leave a reply



Submit