title: KeyEvents and Focus JavaFX
body: <p>I am super confused right now. 
  I've created a Pane in the fxml code and set an onKeyPressed event.</p>
  
  <pre><code>pane.setOnKeyPressed(new EventHandler&lt;KeyEvent&gt;() {
     @Override
     public void handle(KeyEvent event) {
         System.out.println( "I'm here");
         jump( event);
     }
  });
  </code></pre>
  
  <p>This didn't work. Pane didn't catch any KeyEvents. Never printed "I'm here". 
  Then I wanted to set the scene's onKeyPressed, which works, but I have to do it in the controller as the things I want to handle during a key press must be done in the Controller.</p>
  
  <p>Then I called <code>pane.getScene().setOnKeyPressed()</code> but that gave a <code>nullPointerException</code>, as I found out that I have to call it outside <code>initialize()</code>. </p>
  
  <p>So I created a Button and attached an onAction event to it, which prints out the Scene of my Pane, to try if it really works. </p>
  
  <pre><code>@FXML
  public void printScene() {
      System.out.println( pane.getScene());
  }
  </code></pre>
  
  <p>When I clicked the button, it printed out the Scene of my Pane as it should have. But then, I realized that my Pane now catches KeyEvents. I have changed nothing except add a button using Fxml and now the Pane catches KeyEvents? When I comment out the code for the button, the Pane doesn't catch KeyEvents anymore</p>
  
  <pre><code>&lt;Button onAction="#printScene"/&gt;
  </code></pre>
  
  <p>This is all the code I have for the Button.</p>
  
  <p>Note: I also tried <code>pane.requestFocus()</code> and <code>pane.SetFocusTraversable( true)</code> which didn't work either. But adding a button suddenly makes it work? I am so confused.</p>
  
  <p>ENTIRE CODE:</p>
  
  <pre><code>public class Main extends Application {
  
  @Override
  public void start(Stage primaryStage) throws Exception{
      FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("sample.fxml"));
      Parent root = fxmlLoader.load();
      Scene scene = new Scene( root, 800, 600);
      primaryStage.setTitle("Game");
      primaryStage.setScene( scene);
      primaryStage.show();
  
  
  }
  
  public static void main(String[] args) {
      launch(args);
  }
  }
  
  public class Controller {
  
  @FXML private Pane pane;
  @FXML private Rectangle cube;
  
  public void initialize() {
      pane.requestFocus();
      pane.setFocusTraversable( true);
      pane.setOnKeyPressed(new EventHandler&lt;KeyEvent&gt;() {
          @Override
          public void handle(KeyEvent event) {
              System.out.println( "I'm here");
              jump( event);
          }
      });
      System.out.println( pane.getScene());
  }
  }
  
  //FXML Code
  &lt;Pane fx:controller="sample.Controller" fx:id="pane"
    xmlns:fx="http://javafx.com/fxml"&gt;
  
  &lt;Rectangle width="800" height="20" layoutY="580" fill="green"/&gt;
  &lt;Rectangle fx:id="cube" width="40" height="40" layoutX="5" layoutY="540"/&gt;
  &lt;!--&lt;Button /&gt;--&gt;
  &lt;/Pane&gt;
  </code></pre>
  
  <p>Uncommenting button makes Pane gain focus, and vice versa.</p>