Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SHIFT + ENTER, & ENTER in the same component in Java

In my program hte SHIFT+ENTER key does a new line in a text area, but when y remove the established behavior of the ENTER key the program crashes, can i do that?, add functionality for SHIFT+ENTER keys and remove the standard behavior of the Enter key. My following code is, for the remove behavior of Enter key:

public void removeEnterBehavior(){
    KeyStroke enter = KeyStroke.getKeyStroke("ENTER");
    InputMap inputMap = messageTextArea.getInputMap();
    inputMap.put(enter, "none");
}

For the new line with the SHIFT+Enter keys, my code is:

@Override
    public void keyPressed(KeyEvent e) {   
         if(e.getKeyCode() == KeyEvent.VK_ENTER && (e.getModifiers() & InputEvent.SHIFT_MASK) != 0){
             addNewLine();
         }
    }

Also the ENTER key have to call a method in the keyPressed method

like image 915
user1977204 Avatar asked Aug 15 '26 11:08

user1977204


1 Answers

Why are you mixing KeyBinding and KeyListener?

Dont use KeyListener/KeyAdapter with Swing. Always use KeyBindings

You should do something like this to listen for SHIFT+ENTER (it can be in key release or key pressed):

jta.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK, true), "Shift+Enter released");
jta.getActionMap().put("Shift+Enter released", new AbstractAction() {
    @Override
    public void actionPerformed(ActionEvent ae) {
        System.out.println("Shift+Enter released");
    }
});

The important part being:

KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK, true)

Notice the last 2 parameters. The true is used so Keybinding is added for on release of the key, though you can use false for when key is pressed and the same results will occur. The other parameter is for when SHIFT is down.

Also to remove ENTER you should do it on the key pressed:

KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
InputMap inputMap = jta.getInputMap();
inputMap.put(enter, "none");

Notice I use false which means the keybinding is placed when key is pressed.

Here is an example:

public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                JTextArea jta = new JTextArea(20, 20);

                //remove enter pressed
                KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false);
                InputMap inputMap = jta.getInputMap();
                inputMap.put(enter, "none");

                //add shift+enter keybinding can be on pressed or released i.e false or true
                jta.getInputMap(JComponent.WHEN_FOCUSED).put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.SHIFT_DOWN_MASK, true), "Shift+Enter released");
                jta.getActionMap().put("Shift+Enter released", new AbstractAction() {
                    @Override
                    public void actionPerformed(ActionEvent ae) {
                        System.out.println("Shift+Enter released");
                    }
                });

                frame.add(jta);

                frame.pack();
                frame.setVisible(true);
            }
        });
    }
like image 79
David Kroukamp Avatar answered Aug 18 '26 00:08

David Kroukamp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!