From 488c510e02a02769390b9d97833ad5c5a99f4555 Mon Sep 17 00:00:00 2001 From: rhin123 Date: Fri, 17 Jan 2020 13:09:07 -0600 Subject: [PATCH] Calculator: Added keyboard input --- Applications/Calculator/CalculatorWidget.cpp | 41 ++++++++++++++++++++ Applications/Calculator/CalculatorWidget.h | 3 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Applications/Calculator/CalculatorWidget.cpp b/Applications/Calculator/CalculatorWidget.cpp index ca2555e9cb..103f3d0b21 100644 --- a/Applications/Calculator/CalculatorWidget.cpp +++ b/Applications/Calculator/CalculatorWidget.cpp @@ -197,3 +197,44 @@ void CalculatorWidget::update_display() else m_label->set_text(""); } + +void CalculatorWidget::keydown_event(GKeyEvent& event) +{ + //Clear button selection when we are typing + m_equals_button->set_focus(true); + m_equals_button->set_focus(false); + + if (event.key() == KeyCode::Key_Return) { + m_keypad.set_value(m_calculator.finish_operation(m_keypad.value())); + + } else if (event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9) { + m_keypad.type_digit(atoi(event.text().characters())); + + } else { + Calculator::Operation operation; + + switch (event.key()) { + case KeyCode::Key_Plus: + operation = Calculator::Operation::Add; + break; + case KeyCode::Key_Minus: + operation = Calculator::Operation::Subtract; + break; + case KeyCode::Key_Asterisk: + operation = Calculator::Operation::Multiply; + break; + case KeyCode::Key_Slash: + operation = Calculator::Operation::Divide; + break; + case KeyCode::Key_Percent: + operation = Calculator::Operation::Percent; + break; + default: + return; + } + + m_keypad.set_value(m_calculator.begin_operation(operation, m_keypad.value())); + } + + update_display(); +} \ No newline at end of file diff --git a/Applications/Calculator/CalculatorWidget.h b/Applications/Calculator/CalculatorWidget.h index a01b3c4cc2..80f0b29172 100644 --- a/Applications/Calculator/CalculatorWidget.h +++ b/Applications/Calculator/CalculatorWidget.h @@ -22,6 +22,8 @@ private: void update_display(); + virtual void keydown_event(GKeyEvent&) override; + Calculator m_calculator; Keypad m_keypad; @@ -46,5 +48,4 @@ private: RefPtr m_inverse_button; RefPtr m_percent_button; RefPtr m_equals_button; - };