javafx.scene.input.KeyEvent.char is broken -- it seems to always be 0.
I've worked around this in our tool by using KeyEvent.text instead. This -sort- of works - but the description is sometimes surprising, and it differs between platforms.
For example, if you press '[', then the KeyEvent.text will be
* '[' on a Mac (which I think is right)
* "Open Bracket" on Windows
(There are others - on Mac you will get "-" instead of "Minus" on Windows etc).
Anyway, the big problem here is that char isn't set. In my FX program I cannot use just KeyCodes. For example, I want to hook up the "+" key to zoom in. I can't just look for KeyEvent.code = VK_PLUS, because on most keyboards, there is no plus -key-. On my Mac for example, you press plus by holding Shift and pressing the = key. So I get two key events - VK_SHIFT and VK_EQUALS. For that reason, I want to look at the -character-, which should always be "+", and switch on that.
Here is a test program which demonstrates this. Just run it and press keys. You'll notice the event.code and event.text are right, but char is always just "0".
(I see this problem both on 1.2 and 1.3-dev).
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.Group;
import javafx.scene.input.KeyEvent;
var g : Group;
var h : Text;
Stage {
width: 500 height: 150
scene: Scene {
content: [
g = Group {
content: [
h = Text { x: 10 y: 50 content: "Type Away!" }
]
onKeyPressed: function(e:KeyEvent) {
h.content = "keycode='{e.code}' and text='{e.text}' and "
"char='{e.char}' which has length {e.char.length()} and value={e.char.charAt(0) as Integer}";
}
}
]
}
}
g.requestFocus();
I've worked around this in our tool by using KeyEvent.text instead. This -sort- of works - but the description is sometimes surprising, and it differs between platforms.
For example, if you press '[', then the KeyEvent.text will be
* '[' on a Mac (which I think is right)
* "Open Bracket" on Windows
(There are others - on Mac you will get "-" instead of "Minus" on Windows etc).
Anyway, the big problem here is that char isn't set. In my FX program I cannot use just KeyCodes. For example, I want to hook up the "+" key to zoom in. I can't just look for KeyEvent.code = VK_PLUS, because on most keyboards, there is no plus -key-. On my Mac for example, you press plus by holding Shift and pressing the = key. So I get two key events - VK_SHIFT and VK_EQUALS. For that reason, I want to look at the -character-, which should always be "+", and switch on that.
Here is a test program which demonstrates this. Just run it and press keys. You'll notice the event.code and event.text are right, but char is always just "0".
(I see this problem both on 1.2 and 1.3-dev).
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.text.Text;
import javafx.scene.Group;
import javafx.scene.input.KeyEvent;
var g : Group;
var h : Text;
Stage {
width: 500 height: 150
scene: Scene {
content: [
g = Group {
content: [
h = Text { x: 10 y: 50 content: "Type Away!" }
]
onKeyPressed: function(e:KeyEvent) {
h.content = "keycode='{e.code}' and text='{e.text}' and "
"char='{e.char}' which has length {e.char.length()} and value={e.char.charAt(0) as Integer}";
}
}
]
}
}
g.requestFocus();
- relates to
-
JDK-8106133 Documentation for KeyEvents should specify when "code" and "char" are valid
-
- Closed
-