Kensoft PH
  • Download
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
No Result
View All Result
Home Java

How to use the Key Event in JavaFX | 100% Best for beginners

November 28, 2021
in Java
Reading Time: 4 mins read
0
Key Event in Java
2.1k
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

  • Key Event in JavaFX Tutorial
  • Types of Key Event in Java
    • Methods in the KeyEvent class to get the key details
  • How to use the key event in JavaFX
    • Example: KEY_PRESSED in JavaFX KeyEvent
      • Output
    • Example: KEY_RELEASED Event
      • Output
    • Example: KEY_TYPED Event
  • YouTube Video

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.

JavaFX KeyEvent

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.

Key Event in JavaFX

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.

Key Event in Java

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.

YouTube Video

JavaFX KeyEvent Tutorial For Beginners
Watch this video on YouTube.

Tags: JavaFX Key EventKey Event in JavaKey Event in JavaFX
Previous Post

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

Next Post

Window event in JavaFX | 100% Best for beginners

KENSOFT

KENSOFT

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java

Related tutorials

How to use the JavaFX ToolBar
Java

How to use the JavaFX ToolBar | 100% Perfect Tutorial

January 24, 2023
13
JavaFX Context Menu
Java

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

January 22, 2023
10
JavaFX MenuBar
Java

How to use the Menu Bar in JavaFX | 100% Perfect Tutorial

December 3, 2022 - Updated on January 23, 2023
76
Next Post
Window Event in JavaFX

Window event in JavaFX | 100% Best for beginners

Java Jsoup Tutorial

How to use Java Jsoup Tutorial | 100% perfect for beginners

Comments in Java

Comments in Java | 100% Perfect for beginner

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Maximum upload file size: 20 MB. You can upload: image, audio, video, document, text. Drop files here

  • Trending
  • Comments
  • Latest
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

April 11, 2021 - Updated on July 3, 2022
MySQL database using XAMPP

How to connect Java to MySQL database using Xampp server | 100% best for beginners

October 27, 2020 - Updated on January 23, 2023
JavaFX 17

How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

November 15, 2021 - Updated on December 13, 2021
change the stage icon in JavaFX

How to change the stage icon in JavaFX | 100% best for beginners

May 23, 2021 - Updated on September 24, 2022
Failed to automatically set up a JavaFX Platform

Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

send SMS in java

How to send SMS in Java | 100% best example

3DES in Java and AES in Java

How to use AES and 3DES in Java | 100% best for beginners

JavaFX Splash Screen

How to create JavaFX Splash Screen | 100% best for beginners

How to use the JavaFX ToolBar

How to use the JavaFX ToolBar | 100% Perfect Tutorial

January 24, 2023
JavaFX Context Menu

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

January 22, 2023
JavaFX MenuBar

How to use the Menu Bar in JavaFX | 100% Perfect Tutorial

December 3, 2022 - Updated on January 23, 2023
How to use the Slider in JavaFX | 100% Perfect Tutorial

How to use the Slider in JavaFX | 100% Perfect Tutorial

November 27, 2022

Latest Tutorials

How to use the JavaFX ToolBar

How to use the JavaFX ToolBar | 100% Perfect Tutorial

January 24, 2023
JavaFX Context Menu

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

January 22, 2023
JavaFX MenuBar

How to use the Menu Bar in JavaFX | 100% Perfect Tutorial

December 3, 2022 - Updated on January 23, 2023

Popular Tutorials

  • Failed to automatically set up a JavaFX Platform

    Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    0 shares
    Share 0 Tweet 0
  • How to connect Java to MySQL database using Xampp server | 100% best for beginners

    0 shares
    Share 0 Tweet 0
  • How To install JDK 17 and JavaFX 17 on NetBeans IDE | Best

    0 shares
    Share 0 Tweet 0
Facebook Instagram Youtube Github LinkedIn Discord
Kensoft PH

What’s up! Kent is my name. The name KENSOFT is derived from the words Kent and Software. My programming language of choice is Java, which I use to create computer applications. In a company, I created applications and a website.

Website

Privacy Policy

Terms and Condition

Sitemap

Services

Guest Post

Fiverr

Freelancer

Upwork

Categories

Website Status

Check the status

Latest Tutorials

How to use the JavaFX ToolBar

How to use the JavaFX ToolBar | 100% Perfect Tutorial

January 24, 2023
JavaFX Context Menu

How to use the JavaFX ContextMenu | 100% Perfect Tutorial

January 22, 2023
JavaFX MenuBar

How to use the Menu Bar in JavaFX | 100% Perfect Tutorial

December 3, 2022 - Updated on January 23, 2023

© 2023 Made With Love By KENSOFT PH

No Result
View All Result
  • Download
  • Java Quiz
  • Contact
  • About

© 2023 Made With Love By KENSOFT PH

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.