Java KeyListener: how to perform an action when two keys are pressed?

Please view the following code

import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class KeyCheck extends JFrame
{
    private JButton check;
    private JPanel panel;
    private FlowLayout flow;

    public KeyCheck()
    {
        check = new JButton("Check");
        check.addKeyListener(new KeyWork());

        panel = new JPanel();
        panel.setLayout(new FlowLayout());
        panel.add(check);

        getContentPane().add(panel);

    }

    private class KeyWork extends KeyAdapter
    {
        public void keyPressed(KeyEvent k)
        {
            if(k.getKeyCode()==KeyEvent.VK_CONTROL && KeyEvent.VK_A)
            {
                JOptionPane.showMessageDialog(null, "OK");
            }
        }
    }
    public static void main(String[]args)
    {
        KeyCheck k = new KeyCheck();
        k.setVisible(true);
        k.setSize(200,200);
        k.validate();
        k.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

In this case, I added the key to the button, and I need to display the message “OK” when CTRL + A is pressed together (control key and “A”). But the code above is incorrect. So please help me get a message when both keys are pressed together.

+3
source share
4 answers

You mix key code and modifiers :

 if(k.getKeyCode()==KeyEvent.VK_A 
     && (k.getModifiers & KeyEvent.CTRL_MASK==KeyEvent.CTRL_MASK))

But more generally, it is better to use KeyBindings instead of KeyListener. It will make your life a lot easier and allow you to do such tests.

1.Create an action as follows:

 public class MyAction extends AbstractAction {

     public void actionPerformed(ActionEvent e) {
          JOptionPane.showMessageDialog(null, "OK");
     }
 }

2. :

check.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_A, InputEvent.CTRL_MASK), "doSomething");
check.getActionMap().put("doSomething", new MyAction());

: , , , .

+5

k:

if(k.isControlDown() && k.getKeyCode() == KeyEvent.VK_A) {

, . , JTextField frame - - Ctrl+A , JTextField .

+1

        public void keyPressed(KeyEvent k)
        {
            if(k.getKeyCode() == KeyEvent.VK_A && (k.getModifiers & KeyEvent.CTRL_MASK) != 0)
            {
                JOptionPane.showMessageDialog(null, "OK");
            }
        }
    }
0
source

You can also use key mnemonics or accelerators, which is quite simple: 1. Mnemonics will open the menu 2. Accelerators will press the corresponding menu button

JMenuItem combination = new JMenuItem("Save File");
combination.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        combination.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == find) {
                System.out.println("Ctrl+S pressed");
savefile();//depending on what your saving code is                    
                }

            }
        });
0
source

All Articles