【问题标题】:Remove the possibility of using Alt-F4 and Alt-TAB in Java GUI [duplicate]消除在 Java GUI 中使用 Alt-F4 和 Alt-TAB 的可能性 [重复]
【发布时间】:2011-09-01 22:22:04
【问题描述】:

可能重复:
Java Full Screen Program (Swing) -Tab/ALT F4

我正在运行全屏框架,我希望模拟 Kiosk 环境。为此,我需要“捕捉”所有出现的 Alt-F4Alt-Tab 按下一直在键盘上。这甚至可能吗?我的伪代码:

public void keyPressed(KeyEvent e) {
     //get the keystrokes
     //stop the closing or switching of the window/application  
}

我不确定 keyPressed 和它的关联(keyReleased 和 keyTyped)是否正确,因为根据我的阅读,它们只处理单个键/字符。

【问题讨论】:

  • 如果你可以通过某些软件禁用windows键(很常见),那么禁用alt+f4/tab功能应该没问题
  • 重复项是 Google 搜索“Java prevent alt f4”的第一个结果。在询问之前尝试搜索,因为答案可能已经存在。
  • 上面引用的帖子没有回答我的问题,似乎只是真正涉及使应用程序全屏显示。这就是我问这个问题的原因。

标签: java swing user-interface keyboard


【解决方案1】:

停止 Alt-F4:

yourframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

要停止 Alt-Tab,您可以使某些东西更具侵略性。

public class AltTabStopper implements Runnable
{
     private boolean working = true;
     private JFrame frame;

     public AltTabStopper(JFrame frame)
     {
          this.frame = frame;
     }

     public void stop()
     {
          working = false;
     }

     public static AltTabStopper create(JFrame frame)
     {
         AltTabStopper stopper = new AltTabStopper(frame);
         new Thread(stopper, "Alt-Tab Stopper").start();
         return stopper;
     }

     public void run()
     {
         try
         {
             Robot robot = new Robot();
             while (working)
             {
                  robot.keyRelease(KeyEvent.VK_ALT);
                  robot.keyRelease(KeyEvent.VK_TAB);
                  frame.requestFocus();
                  try { Thread.sleep(10); } catch(Exception) {}
             }
         } catch (Exception e) { e.printStackTrace(); System.exit(-1); }
     }
}

【讨论】:

  • 太棒了!谢谢,这正是我需要的。
  • 这会带走 Alt 键的所有使用
  • @StefanReich:哈哈哈,没想到!有趣的是,在我编写此代码 7 年后,人们仍在使用这段代码。
猜你喜欢
  • 2011-05-26
  • 2015-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多