title: JavaFX Key Pressed event for scene not executed if there is a focused component
body: <p>I have a code to execute some function when some key is pressed:</p>
  
  <pre><code>scene.setOnKeyPressed(event -&gt; {
      if (event.getCode() == KeyCode.F1) {
          doSomething();
      }
  });
  </code></pre>
  
  <p>And it works, but only if there is no focused components, like a Button or a TextField.
  I noticed it works if i press CTRL+F1, or ALT+F1, or SHIFT+F1, but only F1 just works if there is no focused component.
  Is there a way to avoid this?</p>
  
  <p><strong>-----UPDATE-----</strong>
  As @James_D said, i can do it using eventFilter instead of eventHandler:</p>
  
  <pre><code>scene.addEventFilter(KeyEvent.KEY_PRESSED, event -&gt; {
      if (event.getCode().equals(KeyCode.ESCAPE)) {
          try {
              FXMLLoader fxmlLoader = new FXMLLoader(TelaPrincipalController.class.getResource("/br/com/atualy/checkout/layout/telaoperacoescaixa.fxml"));
              Parent parent = fxmlLoader.load();
              Scene scene = new Scene(parent, 600,400);
              Stage stage = new Stage();
              stage.setScene(scene);
              stage.initModality(Modality.APPLICATION_MODAL);
              stage.initOwner(this.stage);
              stage.showAndWait();
              System.out.println("----&gt; THIS IS BEING PRINTED TWICE ! &lt;----");
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  });
  </code></pre>
  
  <p>The line 12 in this code gets printed twice for every ESC key press. Which means that when i press esc, it opens the new window, and when i close it, the window opens one more time.
  Can i solve it?</p>