可能重复:
Java全屏程序(Swing)-Tab/ALT F4
我有一个全屏框架运行,我想模仿一个Kiosk环境。要做到这一点,我需要"捕捉"所有出现在键盘上的Alt-F4和Alt-选项卡。这可能吗?我的伪代码:
public void keyPressed(KeyEvent e) {
//get the keystrokes
//stop the closing or switching of the window/application
}
我不确定keyPressed及其关联(keyReleased和keyTyped)是否是正确的方法,因为据我所知,它们只处理单个键/字符。
停止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); }
}
}