1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:47:35 +00:00

Calculator: Handle keydown events correctly with all keyboard layouts

This changes the keydown_event handler to use codepoints instead of key
codes for comparison if possible. This is so the functionality still
works as intended with keyboard layouts where e.g. typing '+' actually
results in KeyCode::Key_ExclamationPoint rather than KeyCode::Key_Plus.

This also removes the unnecessary call to atoi().
This commit is contained in:
Max Wipfli 2021-05-23 20:59:51 +02:00 committed by Linus Groh
parent b53dac6e88
commit 2675d25453

View file

@ -1,6 +1,7 @@
/* /*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org> * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* Copyright (c) 2021, Glenford Williams <gw_dev@outlook.com> * Copyright (c) 2021, Glenford Williams <gw_dev@outlook.com>
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -154,37 +155,32 @@ void CalculatorWidget::keydown_event(GUI::KeyEvent& event)
if (event.key() == KeyCode::Key_Return) { if (event.key() == KeyCode::Key_Return) {
m_keypad.set_value(m_calculator.finish_operation(m_keypad.value())); m_keypad.set_value(m_calculator.finish_operation(m_keypad.value()));
} else if (event.code_point() >= '0' && event.code_point() <= '9') {
} else if (event.key() >= KeyCode::Key_0 && event.key() <= KeyCode::Key_9) { m_keypad.type_digit(event.code_point() - '0');
m_keypad.type_digit(atoi(event.text().characters())); } else if (event.code_point() == '.') {
} else if (event.key() == KeyCode::Key_Period) {
m_keypad.type_decimal_point(); m_keypad.type_decimal_point();
} else if (event.key() == KeyCode::Key_Escape) { } else if (event.key() == KeyCode::Key_Escape) {
m_keypad.set_value(0.0); m_keypad.set_value(0.0);
m_calculator.clear_operation(); m_calculator.clear_operation();
} else if (event.key() == KeyCode::Key_Backspace) { } else if (event.key() == KeyCode::Key_Backspace) {
m_keypad.type_backspace(); m_keypad.type_backspace();
} else { } else {
Calculator::Operation operation; Calculator::Operation operation;
switch (event.key()) { switch (event.code_point()) {
case KeyCode::Key_Plus: case '+':
operation = Calculator::Operation::Add; operation = Calculator::Operation::Add;
break; break;
case KeyCode::Key_Minus: case '-':
operation = Calculator::Operation::Subtract; operation = Calculator::Operation::Subtract;
break; break;
case KeyCode::Key_Asterisk: case '*':
operation = Calculator::Operation::Multiply; operation = Calculator::Operation::Multiply;
break; break;
case KeyCode::Key_Slash: case '/':
operation = Calculator::Operation::Divide; operation = Calculator::Operation::Divide;
break; break;
case KeyCode::Key_Percent: case '%':
operation = Calculator::Operation::Percent; operation = Calculator::Operation::Percent;
break; break;
default: default: