mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 11:37:44 +00:00
Calculator: Added keyboard input
This commit is contained in:
parent
3e8b60c618
commit
488c510e02
2 changed files with 43 additions and 1 deletions
|
@ -197,3 +197,44 @@ void CalculatorWidget::update_display()
|
||||||
else
|
else
|
||||||
m_label->set_text("");
|
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();
|
||||||
|
}
|
|
@ -22,6 +22,8 @@ private:
|
||||||
|
|
||||||
void update_display();
|
void update_display();
|
||||||
|
|
||||||
|
virtual void keydown_event(GKeyEvent&) override;
|
||||||
|
|
||||||
Calculator m_calculator;
|
Calculator m_calculator;
|
||||||
Keypad m_keypad;
|
Keypad m_keypad;
|
||||||
|
|
||||||
|
@ -46,5 +48,4 @@ private:
|
||||||
RefPtr<GButton> m_inverse_button;
|
RefPtr<GButton> m_inverse_button;
|
||||||
RefPtr<GButton> m_percent_button;
|
RefPtr<GButton> m_percent_button;
|
||||||
RefPtr<GButton> m_equals_button;
|
RefPtr<GButton> m_equals_button;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue