Remove the ability to use Alt-F4 and Alt-TAB in the Java GUI

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

I have a full-screen frame running and I want to emulate a kiosk environment. To do this, I need to "catch" all occurrences Alt- F4and Alt- Tab, pressed on the keyboard all the time. Is it possible? My pseudo code:

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

I'm not sure that keyPressed and it binds (keyReleased and keyTyped) the correct path, because from what I read, they only process individual keys / characters.

+4
source share
1 answer

To stop 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); }
     }
}
+18

All Articles