3

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 (i.e. there is no escape). However it should be possible to 'release' this 'grabbing' later or at any time.

Community
  • 1
  • 1
Amandasaurus
  • 58,203
  • 71
  • 188
  • 248

1 Answers1

0

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 (i.e. visible); using the root window works fine if you do not wish to map a new window.

gandalf3
  • 1,636
  • 4
  • 24
  • 40