Skip to content

Instantly share code, notes, and snippets.

@TheItachiUchiha
Created February 18, 2015 12:37
Show Gist options
  • Save TheItachiUchiha/419221a35fa67f54df06 to your computer and use it in GitHub Desktop.
Save TheItachiUchiha/419221a35fa67f54df06 to your computer and use it in GitHub Desktop.
KeyStroke
package delete;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
public class KeyStroke extends Application {
@Override
public void start(Stage primaryStage) {
Pane root = new Pane();
TextField textField = new TextField();
textField.setOnKeyPressed(ke -> {
if (ke.getCode().equals(KeyCode.DIGIT0)) {
textField.setText("0");
}
else if (ke.getCode().equals(KeyCode.DIGIT1)) {
textField.setText("1");
}
else if (ke.getCode().equals(KeyCode.DIGIT2)) {
textField.setText("2");
} else if (ke.getCode().equals(KeyCode.DIGIT3)) {
textField.setText("3");
}
else if (ke.getCode().equals(KeyCode.DIGIT4)) {
textField.setText("4");
}
else if (ke.getCode().equals(KeyCode.DIGIT5)) {
textField.setText("5");
}
else if (ke.getCode().equals(KeyCode.DIGIT6)) {
textField.setText("6");
}
else if (ke.getCode().equals(KeyCode.DIGIT7)) {
textField.setText("7");
} else if (ke.getCode().equals(KeyCode.DIGIT8)) {
textField.setText("8");
}
else if (ke.getCode().equals(KeyCode.DIGIT9)) {
textField.setText("9");
}
});
root.getChildren().add(textField);
Scene scene = new Scene(root, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment