keyReleases 正在模拟 Linux 中的按键(java Swing GUI)

发布于 2024-11-18 01:14:29 字数 1253 浏览 3 评论 0原文

我正在开发一个信息亭 GUI 应用程序,它要求我阻止用户通过 Alt-Tab 键退出全屏窗口。前段时间我发了一个关于这个的问题,一位会员帮我写了一些代码,在Windows环境下运行得很好。

就是这样:

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;
    }
}

但是,我需要它也能够在 Linux 中工作。我从源代码制作了一个可执行的 jar 并将其引入 Linux。除了不断按下 Alt 和 Tab 键之外,一切正常。我的 GUI 上的按钮不断循环,我能够打开一个终端(我在测试期间在应用程序中设置了一个后门,以防发生类似情况),这不会让我输入任何内容,因为 Tab 列出了当前目录。

谁能告诉我是否有一个适用于 Linux 和 Windows 环境的修复程序。然而,如果我必须选择的话,我会选择Linux。

编辑:我还可以确认 Alt 键被“按下”。这种奇怪的行为是怎么回事?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2024-11-25 01:14:29

忘记使用像这样的 hack 来抓住 Alt+Tab 吧。这是一个糟糕的黑客行为,而且很容易出错。还有很多其他热键组合。

对于 Linux,您有两个选择:

  1. 使用最小窗口管理器或根本不使用窗口管理器。例如,使用 fluxbox 您可以删除所有按键绑定,还可以使您的应用程序默认最大化,等等。您可以清空桌面菜单,这样即使您的应用程序崩溃,用户也无法获得控制权。这是一个干净的解决方案,它真正解决了您的问题,而不是问题的某些部分。除了 Alt+Tab 之外,还有很多方法可以摆弄系统。

  2. 完全抓住输入控件。这就是游戏的作用。例如,libSDL 可以为您完成此操作,并且还有用于该功能的 java 包装器。这也应该按预期工作,除非您使用默认情况下不允许抓取输入控件的窗口管理器(我不知道有任何)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文