dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / ui_events / keyboard_event_1.lucb
#==============================================================================================
#
# keyboard_event_1 - UIEvents::KeyboardEvent: the key, code and location of a platform key
#
# DESCRIPTION:
# Port of Libraries/LibWeb/UIEvents/KeyboardEvent.cpp lines 18-344: determine_key_code,
# determine_char_code, get_event_named_key, get_event_key_string, is_glyph_modifier_key,
# remove_non_glyph_modifier_keys and get_event_key. The tables are the donor's switches,
# arm for arm. A KeyCode's own value (the "virtual key code from the operating system")
# is the donor's u8 value: the port's enum carries the generator's signed reading of it.
#
# Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: keyCode and charCode ====================================================================
## The donor's u8 value of a KeyCode (`to_underlying(platform_key)`).
pub func ui_events_key_code_value(platform_key: UiEventsKeyCode) -> u32:
return (u32)(((i64)platform_key + 256) % 256)
## Ported from KeyboardEvent.cpp:19 determine_key_code.
## https://www.w3.org/TR/uievents/#determine-keydown-keyup-keyCode
func ui_events_determine_key_code(platform_key: UiEventsKeyCode, code_point: u32) -> u32:
# If input key when pressed without modifiers would insert a numerical character (0-9), return the ASCII code of that numerical character.
if ak.is_ascii_digit(code_point):
return code_point
let shifted_digit: u32 = match platform_key:
.key_exclamation_point => 49
.key_at_sign => 50
.key_hashtag => 51
.key_dollar => 52
.key_percent => 53
.key_circumflex => 54
.key_ampersand => 55
.key_asterisk => 56
.key_left_paren => 57
.key_right_paren => 48
_ => 0
if shifted_digit != 0:
return shifted_digit
# 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 ak.is_ascii_lower_alpha(code_point):
return ak.to_ascii_uppercase(code_point)
# If the key’s function, as determined in an implementation-specific way, corresponds to one of the keys in the §8.3.3 Fixed virtual key codes table, return the corresponding key code.
# https://www.w3.org/TR/uievents/#fixed-virtual-key-codes
let fixed: u32 = match platform_key:
.key_backspace => 8
.key_tab => 9
.key_return => 13
.key_left_shift, .key_right_shift => 16
.key_left_control, .key_right_control => 17
.key_left_alt, .key_right_alt => 18
.key_caps_lock => 20
.key_escape => 27
.key_space => 32
.key_page_up => 33
.key_page_down => 34
.key_end => 35
.key_home => 36
.key_left => 37
.key_up => 38
.key_right => 39
.key_down => 40
_ => 0
if fixed != 0:
return fixed
# https://www.w3.org/TR/uievents/#optionally-fixed-virtual-key-codes
let optionally_fixed: u32 = match platform_key:
.key_semicolon, .key_colon => 186
.key_equal, .key_plus => 187
.key_comma, .key_less_than => 188
.key_minus, .key_underscore => 189
.key_period, .key_greater_than => 190
.key_slash, .key_question_mark => 191
.key_backtick, .key_tilde => 192
.key_left_bracket, .key_left_brace => 219
.key_backslash, .key_pipe => 220
.key_right_bracket, .key_right_brace => 221
.key_apostrophe, .key_double_quote => 222
_ => 0
if optionally_fixed != 0:
return optionally_fixed
# Return the virtual key code from the operating system.
return ui_events_key_code_value(platform_key)
## Ported from KeyboardEvent.cpp:142 determine_char_code.
## https://www.w3.org/TR/uievents/#dom-keyboardevent-charcode
func ui_events_determine_char_code(event_name: ak.FlyString, code_point: u32) -> u32:
# charCode holds a character value, for keypress events which generate character input. The value is the Unicode
# reference number (code point) of that character (e.g. event.charCode = event.key.charCodeAt(0) for printable
# characters). For keydown or keyup events, the value of charCode is 0.
if ak.fly_string_eq_fly_string(&event_name, &ui_events_event_names_keypress):
if web_unicode.code_point_is_printable(code_point):
return code_point
return 0
# mark: key =====================================================================================
## Ported from KeyboardEvent.cpp:156 get_event_named_key.
## 3. Named key Attribute Values, https://www.w3.org/TR/uievents-key/#named-key-attribute-values
func ui_events_get_event_named_key(platform_key: UiEventsKeyCode) -> ak.String?:
# (The donor's comments name the spec's sections: 3.1 Special, 3.2 Modifier, 3.3 Whitespace,
# 3.4 Navigation, 3.5 Editing, 3.6 UI, 3.7 Device and 3.9 General-Purpose Function Keys; its
# FIXMEs: Fn, FnLock, Help, Pause.)
return match platform_key:
.key_invalid => ak.string_from_str("Unidentified")
.key_left_alt, .key_right_alt => ak.string_from_str("Alt")
.key_alt_gr => ak.string_from_str("AltGraph")
.key_caps_lock => ak.string_from_str("CapsLock")
.key_left_control, .key_right_control => ak.string_from_str("Control")
.key_left_super, .key_right_super => ak.string_from_str("Meta")
.key_num_lock => ak.string_from_str("NumLock")
.key_scroll_lock => ak.string_from_str("ScrollLock")
.key_left_shift, .key_right_shift => ak.string_from_str("Shift")
.key_return => ak.string_from_str("Enter")
.key_tab => ak.string_from_str("Tab")
.key_space => ak.string_from_str(" ")
.key_down => ak.string_from_str("ArrowDown")
.key_left => ak.string_from_str("ArrowLeft")
.key_right => ak.string_from_str("ArrowRight")
.key_up => ak.string_from_str("ArrowUp")
.key_end => ak.string_from_str("End")
.key_home => ak.string_from_str("Home")
.key_page_down => ak.string_from_str("PageDown")
.key_page_up => ak.string_from_str("PageUp")
.key_backspace => ak.string_from_str("Backspace")
.key_delete => ak.string_from_str("Delete")
.key_insert => ak.string_from_str("Insert")
.key_menu => ak.string_from_str("ContextMenu")
.key_escape => ak.string_from_str("Escape")
.key_print_screen => ak.string_from_str("PrintScreen")
.key_f1 => ak.string_from_str("F1")
.key_f2 => ak.string_from_str("F2")
.key_f3 => ak.string_from_str("F3")
.key_f4 => ak.string_from_str("F4")
.key_f5 => ak.string_from_str("F5")
.key_f6 => ak.string_from_str("F6")
.key_f7 => ak.string_from_str("F7")
.key_f8 => ak.string_from_str("F8")
.key_f9 => ak.string_from_str("F9")
.key_f10 => ak.string_from_str("F10")
.key_f11 => ak.string_from_str("F11")
.key_f12 => ak.string_from_str("F12")
_ => none
## Ported from KeyboardEvent.cpp:267 get_event_key_string.
## 2.1. Unicode Values, https://www.w3.org/TR/uievents-key/#keys-unicode
func ui_events_get_event_key_string(code_point: u32) -> ak.String?:
# A non-control character is any valid Unicode character except those that are part of the "Other, Control"
# ("Cc") General Category.
let is_non_control_character = not web_unicode.code_point_has_control_general_category(code_point)
# A key string is a string containing a 0 or 1 non-control characters ("base" characters) followed by 0 or more
# combining characters. The string MUST be in Normalized Form C (NFC) as described in [UAX15].
# FIXME: Our key events are currently set up to provide one code point at a time. We will need to handle multi-
# code point events and NFC normalize that string.
if is_non_control_character:
return ak.string_from_code_point(code_point)
return none
## Ported from KeyboardEvent.cpp:286 is_glyph_modifier_key.
## https://www.w3.org/TR/uievents-key/#glyph-modifier-key
func ui_events_is_glyph_modifier_key(platform_key: UiEventsKeyCode) -> bool:
# A glyph modifier key is any of the following modifier keys: Shift, CapsLock or AltGr.
return match platform_key:
.key_left_shift, .key_right_shift, .key_caps_lock, .key_alt_gr => true
_ => false
## Ported from KeyboardEvent.cpp:300 remove_non_glyph_modifier_keys.
func ui_events_remove_non_glyph_modifier_keys(modifiers: u32) -> u32:
return (modifiers & (u32)UiEventsKeyModifier.mod_shift) | (modifiers & (u32)UiEventsKeyModifier.mod_alt)
## Ported from KeyboardEvent.cpp:306 get_event_key.
## 2.2. Selecting key Attribute Values, https://www.w3.org/TR/uievents-key/#selecting-key-attribute-values
func ui_events_get_event_key(platform_key: UiEventsKeyCode, a_code_point: u32, a_modifiers: u32) -> ak.String:
var code_point = a_code_point
var modifiers = a_modifiers
# 1. Let key be a DOMString initially set to "Unidentified".
# NOTE: We return "Unidentified" at the end to avoid needlessly allocating it here.
# 2. If there exists an appropriate named key attribute value for this key event, then
# AD-HOC: Key_Invalid would be interpreted as "Unidentified" here. But we also use Key_Invalid for key presses that
# are not on a standard US keyboard. If such a key would generate a valid key string below, let's allow that
# to happen; otherwise, we will still return "Unidentified" at the end.
if platform_key != UiEventsKeyCode.key_invalid:
if let named_key = ui_events_get_event_named_key(platform_key):
# 1. Set key to that named key attribute value.
return named_key
# 3. Else, if the key event generates a valid key string, then
if let key_string = ui_events_get_event_key_string(code_point):
# 1. Set key to that key string value.
return key_string
# 4. Else, if the key event has any modifier keys other than glyph modifier keys, then
if not ui_events_is_glyph_modifier_key(platform_key):
# 1. Set key to the key string that would have been generated by this event if it had been typed with all
# modifier keys removed except for glyph modifier keys.
code_point = ui_events_key_code_value(platform_key)
modifiers = ui_events_remove_non_glyph_modifier_keys(modifiers)
if ak.is_ascii_upper_alpha(code_point) and (modifiers & (u32)UiEventsKeyModifier.mod_shift) == 0:
code_point = ak.to_ascii_lowercase(code_point)
if let key_string = ui_events_get_event_key_string(code_point):
return key_string
# 5. Return key as the key attribute value for this key event.
return ak.string_from_str("Unidentified")