JavaFX KeyEvent returns KeyCode.UNDEFINED

爷,独闯天下 提交于 2019-12-30 18:27:13

问题


I created a simple JavaFX application that receives input from the user in a TextField. I attached 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 out UNDEFINED when I type the enter key. Any ideas on how to fix this? Other letters I type seem to have the same problem as well.


回答1:


KeyTyped is a special event. It doesn't have KeyCode but has character set instead.

See example for 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()

The key code associated with the key in this key pressed or key released event. For key typed events, code is always KeyCode.UNDEFINED.



来源:https://stackoverflow.com/questions/20723340/javafx-keyevent-returns-keycode-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!