aNNgeL0
aNNgeL0

Reputation: 161

Skip keyPressed event

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

Answers (2)

arcy
arcy

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

christopher
christopher

Reputation: 27356

Just stick a return statement underneath.

arg0.consume();
return;

Upvotes: 1

Related Questions