diff --git a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp index a97ac2c8ac..c5fe7bc438 100644 --- a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp +++ b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp @@ -484,6 +484,25 @@ static ErrorOr get_event_code(KeyCode platform_key, unsigned modifiers) VERIFY_NOT_REACHED(); } +// 5.6.2. Keyboard Event Key Location, https://www.w3.org/TR/uievents/#events-keyboard-key-location +static DOMKeyLocation get_event_location(KeyCode platform_key, unsigned modifiers) +{ + if ((modifiers & Mod_Keypad) != 0) + return DOMKeyLocation::Numpad; + + // FIXME: Detect left vs. right for Control and Alt keys. + switch (platform_key) { + case KeyCode::Key_LeftShift: + return DOMKeyLocation::Left; + case KeyCode::Key_RightShift: + return DOMKeyLocation::Right; + default: + break; + } + + return DOMKeyLocation::Standard; +} + WebIDL::ExceptionOr> KeyboardEvent::create_from_platform_event(JS::Realm& realm, FlyString const& event_name, KeyCode platform_key, unsigned modifiers, u32 code_point) { auto& vm = realm.vm(); @@ -495,7 +514,7 @@ WebIDL::ExceptionOr> KeyboardEvent::create_from_ KeyboardEventInit event_init {}; event_init.key = move(event_key); event_init.code = move(event_code); - event_init.location = 0; + event_init.location = to_underlying(get_event_location(platform_key, modifiers)); event_init.ctrl_key = modifiers & Mod_Ctrl; event_init.shift_key = modifiers & Mod_Shift; event_init.alt_key = modifiers & Mod_Alt; diff --git a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h index a6f0435e69..787509aac5 100644 --- a/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h +++ b/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h @@ -24,6 +24,13 @@ struct KeyboardEventInit : public EventModifierInit { u32 char_code { 0 }; }; +enum class DOMKeyLocation { + Standard = 0, + Left = 1, + Right = 2, + Numpad = 3, +}; + // https://www.w3.org/TR/uievents/#interface-keyboardevent class KeyboardEvent final : public UIEvent { WEB_PLATFORM_OBJECT(KeyboardEvent, UIEvent);