title: How do I ensure the user is only allowed integer input?
body: <p>I'm new to Java and JavaFX, but I'm trying to build a pretty basic program. Below is a snippet of code I had originally placed in the <code>On Key Pressed</code> event of the <code>TextField</code> (it did of course have a different parameter type). However, the code didn't work there (i.e. the user was still allowed to enter characters), and after some Googling I found that maybe this code should be moved to the <code>On Input Method Text Changed</code> event of the <code>TextField</code>. But, I'm unable to determine how I can recover the key that was pressed and whether or not CTRL is down.</p>
  <pre><code>private void verifyKeyIsInteger(InputMethodEvent event) {
      KeyCode code = event.getCode();
      if (event.isControlDown() &amp;&amp; (code.equals(KeyCode.C) || code.equals(KeyCode.X) || code.equals(KeyCode.V))) {
          return;
      }
      else if (code.isDigitKey()) {
          return;
      }
  
      event.consume();
  }
  </code></pre>
  <p>This code is in my controller that is attached to the <code>FXML</code> file.</p>
  <p><strong>How can I ensure that user's can only input integers into the <code>TextField</code>?</strong></p>
  <h1>EDIT</h1>
  <p>Here is my current <code>FXML</code>:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  
  &lt;?import java.lang.*?&gt;
  &lt;?import java.util.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;?import javafx.scene.paint.*?&gt;
  
  &lt;AnchorPane id=&quot;AnchorPane&quot; maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; prefHeight=&quot;400.0&quot; prefWidth=&quot;600.0&quot; xmlns:fx=&quot;http://javafx.com/fxml&quot; fx:controller=&quot;tutoringcalculator.MainFormController&quot;&gt;
    &lt;children&gt;
      &lt;Label layoutX=&quot;14.0&quot; layoutY=&quot;14.0&quot; text=&quot;Session Minutes:&quot; /&gt;
      &lt;TextField fx:id=&quot;sessionMinutes&quot; layoutX=&quot;100.0&quot; layoutY=&quot;14.0&quot; prefWidth=&quot;200.0&quot; /&gt;
      &lt;Label layoutX=&quot;14.0&quot; layoutY=&quot;34.0&quot; text=&quot;Earnings:&quot; /&gt;
      &lt;TextField fx:id=&quot;earnings&quot; layoutX=&quot;100.0&quot; layoutY=&quot;34.0&quot; prefWidth=&quot;200.0&quot; /&gt;
      &lt;Button fx:id=&quot;quitButton&quot; layoutX=&quot;511.0&quot; layoutY=&quot;14.0&quot; minWidth=&quot;75.0&quot; mnemonicParsing=&quot;false&quot; onMouseClicked=&quot;#quitApplication&quot; text=&quot;Quit&quot; /&gt;
    &lt;/children&gt;
  &lt;/AnchorPane&gt;
  </code></pre>