Make the application handle keyboard keys

This commit is contained in:
Oystein Kristoffer Tveit 2021-09-05 19:40:00 +02:00
parent e0cffd92fa
commit dd35b82678
2 changed files with 79 additions and 3 deletions

View File

@ -12,15 +12,91 @@ import java.io.IOException;
* JavaFX App
*/
public class CalcApp extends Application {
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(this.getClass().getResource("Calc.fxml"));
Parent parent = fxmlLoader.load();
stage.setScene(new Scene(parent));
/* -------------------------------------------------------------------------- */
/* Experimental Stuff */
/* -------------------------------------------------------------------------- */
CalcController controller = fxmlLoader.getController();
Scene scene = new Scene(parent);
this.setKeys(scene, controller);
stage.setScene(scene);
stage.show();
}
private void setKeys(Scene scene, CalcController controller) {
scene.setOnKeyTyped(e -> {
switch (e.getCharacter()) {
case ",":
case ".":
controller.handlePoint();
break;
case "\n":
case "\r":
controller.handleEnter();
break;
case "+":
controller.handleOpAdd();
break;
case "-":
controller.handleOpSub();
break;
case "*":
controller.handleOpMult();
break;
case "/":
case "\\":
controller.handleOpDiv();
break;
case "v": // Looks like a root symbol
case "r": // (R)oot
controller.handleOpRoot();
break;
case "~":
case "s": // (S)wap
controller.handleSwap();
break;
case "p": // (P)i
controller.handleOpPi();
break;
case "c": // (C)lear
controller.handleClear();
break;
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
controller.appendToOperand(e.getCharacter());
break;
default:
break;
}
});
}
public static void main(String[] args) {
launch();
}

View File

@ -78,7 +78,7 @@ public class CalcController {
/* Exercise Functions */
/* -------------------------------------------------------------------------- */
private void appendToOperand(String s) {
protected void appendToOperand(String s) {
this.setOperand(this.getOperandString() + s);
}