如何用两个键盘键终止java程序?

jslywgbw  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(316)

我正在开发一个关于国家时间的项目。当我按下ctrl+x时,我希望主类在不断显示时间的同时结束。我用的是jframe。当我只按一个键时,程序就终止了。但我不能用两把钥匙解决这个问题。我试过这里找到的一些解决办法。按键代码和主类如下。这样程序就不会停止。
密钥代码:

public void keyPressed(KeyEvent e) {
        System.out.println("Key Pressed " + KeyEvent.getKeyText(e.getKeyCode()) );
        if("Right".equals(KeyEvent.getKeyText(e.getKeyCode())))
            System.out.println("Right Button Detected");
        if(e.getKeyCode()==KeyEvent.VK_CONTROL && e.getKeyCode()==KeyEvent.VK_X);
            System.exit(0);
    }

主代码:

public class Main {
public static void main(String[] args) {
    System.out.println("You enter CTRL X to exit the program.");

    MyKeyListener klavye = new MyKeyListener();
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.addKeyListener(klavye);
    frame.setVisible(true);
}
5uzkadbs

5uzkadbs1#

我不知道这是否对你有用:

public void keyPressed(KeyEvent kevt) {
 if(kevt.getKeyChar()=='X') {
  if(kevt.isAltDown())
  //Code if Alt+x pressed
  if(kevt.isControlDown())
  //Code if Ctrl+x pressed
  if(kevt.isShiftDown())
  //Code if Shift+x pressed
  if(kevt.isAltDown()&&kevt.isControlDown()&&(!kevt.isShiftDown()))
  //Code if Alt+Ctrl+x pressed
  if(kevt.isAltDown()&&kevt.isShiftDown()&&(!kevt.isControlDown()))
  //Code if Alt+Shift+x pressed
  if(!(kevt.isAltDown())&&kevt.isControlDown()&&(kevt.isShiftDown()))
  //Code if Shift+Ctrl+x pressed
  if(kevt.isAltDown()&&kevt.isControlDown()&&kevt.isShiftDown())
  //Code if Alt+Ctrl+Shift+x pressed
}

我认为这个代码应该很容易理解

izkcnapc

izkcnapc2#

改用键绑定,看看keylister和keybinding。

public class KeybindingTest {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            JPanel contentPane = new JPanel(new FlowLayout());
            contentPane.add(new JLabel("Press CTRL+X to exit."));

            contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                .put(KeyStroke.getKeyStroke("control X"), "exit");

            contentPane.getActionMap().put("exit", new AbstractAction() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
                }
            });

            frame.setContentPane(contentPane);

            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        });
    }
}
a9wyjsp7

a9wyjsp73#

根据著名帝企鹅的回答,我用以下代码解决了我的问题:

@Override
    public void keyPressed(KeyEvent e) {

        if (e.isControlDown()){
            System.out.println("PRESSED CTRL.");

        }
        if (e.getKeyCode()==KeyEvent.VK_X){
            System.out.println("PRESSED X.");
        }
        if (e.isControlDown() && e.getKeyCode()==KeyEvent.VK_X){
            System.out.println("PRESSED BOTH.");
            System.exit(0);
        }
    }

相关问题