import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.stage.Stage;
import javafx.scene.input.KeyEvent;

public class KeyTest extends Application {
    private double x = 50, y = 20;
    private Canvas canvas;
    public void start(Stage stage) {
        stage.setTitle("Hello");

        canvas = new Canvas(150, 150);
        GraphicsContext gc = canvas.getGraphicsContext2D();
        paint(gc);
        canvas.setFocusTraversable(true);
        canvas.setOnKeyTyped(e -> keyTyped(e));

        Group root = new Group();
        root.getChildren().addAll(canvas);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }

    private void paint(GraphicsContext gc) {
        gc.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());
        gc.fillText("HELLO WORLD!", x, y);
    }

    private void keyTyped(KeyEvent e) {
        String k = e.getCharacter();
        if (k.equals("u")) {
            y -= 10;
        } else if (k.equals("d")) {
            y += 10;
        }
        paint(canvas.getGraphicsContext2D());
        System.err.printf("key = %s%n", k);
    }

    public static void main(String... args) {
        launch(args);
    }
}
