KeyCode in the javafx.scene.robot.Robot interface
Martin Fox
martin at martinfox.com
Fri May 17 14:47:33 UTC 2024
Mark,
> I iterate over each key in that set, sending that keycode to a text
> field. I then do the same for each keycode but with the shift key held.
> I don't doubt that it could cause havoc in some setups!
On a German or Spanish layout on the Mac BACK_QUOTE will invoke a dead key and throw everything off. Dead keys are miscoded on the Mac but since they aren’t used while typing there hasn’t been much motivation to fix that. If sending a KeyCode generates an InputMethodEvent instead of a KeyEvent you hit a dead key. Send a SPACE to clear the dead key state. That will generate another InputMethodEvent and insert a character into the text control.
> In my case, I'm almost always testing whether some UI elements become
> enabled or disabled based on validation occurring on a text field of
> some kind. A recent case was checking to see if a text field correctly
> rejected input that didn't appear to be an email address. Typing '@'
> into the field was difficult to make reliable. :)
For this sort of testing you can build KeyEvents and fire them yourself. That would allow you to bypass a bunch of fraught areas of the Robot like incorrect focus or accidentally invoking input methods. The internal KeyEventFirer object in JavaFX takes this approach. It uses several internal API’s but illustrate the basics, like sending PRESSED/TYPED/RELEASED trios for KeyCodes that generate characters. I would not mix this approach with a Robot since you would lose control of the ordering of events.
Martin
More information about the openjfx-dev
mailing list