简体   繁体   中英

Simple Linux/X11 programme to grab and keep the keyboard focus?

I am writing a simple typing break / anti-RSI programme on Linux (Ubuntu) in Python. I asked similar questions about locking the keyboard ( How do I 'lock the keyboard' to prevent any more keypresses being sent on X11/Linux/Gnome? and Releasing all keys after disabling the keyboard in X11/Linux using xinput? ) and someone suggested ( https://stackoverflow.com/a/10769704/161922 ) that grabbing the keyboard focus would be the best way. However I'm new to X11 programming. How would I do that in Python? (Presume I have python-xlib or whatever installed, I can also install anything from pypi or apt).

How do you make a simple X11 application that will grab the keyboard focus (even if another window is currently focussed). It should not be possible for the user to alt-tab or alt-f4 away from this (ie there is no escape). However it should be possible to 'release' this 'grabbing' later or at any time.

So it's been a literal decade but by some spectacular coincidence I stumbled upon this right after writing such a program so it felt silly to not mention it in case it may be useful to any one else who comes across this: https://git.sr.ht/~kitt/inputgrabby .

The main idea to grabbing the keyboard is to just to call either XGrabKeyboard if using Xlib or as my program uses xcb, xcb_grab_keyboard . For the grab to succeed, the grab_window must be mapped (ie visible); using the root window works fine if you do not wish to map a new window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM