删除在Java GUI中使用Alt-F4和Alt-TAB的可能性

n0p*_*0pe 4 java keyboard user-interface swing

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

我有一个全屏框架运行,我希望模仿一个Kiosk环境.要做到这一点,我需要"抓住"所有出现的Alt- F4并且Alt- Tab始终按下键盘.这甚至可能吗?我的伪代码:

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

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

Mar*_*aux 19

要停止Alt-F4:

yourframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Run Code Online (Sandbox Code Playgroud)

要停止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); }
     }
}
Run Code Online (Sandbox Code Playgroud)

  • @StefanReich:哈哈哈哈,没想过!有趣的是,在我编写此代码7年后,人们仍在使用这段代码。 (2认同)