user623990
user623990

Reputation:

keyReleases are simulating keyPresses in Linux (java Swing GUI)

I have a kiosk GUI application I'm working on and it requires me to block users from being able to Alt-Tab out of the fullscreen window. I posted a question about this a while back and a member helped me with some code, which worked perfectly under a Windows environment.

Here it is:

public class TabStopper implements Runnable {

    private boolean isWorking = false;
    private MenuFrame parent;

    public TabStopper(MenuFrame parent) {
        this.parent = parent;
        new Thread(this, "TabStopper").start();
    }

    public void run() {
        this.isWorking = true;
        Robot robot;
        try {
            robot = new Robot();

            while (isWorking) {
                robot.keyRelease(KeyEvent.VK_ALT);
                robot.keyRelease(KeyEvent.VK_TAB);
                parent.requestFocus();
                Thread.sleep(10); 
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        this.isWorking = false; 
    }

    public boolean isWorking() {
        return this.isWorking;
    }
}

However, I need this to be able to work in Linux as well. I made an executable jar from the source and brought it into Linux. Everything worked except the Alt and Tab keys were being constantly pressed. The buttons on my GUI were constantly being cycled and I was able to open a terminal (I set a backdoor in the application during testing in case something like this happens) which wouldn't let me type anything because Tab lists all the files in the current directory.

Could anyone tell me if there would be a fix that would work in both Linux and Windows environments. However, if I had to choose, I would go for Linux.

EDIT: I can also confirm that the Alt key is being "pressed". What's with this weird behaviour?

Upvotes: 4

Views: 591

Answers (1)

ypnos
ypnos

Reputation: 52367

Forget grabbing Alt+Tab with hacks like this. It is a bad hack and it is error-prone. There are also so many other hotkey combinations.

For linux you have two options:

  1. Use a minimal window manager or no window manager at all. For example, with fluxbox you can remove all key bindings alltogether and you can also make your application maximise by default, etc. You can empty the desktop menus such that the user gains no control even when your application crashes. This is a clean solution that really solves your problem instead of some parts of it. There are many ways to fiddle with the system other than Alt+Tab.

  2. Grab input controls completely. This is what games do. For example libSDL does it for you and there are java wrappers for the functionality as well. This should also work as expected, except you use a window manager that does not allow input control grabbing per default (I don't know of any).

Upvotes: 3

Related Questions