Java Reference
In-Depth Information
}else if (e.code.equals(KeyCode.VK_ENTER)){
text.content = "{text.content}\n";
}else{
text.content = "{text.content}{e.text}";
}
}
};
rect.requestFocus();
When the application runs, you get a window that displays your text as you type, as shown in
the next figure:
How it works...
The code in this recipe shows you how to set up a visual node in the scene graph to receive
input events from the keyboard and the mouse. In the application, a black-filled rectangle
receives both keyboard and mouse input events, which works as follows:
F The Text object—as the rectangle instance receives keyboard input events,
the value of the character pressed is used to update the content of text.
F The Rectangle —as mentioned, the Rectangle is the node set up to receive input
events. Calling the Rectangle's requestFocus() method causes it to receive
keyboard events. For our example, we are using two event handling properties:
onKeyPressed:function(:KeyEvent) —the function assigned
to this property is fired every time a key on the keyboard is pressed
and receives an instance of class KeyEvent as an argument.
KeyEvent contains all of the information captured about the
key pressed. In this example, the function is used to process the
pressed key and update the content of the text variable.
onMousePressed:function(:MouseEvent) —this function
is invoked whenever the mouse button is pressed and receives an
instance of MouseEvent as an argument. In this example, when
the user clicks on the rectangle, it calls requestFocus() on
the rectangle and allows it to receive keyboard events and update
the text instance.
 
Search WWH ::




Custom Search