VOOZH about

URL: https://dzone.com/articles/handling-keyboard-sortcuts

⇱ Handling Keyboard Sortcuts in JavaFx


Related

  1. DZone
  2. Coding
  3. Java
  4. Handling Keyboard Sortcuts in JavaFx

Handling Keyboard Sortcuts in JavaFx

By Jun. 27, 13 · Code Snippet
Likes
Comment
Save
27.1K 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.

Let's be friends: