Kensoft PH
  • Download
    • KenshotApplication
  • Contact
  • About
Java Quiz
No Result
View All Result
Kensoft PH
  • Download
    • KenshotApplication
  • 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.5k
VIEWS
Share on FacebookShare on TwitterShare via Email

Contents

Toggle
  • 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

YouTube video

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 Pie Chart 100% For Beginners
Java

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
205
How to Connect to an API Using JavaFX
Java

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
215
JavaFX SQLite Database CRUD Tutorial
Java

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
587
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 *

Tools

Multi-platform installer builder

Java profiler

  • Trending
  • Comments
  • Latest
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
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
JavaFX 17

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

November 15, 2021 - Updated on December 13, 2021
hide and show password in jPasswordField

JPasswordField in Java Hide or Show Password | 100% best for beginners

April 2, 2021 - Updated on September 21, 2022
Failed to automatically set up a JavaFX Platform

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

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

set up JavaFX and Scene Builder

How to set up JavaFX and Scene Builder in NetBeans IDE | 100% best for beginners

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024
How to take a screenshot on PC using Kenshot

How to Take a Screenshot on PC Using Kenshot: A Full Guide

January 18, 2024 - Updated on October 6, 2024

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

Popular Tutorials

  • MySQL database using XAMPP

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

    0 shares
    Share 0 Tweet 0
  • Failed to automatically set up a JavaFX Platform SOLVED Apache NetBeans 12.3 | Best way

    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! I'm Kent. 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.

Categories

Website

Check the status

Privacy Policy

Terms and Condition

Sitemap

Latest Tutorials

How to Use the JavaFX Pie Chart 100% For Beginners

How to Use the JavaFX Pie Chart 100% For Beginners

June 12, 2024 - Updated on October 6, 2024
How to Connect to an API Using JavaFX

How to Connect to an API Using JavaFX

May 26, 2024 - Updated on September 28, 2024
JavaFX SQLite Database CRUD Tutorial

JavaFX SQLite Database CRUD Tutorial | Note Application

May 26, 2024 - Updated on September 28, 2024

© 2024 Made With Love By KENSOFT PH

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

© 2024 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.