2

According to the JavaDoc for KeyEvent's getText() method, it returns

A String describing the key code, such as "HOME", "F1" or "A", for key pressed and key released events. For key typed events, text is always the empty string.

However, when I try printing getText(), it works as expected for printable characters, but not for other keys. "a" and "A" are the results for pressing the a and Shift-A keys, but pressing the Home, F1, or arrow keys results in "". Similarly, it registers the pressing of the Shift key before the A key when I press Shift-A, but the getText() for Shift is "".

Strangely enough, pressing Ctrl + some key produces a non-printable control key.

Is this a bug, or at least a mismatch between the docs and the real behavior? I am using ScalaFX, so I suppose there could be a problem in the Scala interface, but I'm actually using the javafx.scene.input.KeyEvent and javafx.event.EventHandler classes, not their Scala wrappers, so I doubt that's the problem.

Todd O'Bryan
  • 2,234
  • 17
  • 30
  • try using the `getCharacter()` instead, it might be reading those as unicode for some strange reason. If that still gives the "", what's the keyCode it's giving back? it might not be picking up the event properly for some reason. – WillBD Apr 03 '14 at 14:18
  • On which key event you have attached the handler? "key pressed", "key released" and "key typed" events will contain different part infos of KeyEvent as stated also in javadoc of the question. – Uluk Biy Apr 03 '14 at 15:03
  • 1
    I'm attaching the handler for `onKeyPressed`, and `getCode()` works fine--it's just that `getText()` doesn't return `"HOME"`, `"F1"`, or any other non-printable characters for that matter. I guess it's a bug. – Todd O'Bryan Apr 03 '14 at 17:23

0 Answers0