From 2675d254539ec5ef5f5c0230b29e8ebbc77002ba Mon Sep 17 00:00:00 2001 From: Max Wipfli Date: Sun, 23 May 2021 20:59:51 +0200 Subject: [PATCH] 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(). --- .../Calculator/CalculatorWidget.cpp | 24 ++++++++----------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/Userland/Applications/Calculator/CalculatorWidget.cpp b/Userland/Applications/Calculator/CalculatorWidget.cpp index 4f3bb2d36b..45fab76d61 100644 --- a/Userland/Applications/Calculator/CalculatorWidget.cpp +++ b/Userland/Applications/Calculator/CalculatorWidget.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2019-2020, Sergey Bugaev * Copyright (c) 2021, Glenford Williams + * Copyright (c) 2021, Max Wipfli * * SPDX-License-Identifier: BSD-2-Clause */ @@ -154,37 +155,32 @@ void CalculatorWidget::keydown_event(GUI::KeyEvent& event) 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 if (event.key() == KeyCode::Key_Period) { + } else if (event.code_point() >= '0' && event.code_point() <= '9') { + m_keypad.type_digit(event.code_point() - '0'); + } else if (event.code_point() == '.') { m_keypad.type_decimal_point(); - } else if (event.key() == KeyCode::Key_Escape) { m_keypad.set_value(0.0); m_calculator.clear_operation(); - } else if (event.key() == KeyCode::Key_Backspace) { m_keypad.type_backspace(); - } else { Calculator::Operation operation; - switch (event.key()) { - case KeyCode::Key_Plus: + switch (event.code_point()) { + case '+': operation = Calculator::Operation::Add; break; - case KeyCode::Key_Minus: + case '-': operation = Calculator::Operation::Subtract; break; - case KeyCode::Key_Asterisk: + case '*': operation = Calculator::Operation::Multiply; break; - case KeyCode::Key_Slash: + case '/': operation = Calculator::Operation::Divide; break; - case KeyCode::Key_Percent: + case '%': operation = Calculator::Operation::Percent; break; default: