title: setOnKeyPressed event not working properly
body: <p>I am in need to create a generated tunnel-like thing with curves for my project in the university. I found some code in the Internet that allowed me to drag the controlpoint (attached to Anchor object) of the curve. </p>
  
  <p>However, I wanted to make it possible to change the controlpoint Ylocation by pressing arrow keys ("up" and "down") and somehow it's not working at all. I have been trying to hopelessly solve this issue myself, but I can' t find the way out.</p>
  
  <pre><code>import javafx.application.Application;
  import javafx.beans.property.DoubleProperty;
  import javafx.event.EventHandler;
  import javafx.scene.*;
  import javafx.scene.input.KeyCode;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.layout.BorderPane;
  import javafx.scene.paint.Color;
  import javafx.scene.shape.*;
  import javafx.stage.Stage;
  
  /** Example of how a cubic curve works, drag the anchors around to change the curve. */
  public class testsdfasdf extends Application {
    public static void main(String[] args) throws Exception { launch(args); }
  
  
    @Override public void start(Stage primaryStage) throws Exception {
      QuadCurve curve = createStartingCurve();
  
  
      final BorderPane root = new BorderPane();
      Scene scene = new Scene(root, 1000, 1000);
  
      final Anchor control2 = new Anchor(Color.GOLDENROD, curve.controlXProperty(), curve.controlYProperty());
  
  
      root.getChildren().add(curve);
      root.getChildren().add(control2);
  
  
      primaryStage.setTitle("Quadcurve Manipulation");
      primaryStage.setScene(scene);
      primaryStage.show();
    }
  
    private QuadCurve createStartingCurve() {
      QuadCurve curve = new QuadCurve();
      curve.setStartX(100);
      curve.setStartY(100);
      curve.setControlX(200);
      curve.setControlY(50);
      curve.setEndX(300);
      curve.setEndY(100);
      curve.setStroke(Color.BLACK);
      curve.setStrokeWidth(10);
      curve.setStrokeLineCap(StrokeLineCap.ROUND);
      curve.setFill(Color.CORNSILK.deriveColor(0, 1.2, 1, 0.6));
      return curve;
    }
  
    // a draggable anchor displayed around a point.
    class Anchor extends Circle { 
  
        Anchor(Color color, DoubleProperty x, DoubleProperty y) {
        super(x.get(), y.get(), 10);
        setFill(color.deriveColor(1, 1, 1, 0.5));
  
        y.bind(centerYProperty());
        enableDrag();
        changeCurve();
      }
  
  
      private void changeCurve() {
          setOnKeyPressed((event) -&gt; { 
                  System.out.println(event.getCode() == KeyCode.DOWN);
                  if (event.getCode() == KeyCode.DOWN) {
                      double newY = getCenterY();
                      while (newY &gt; 0 &amp;&amp; newY &lt; getScene().getHeight()) {
                          setCenterY(newY);
                          newY+=1;
                      }
                  }
                  else if (event.getCode() == KeyCode.UP) {
                      double newY = getCenterY();
                      while (newY &gt; 0 &amp;&amp; newY &lt; getScene().getHeight()) {
                          setCenterY(newY);
                          newY-=1;
                      }
                  }
          });
          setOnKeyReleased((event) -&gt; {
                  if (event.getCode() == KeyCode.DOWN) {
                  }
                  if (event.getCode() == KeyCode.UP) {
                  }
  
              });
      }
  
      // make a node movable by dragging it around with the mouse.
      private void enableDrag() {
        setOnMousePressed(new EventHandler&lt;MouseEvent&gt;() {
          @Override public void handle(MouseEvent mouseEvent) {
            // record a delta distance for the drag and drop operation.
            getScene().setCursor(Cursor.MOVE);
          }
        });
        setOnMouseReleased(new EventHandler&lt;MouseEvent&gt;() {
          @Override public void handle(MouseEvent mouseEvent) {
            getScene().setCursor(Cursor.HAND);
          }
        });
        setOnMouseDragged(new EventHandler&lt;MouseEvent&gt;() {
          @Override public void handle(MouseEvent mouseEvent) {
            double newY = mouseEvent.getY();
            System.out.println(newY);
            if (newY &gt; 0 &amp;&amp; newY &lt; getScene().getHeight()) {
              setCenterY(newY);
            }  
          }
        });
        setOnMouseEntered(new EventHandler&lt;MouseEvent&gt;() {
          @Override public void handle(MouseEvent mouseEvent) {
            if (!mouseEvent.isPrimaryButtonDown()) {
              getScene().setCursor(Cursor.HAND);
            }
          }
        });
        setOnMouseExited(new EventHandler&lt;MouseEvent&gt;() {
          @Override public void handle(MouseEvent mouseEvent) {
            if (!mouseEvent.isPrimaryButtonDown()) {
              getScene().setCursor(Cursor.DEFAULT);
            }
          }
        });
      }
    }  
  }
  </code></pre>
  
  <p>Maybe there' s too much code here. But that is all to actually draw the curve there and make it draggable with <strong>enableDrag()</strong> function.</p>
  
  <p><strong>changeCurve()</strong> function is not working and that' s the problem.</p>