消除在 Java GUI 中使用 Alt-F4 和 Alt-TAB 的可能性 [英] Remove the possibility of using Alt-F4 and Alt-TAB in Java GUI

查看:18
本文介绍了消除在 Java GUI 中使用 Alt-F4 和 Alt-TAB 的可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
Java 全屏程序 (Swing) -Tab/ALT F4

我有一个全屏框架正在运行,我希望模拟一个 Kiosk 环境.为此,我需要捕获"所有出现的 Alt-F4Alt-Tab键盘.这甚至可能吗?我的伪代码:

I've got a full screen frame running and I wish to emulate a Kiosk environment. To do this, I need to "catch" all occurrences of Alt-F4 and Alt-Tab pressed on the keyboard at all times. Is this even possible? My pseudocode:

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

我不确定 keyPressed 及其关联(keyReleased 和 keyTyped)是否是正确的方法,因为从我读过的内容来看,它们只处理单个键/字符.

I'm not sure if keyPressed and it's associates (keyReleased and keyTyped) are the right way to go because from what I've read, they only handle single keys/chars.

推荐答案

停止 Alt-F4:

yourframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

要停止 Alt-Tab,你可以做一些更激进的事情.

To stop Alt-Tab, you can make something more aggressive.

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

这篇关于消除在 Java GUI 中使用 Alt-F4 和 Alt-TAB 的可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆