1

I am developing a project about time of countries. I want the main class to end while constantly showing the time when I press Ctrl+X. I used JFrame. When I press on just one key, the program is terminated. But I can't solve this using two keys. I tried some solutions found here. KeyPressed code and main class are below. This way the program doesn't stop.

Key Code:

 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);
    }

Main Code:

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);
}
George Z.
  • 6,643
  • 4
  • 27
  • 47

3 Answers3

1

Use Key Bindings instead and take a look at KeyListener vs 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);
        });
    }
}
George Z.
  • 6,643
  • 4
  • 27
  • 47
0

I don't know if this can work for you:

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
}

I think this code should be pretty easy to understand

0

With Eminent Emperor Penguin's answer, I solved my problem with these codes like below:

       @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);
        }
    }