JavaFX KeyEvent returns KeyCode.UNDEFINED

I created a simple JavaFX application that receives input from a user in TextField. I bound the KeyTyped event from SceneBuilder to the controller. My function looks like this:

@FXML private void keyTyped(KeyEvent event) { System.out.println(event.getCode().equals(KeyCode.ENTER)); } 

This function always prints UNDEFINED when entering an input key. Any ideas how to fix this? The other letters I type seem to have the same problem.

+6
source share
1 answer

KeyTyped is a special event. It does not have KeyCode , but instead has character .

See an example for the letter "a":

 KeyEvent [source = TextField[id=null, styleClass=text-input text-field], target = TextField[id=null, styleClass=text-input text-field], eventType = KEY_TYPED, consumed = false, character = a, text = , code = UNDEFINED] 

and javadoc: http://docs.oracle.com/javafx/2/api/javafx/scene/input/KeyEvent.html#getCode ()

key code associated with the key in the pressed key or key issued event. For events with a typed type, the code is always KeyCode.UNDEFINED.

+11
source

All Articles