title: Set KeyPressed event for a TextField in JavaFX
body: <p>I have number of <code>TextField</code> objects inside a pop up window called dialog (type : <code>Stage</code>).</p>
  
  <p>I am trying to define an action handler for them which aims to close the stage once the escape button is clicked on the key board.</p>
  
  <p>Here is my function for the stage closer :</p>
  
  <pre><code>public void escapeKeyPressed(KeyCode keyCode , Stage dialog){
      if (keyCode == KeyCode.ESCAPE ){
          dialog.close();
          System.out.println("escape got called");
      }
  } 
  </code></pre>
  
  <p>and the following is where I call it : </p>
  
  <pre><code>textUsername.setOnAction((event) -&gt; {escapeKeyPressed(KeyCode.ESCAPE ,dialog );});
  textAddress.setOnAction((event) -&gt; {escapeKeyPressed(KeyCode.ESCAPE ,dialog );});
  textwp.setOnAction((event) -&gt; {escapeKeyPressed(KeyCode.ESCAPE ,dialog );});
  textState.setOnAction((event) -&gt; {escapeKeyPressed(KeyCode.ESCAPE ,dialog );});
  textloginName.setOnAction((event) -&gt; {escapeKeyPressed(KeyCode.ESCAPE ,dialog );});
  </code></pre>
  
  <p>The problem is that the function doesn't get called.</p>
  
  <p>Any idea how can I fix that? It's woth mentioning that the function by itself works fine if I replace the caller out of <code>setOnAction();</code></p>