I want add a KeyEventListener to JButton which responds to Enter key, using following code segment:
private void jButton3KeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == 10) {
eventRegister();
}
}
I pressed space bar instead of enter and the if condition set to true and the eventRegister invoked. Why? How could I prevent this manner?
don't to use KeyListener or MouseListener for JButton or JButtons JComponent, those events are implemented in API, or ButtonsModel, every can be testable, with to consume() of KeyEvent
JButton has implemented ENTER and SPACE key as accelator in KeyBindings
remove SPACE from KeyBindings, but not suggest that, I'm wouldn'd be confuse users, sure depends of
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With