Java Reference
In-Depth Information
Getting ready
When a key from the keyboard is pressed or a mouse event occurs, the JavaFX application
framework will generate input events information stored in instances of KeyEvent and
MouseEvent respectively. These classes are found in the package javafx.scene.input .
Input events are sent to nodes attached to your scene graph. Therefore, in order to receive
input events, you must have at least one node added to your scene. Review the recipe
Building a JavaFX application for background information on how to do that.
How to do it...
To demonstrate how to handle user input, the abbreviated code given next creates a virtual
terminal that captures user's keyboard input (think of a command shell). The full code listing
for the code can be found in ch02/source-code/src/input/KeyboardMouseEvent.fx .
def w = 500;
def h = 300;
var text = Text {
x: 10, y: 10
fill: Color.LIMEGREEN
font: Font {name: "Courier New" size: 12 }
wrappingWidth: w - 100
};
var rect: Rectangle = Rectangle {
x: 0 y: 0
width: w height: h
onMouseClicked: function (e: MouseEvent): Void {
rect.requestFocus();
if (text.content.length() > 0) {
text.content = text.content.substring(0,
text.content.length() - 1);
}
}
onKeyPressed: function (e: KeyEvent): Void {
if (e.code.equals(KeyCode.VK_BACK_SPACE)
or e.code.equals(KeyCode.VK_DELETE)) {
if(text.content.length() > 0){
text.content = text.content.substring(0,
text.content.length() - 1)
}
}else if (e.code.equals(KeyCode.VK_SPACE)){
text.content = "{text.content} ";
 
Search WWH ::




Custom Search