From dd35b82678efd99efe04ac88e54b768af027b6dc Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sun, 5 Sep 2021 19:40:00 +0200 Subject: [PATCH] Make the application handle keyboard keys --- .../src/main/java/oysteikt/calc/CalcApp.java | 80 ++++++++++++++++++- .../java/oysteikt/calc/CalcController.java | 2 +- 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/oysteikt-calc/src/main/java/oysteikt/calc/CalcApp.java b/oysteikt-calc/src/main/java/oysteikt/calc/CalcApp.java index 1fc4a92..d67f205 100644 --- a/oysteikt-calc/src/main/java/oysteikt/calc/CalcApp.java +++ b/oysteikt-calc/src/main/java/oysteikt/calc/CalcApp.java @@ -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(); } diff --git a/oysteikt-calc/src/main/java/oysteikt/calc/CalcController.java b/oysteikt-calc/src/main/java/oysteikt/calc/CalcController.java index dd8ad13..96da73d 100644 --- a/oysteikt-calc/src/main/java/oysteikt/calc/CalcController.java +++ b/oysteikt-calc/src/main/java/oysteikt/calc/CalcController.java @@ -78,7 +78,7 @@ public class CalcController { /* Exercise Functions */ /* -------------------------------------------------------------------------- */ - private void appendToOperand(String s) { + protected void appendToOperand(String s) { this.setOperand(this.getOperandString() + s); }