简体   繁体   中英

JavaFX - get KeyEvent for non-ASCII characters

In a JavaFX (actually ScalaFX) application, I would like to allow the user to invoke commands by a sequence of keyboard keys. These commands should work regardless of the current keyboard layout (language). for example, the user may be typing in Thai (ภาษา) and then want to save by pressing 'Escape' and than 'S' (or ':' and then 'w', vim style).

I have found that KeyEvents give undefined key code for non-English (non-ASCII?) characters.

This code:

onKeyPressed = (k: KeyEvent) => {
  println("text: " + k.text + "  " + "code name: " + k.code.name)
}

gives:

text: a  code name: A  
text:   code name: Alt          << Alt + Shift switches from English to Hebrew
text:   code name: Shift  
text: ש  code name: Undefined  

when pressing A, changing language to Hebrew and pressing it again.

How can I overcome this problem? how can I know which key was pressed regardless of language?

similar unanswered question: JavaFX KeyEvent and accented characters

getCharacter() will get you the unicode character behind the KeyEvent :

The unicode character associated with the key typed event. For example, char will have the value "A" for a key typed event generated by pressing SHIFT + 'a'. For key pressed and key released events, char is always CHAR_UNDEFINED.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM