开发者

Remove the possibility of using Alt-F4 and Alt-TAB in Java GUI [duplicate]

This question already has answers here: Closed 11 year开发者_Go百科s ago.

Possible Duplicate:

Java Full Screen Program (Swing) -Tab/ALT F4

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  
}

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.


To stop Alt-F4:

yourframe.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜