我有一个全屏框架正在运行,我希望模仿Kiosk环境。要做到这一点,我需要“捕获”所有出现的 Alt - F4 和 Alt - Tab 按下键盘在任何时候。这甚至可能吗?我的伪代码:
public void keyPressed(KeyEvent e) {
//get the keystrokes
//stop the closing or switching of the window/application
}
我不确定keyPressed及其关联(keyReleased和keyTyped)是否正确,因为根据我的阅读,他们只处理单个键/字符。
答案 0 :(得分:19)
停止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); }
}
}