Key Event in JavaFX Tutorial
Key Event in Java is a type of input event, If the node has focus, the event will occur. Let say for example you have focus on a text field and when you input something into it, the event will fire. Working with Key Events is simple and easy. This tutorial is ideal for beginners like you so I will show the basics of the Key Events. If you want to learn more about Java programming, go here.
There are three types of JavaFX KeyEvent, the following is the lists of the events in JavaFX showing the type of the event and its description.
Types of Key Event in Java
- KEY_PRESSED – When a key on the keyboard is pressed, this event will be triggered.
- KEY_RELEASED – When the pressed key on the keyboard is released, this event will be executed.
- KEY_TYPED – This event will be triggered when a Unicode character is entered
There are two kinds of events in JavaFX, The lower-level and higher-level event. The lower-level event are KEY_PRESSED AND KEY_RELEASED since they will occur with a key press and key release. The KEY_TYPED event is the higher-level event, it occurs when a Unicode character is typed. Typically, a key press generates a KEY_TYPED event. However, a key release may also generate a KEY_TYPED event.
A KEY_TYPED event can also be triggered by a series of key press and releases. For example, the upper case K may be generated by hitting shift + K, which comprises two key presses, shift and K. In this example, two key presses lead in one KEY TYPED event.
The Key event class has three (3) variables that describe the keys connected with the event, including the code, text, and character. The getter methods in JavaFX can be used to access these three (3) variables. Please continue to the lists below.
Methods in the KeyEvent class to get the key details
- KeyCode getCode() – This method returns the key information or the KeyCode enum constant linked with the pressed or released key.
- String getText() – This method returns a String description of the KeyCode linked with the KEY_PRESSED and KEY_RELEASED events.
- String getCharacter() – This method returns a string representing a character or a sequence of characters connected with the KEY_TYPED event.
How to use the key event in JavaFX
Key event in Java are easy to learn and understand, and if you are a beginner, learning the JavaFX KeyEvent is essential since it is part of your progress in mastering the Java programming language. In this tutorial, I’ll show you few examples. Please keep reading to see the examples in this tutorial.
Example: KEY_PRESSED in JavaFX KeyEvent
In this KEY PRESSED event example, I will show the event type as well as the KeyCode.
@FXML private void txtFieldKeyPressed(KeyEvent event) { String type = event.getEventType().getName(); KeyCode keyCode = event.getCode(); System.out.println("Type: " + type + " Code: " + keyCode); }
Output
Type: KEY_PRESSED Code: A
Example: KEY_RELEASED Event
In this KEY_RELEASED event example, I will show you how to display a message when you hit the ENTER key on your keyboard. In this case, I will use the KEY_RELEASED event as an example. However, it may also be used on the KEY_PRESSED event.
@FXML private void txtFieldKeyReleased(KeyEvent event) { if (event.getCode() == event.getCode().ENTER) { System.out.println("Hello"); System.out.println("Code: " + event.getCode()); } }
Output
Hello Code: ENTER
Example: KEY_TYPED Event
In this KEY TYPED event example, I will show how to type just characters, excluding numbers and other special characters.
@FXML private void txtFieldKeyTyped(KeyEvent event) { String str = event.getCharacter(); int length = str.length(); for (int i = 0; i < length; i++) { Character c = str.charAt(i); if (!Character.isLetter(c)) { event.consume(); } } }
I hope you learned something new in this tutorial, if you want to learn the Key Event in Java via YouTube Video, Please proceed below.