Over a million developers have joined DZone.

Handling Keyboard Sortcuts in JavaFx

DZone's Guide to

Handling Keyboard Sortcuts in JavaFx

· Java Zone
Free Resource

Never build auth again! The Okta Developer Platform makes it simple to implement authentication, authorization, MFA and more in Java applications. Get started with the free API.

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

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


Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}