JavaFX KeyCode check arrow key

Description

JavaFX KeyCode check arrow key

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Main extends Application {
   double width = 400;
   double height = 400;
   double cX = width / 2;
   double cY = height / 2;
   Circle c = new Circle(cX, cY, Math.min(width, height) / 10);

   @Override/* w  ww .j  a  v a 2 s .  c  om*/
   public void start(Stage primaryStage) {
      Pane pane = new Pane(c);

      pane.setOnKeyPressed(e -> {
         switch (e.getCode()) {
         case UP:
            moveUp();
            break;
         case DOWN:
            moveDown();
            break;
         case LEFT:
            moveLeft();
            break;
         case RIGHT:
            moveRight();
            break;
         }
      });

      primaryStage.setScene(new Scene(pane, width, height));
      primaryStage.setTitle("Click to see position..");
      primaryStage.show();
      pane.requestFocus();
   }

   private void moveUp() {
      c.setCenterY(cY -= 10);
   }

   private void moveDown() {
      c.setCenterY(cY += 10);

   }

   private void moveLeft() {
      c.setCenterX(cX -= 10);
   }

   private void moveRight() {
      c.setCenterX(cX += 10);
   }

   public static void main(String[] args) {
      Application.launch(args);
   }
}



PreviousNext

Related