Over a million developers have joined DZone.

Handling Keyboard Sortcuts in JavaFx

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

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");


Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

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 }}