Calculator: Added keyboard input

This commit is contained in:
rhin123 2020-01-17 13:09:07 -06:00 committed by Andreas Kling
parent 3e8b60c618
commit 488c510e02
Notes: sideshowbarker 2024-07-19 09:59:41 +09:00
2 changed files with 43 additions and 1 deletions

View file

@ -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();
}

View file

@ -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<GButton> m_inverse_button;
RefPtr<GButton> m_percent_button;
RefPtr<GButton> m_equals_button;
};