Handle Keyboard Keys ALT+F4 combination in Java

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

I am a windows user and i don't want to close my jframe when I press ALT+F4,

public void keyPressed(KeyEvent e) {  if (KeyEvent.VK_SPACE, java.awt.event.InputEvent.CTRL_DOWN_MASK){  } } 

get the keystrokes and handle/stop closing or switching of the jframe

so how can i handle this keypressed combination in java, please help....

回答1:

the previous will lock closing if indeed you press the key combination meaning there on even close X will not work. hence the following REVIEW: much better based on fast snail answer:

public class OnKeyAltF4DontClose2 extends JFrame {//implements  WindowListener {  public OnKeyAltF4DontClose2() {     setVisible(true);     setDefaultCloseOperation(3);     setBounds(400,400,400,400);        addKeyListener(new KeyAdapter(){         public void keyPressed(KeyEvent e) {              if(((KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, java.awt.event.InputEvent.ALT_DOWN_MASK)) != null)  &&  e.getKeyCode() == KeyEvent.VK_F4){              e.consume();             }              }      });  } public static void main(String[] args) {     new OnKeyAltF4DontClose2();  } 

}



回答2:

If you don't want to allow window to close when press alt+f4 all you need to do is add key bindings to child element of window .but you need to use key-bind for alt+f4 press event as action listner is not able to do it.i guess because it lost focus when you press that keys. window stay without closing if it listen to press event even for alt+f4.you can add keylistner to child component like shown below.

childComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_DOWN_MASK), "stopclose");  childComponent.getActionMap().put("stopclose", new AbstractAction() {     @Override     public void actionPerformed(ActionEvent e) {         System.out.println("i'm waiting ");     } }); 

and complete example

import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import javax.swing.AbstractAction; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.KeyStroke;  public class NoExit {      public NoExit() {         JFrame f = new JFrame();         f.setLayout(new GridLayout(2, 2));         JLabel lable = new JLabel("i will not exit for your  ALT + F4 command");         lable.setBackground(Color.GRAY);         lable.setOpaque(true);         lable.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F4, InputEvent.ALT_DOWN_MASK), "stopclose");         lable.getActionMap().put("stopclose", new AbstractAction() {             @Override             public void actionPerformed(ActionEvent e) {                 System.out.println("i'm waiting ");             }         });         f.add(lable);         f.setVisible(true);     }     public static void main(String[] args) {         new NoExit();     }  } 

output>>



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!