Reputation: 161
How can i skip a keyevent if the typed key is a specific key ( in my case enter) ? When i press enter i want to click on the button and stop there, without continuing the code that comes next in this method. Seems like the "consume()" doesn't work.
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode() == KeyEvent.VK_ENTER && button.isDisplayable()){
button.doClick();
arg0.consume();
}}
Upvotes: 0
Views: 183
Reputation: 13153
Read the documentation on the KeyEvent. KeyEvent.consume()
doesn't prevent the method from continuing to execute, it prevents any other listener method from executing due to the event.
Arrange the logic of your method so that no other code executes if the key pressed was a return key.
Upvotes: 1
Reputation: 27356
Just stick a return
statement underneath.
arg0.consume();
return;
Upvotes: 1