Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
983 views
in Technique[技术] by (71.8m points)

javafx - Check if logical colon is typed, not if physical shift+semicolon key combination was pressed

In a JavaFX 11 key event handler, when I check the event's key code, how can I check logically for the colon character, no matter which key combination on whatever keyboard layout means a colon? My application must respond to certain characters being input on the keyboard, where a colon means a certain command.

When I type colon (Shift+semicolon on the U.S. keyboard), the key code is not KeyCode.COLON but rather KeyCode.SEMICOLON with the modifier shift: true. This is the case for both KeyPressed and KeyTyped.

I want my listener to check for the resulting character, not the particular key combination that produced that character. Otherwise, my listener may fail on other keyboard layouts besides U.S.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

KeyTyped events contain the character that was typed.

boolean typedAColon = ":".equals(event.getCharacter())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
...