user1977204 user1977204 - 1 year ago 105
Java Question

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:

public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER && (e.getModifiers() & InputEvent.SHIFT_MASK) != 0){

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

Answer Source

I think your issue is similar to keyEventIssue with keyPressed

You should use keyReleased rather than keyPressed to avoid the enter behaviour.

Please check my answer in the above link and let me know if it doesn't help or if your issue is different

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download