From 060807e3da8b32ccd04b3c2b36cbc66773ddd229 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 1 Dec 2023 11:08:12 -0500 Subject: [PATCH] LibWeb: Set correct keyCode for number keys and optional virtual keys The spec states that if an input key would insert a numerical character if it were pressed without a modifier, then the keyCode should be that of the numerical character. For example, the keyCode for a dollar sign should be that of the number 4. Further, we should implement the optional fixed virtual key codes. Otherwise, our implementation would give e.g. the double quote a keyCode value of 38, which is the same as the up arrow key. --- .../LibWeb/UIEvents/KeyboardEvent.cpp | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp index d745d63907..52985ed8af 100644 --- a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp @@ -20,6 +20,31 @@ static unsigned long determine_key_code(KeyCode platform_key, u32 code_point) if (is_ascii_digit(code_point)) return code_point; + switch (platform_key) { + case KeyCode::Key_ExclamationPoint: + return static_cast('1'); + case KeyCode::Key_AtSign: + return static_cast('2'); + case KeyCode::Key_Hashtag: + return static_cast('3'); + case KeyCode::Key_Dollar: + return static_cast('4'); + case KeyCode::Key_Percent: + return static_cast('5'); + case KeyCode::Key_Circumflex: + return static_cast('6'); + case KeyCode::Key_Ampersand: + return static_cast('7'); + case KeyCode::Key_Asterisk: + return static_cast('8'); + case KeyCode::Key_LeftParen: + return static_cast('9'); + case KeyCode::Key_RightParen: + return static_cast('0'); + default: + break; + } + // If input key when pressed without modifiers would insert a lower case character in the a-z alphabetical range, return the ASCII code of the upper case equivalent. if (is_ascii_lower_alpha(code_point)) return to_ascii_uppercase(code_point); @@ -65,6 +90,45 @@ static unsigned long determine_key_code(KeyCode platform_key, u32 code_point) break; } + // https://www.w3.org/TR/uievents/#optionally-fixed-virtual-key-codes + switch (platform_key) { + case KeyCode::Key_Semicolon: + case KeyCode::Key_Colon: + return 186; + case KeyCode::Key_Equal: + case KeyCode::Key_Plus: + return 187; + case KeyCode::Key_Comma: + case KeyCode::Key_LessThan: + return 188; + case KeyCode::Key_Minus: + case KeyCode::Key_Underscore: + return 189; + case KeyCode::Key_Period: + case KeyCode::Key_GreaterThan: + return 190; + case KeyCode::Key_Slash: + case KeyCode::Key_QuestionMark: + return 191; + case KeyCode::Key_Backtick: + case KeyCode::Key_Tilde: + return 192; + case KeyCode::Key_LeftBracket: + case KeyCode::Key_LeftBrace: + return 219; + case KeyCode::Key_Backslash: + case KeyCode::Key_Pipe: + return 220; + case KeyCode::Key_RightBracket: + case KeyCode::Key_RightBrace: + return 221; + case KeyCode::Key_Apostrophe: + case KeyCode::Key_DoubleQuote: + return 222; + default: + break; + } + // Return the virtual key code from the operating system. return platform_key; }