Remove the possibility of using Alt-F4 and Alt-TAB in Java GUI [duplicate]
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); }
}
}
精彩评论