DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Top 7 Mistakes When Testing JavaFX Applications
  • JavaFX Gets Video Capabilities
  • The Challenges of a JavaFX Reboot
  • Calling JavaFX From Java?

Trending

  • Master-Class: Understanding Database Replication (Single, Multi, and Leaderless)
  • The Developer's Guide to Context-Aware AI: When Your Code Documentation Becomes Intelligent
  • LLM-Powered Deep Parsing for Industrial Inventory Search
  • Genkit Middleware: Intercept, Extend, and Harden your Gen AI Pipelines
  1. DZone
  2. Coding
  3. Java
  4. Handling Keyboard Sortcuts in JavaFx

Handling Keyboard Sortcuts in JavaFx

By 
Neil Ghosh user avatar
Neil Ghosh
·
Jun. 27, 13 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
27.0K Views

Join the DZone community and get the full member experience.

Join For Free

A lot of times you need to to assign some functionality to some keyboard shortcut like F5 or Ctrl+R  in your application. JavaFx also provides KeyCodeCombination API for handling multiple key events.

scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
 public void handle(final KeyEvent keyEvent) {
   if (keyEvent.getCode() == KeyCode.F5) {
    System.out.println("F5 pressed");
    //Stop letting it do anything else
    keyEvent.consume();
   }
 }
});


final KeyCombination keyComb1 = new KeyCodeCombination(KeyCode.R,
                                    KeyCombination.CONTROL_DOWN);
scene.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler() {
                @Override
                public void handle(KeyEvent event) {
                    if (keyComb1.match(event)) {
                        System.out.println("Ctrl+R pressed");

                    }
                }
            });
JavaFX

Opinions expressed by DZone contributors are their own.

Related

  • Top 7 Mistakes When Testing JavaFX Applications
  • JavaFX Gets Video Capabilities
  • The Challenges of a JavaFX Reboot
  • Calling JavaFX From Java?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook