How can I disable OS-level keyboard shortcuts (e.g. Alt-Tab, Ctrl-Alt-Left/Right, etc.) on a [Ubuntu] Linux machine? I'm developing a full-screen Java Swing app and don't want the user to be able to task switch away from the program arbitrarily. It's not enough to toggle the "always on top" flag; users mustn't be allowed to switch workspaces, migrate focus or any other such things. The machine must function normally before and after the application is executed. Google says that this will require JNI or JNA but I'm looking for a bit more hand-holding.
1 Answers
There's no point in trying to do this in your application because any of these changes are going to need to be handled by X11 and/or the window manager since those are what respond to the commands. Assuming that you have control of the platform, choose a window manager which supports a kiosk mode. Then use the window manager's settings to start your application and enter kiosk mode.
Options for window managers which can do this include KDE or twm-kiosk.
(And if you don't have control of the platform, you're not likely to be able to have your application intercept things like ctrl-alt-backspace anyway.)
Edit:
In response to a scaled-down version of the question in which he's willing to let things like ctl-alt-backspace go and just wants most of the keys including alt-tab or other similar application switching key combinations, the following should work:
You should be able to do this using XLib's XGrabKeyboard method through JNI. This Java/XLib JNI keypress capture tutorial should be a good starting point. However, it uses XGrabKey which just passively listens for keys and does not prevent other applications from receiving them. You'll instead want to use XGrabKeyboard which actively snags all of the normal keyboard events (which, if the premise of this StackOverflow question is correct, includes the task switching keys).
Note that as a side-effect, key capture in Swing will also probably stop working because your Swing windows are going to be separate from the window you create in C. As such, you will probably have to use your JNI interface to get key presses to your program when needed. (Although I would definitely advise testing it first before writing the code.) You might be able to avoid this if you can get the window using Java AWT Native Interface to get the window ID. (Note that Swing is built on top of AWT, so this will work for Swing.) However, I'm not sure how to do this. It looks like you might be able to navigate the window tree by getting the root window from the Display and going from there to find your Window, but it's all kind of weird. It would be nice if the AWT NI just told you the window ID, but it doesn't look like it does that.
As this warning Reminder: XGrabKeyboard is not a security interface notes, this doesn't make it impossible for other programs to see the keys, but it seems likely that window managers will not be using XQueryKeyMap so it is likely to prevent task switching.

- 1
- 1

- 5,628
- 22
- 31
-
The application's installer will have admin-level authentication but that's the extent of my control over the system. Are you saying that I'll need to force the OS to use a specific window manager prior to launching my Java app? Is this a setting I can automatically toggle when the app starts and revert once execution terminates (gracefully or otherwise)? Would I need superuser rights in order to do something like that? I always imagined that Linux video games somehow manage a hostile takeover of arbitrarily configured machines. Perhaps I can leverage whatever library they're using. – Hollis Waite Oct 20 '11 at 04:50
-
I would be quite surprised if very many Linux games trap combinations like ctrl-alt-backspace and similar. They're not trying to lock the user out of the OS, just avoiding accidental switching. That's a much easier problem. – Keith Irwin Oct 20 '11 at 05:07
-
Some Googling turns up different opinions of whether or not this is possible. This thread: http://fixunix.com/xwindows/90830-grab-keyboard-mouse-including-ctrl-alt-f1-ctrl-alt-backspace.html states that VMWare does manage to grab these keyboard inputs when it's trapping keyboard input and suggests that it somehow uses DRI to do this. This faq for xscreensaver: http://www.jwz.org/xscreensaver/faq.html#no-ctl-alt-bs on the other hand, says that this is impossible. It may be the case that it used to be possible, but isn't any more. One thing I can guarantee, though: if possible, it's a pain. – Keith Irwin Oct 20 '11 at 05:12
-
What if I abandon some of the more ambitious reqs? I can live with users being able to kill app or logout. Want to make it as inconvenient as possible to use other apps while mine is active. If foolproof solution is nontrivial, I'd settle for disabling Alt-Tab, Ctrl-Alt-Right/Left and other task-switching shortcuts where possible. Maybe throw up a blank "always on top" window on each of user's workspaces. If push comes to shove, I can also disable the application for non-Gnome window managers. Basically, I'd like to implement all of the countermeasures that qualify as "a much easier problem". – Hollis Waite Oct 20 '11 at 22:32
-
I've updated the answer above to answer the easier version of the problem as best I can. I deleted the comment which was right here before because all of the details from it have been moved into that answer, plus a little bit more. Thanks for the accept, by the way. – Keith Irwin Oct 21 '11 at 04:44