mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 07:27:45 +00:00
LibWeb: Add a basic KeyboardEvent and fire "keydown" events :^)
This commit is contained in:
parent
09153b8997
commit
554c344ffe
9 changed files with 221 additions and 4 deletions
|
@ -9,6 +9,7 @@
|
|||
#include <LibWeb/Bindings/CustomEventWrapper.h>
|
||||
#include <LibWeb/Bindings/EventWrapper.h>
|
||||
#include <LibWeb/Bindings/EventWrapperFactory.h>
|
||||
#include <LibWeb/Bindings/KeyboardEventWrapper.h>
|
||||
#include <LibWeb/Bindings/MessageEventWrapper.h>
|
||||
#include <LibWeb/Bindings/MouseEventWrapper.h>
|
||||
#include <LibWeb/Bindings/PageTransitionEventWrapper.h>
|
||||
|
@ -29,6 +30,8 @@ EventWrapper* wrap(JS::GlobalObject& global_object, DOM::Event& event)
|
|||
return static_cast<PageTransitionEventWrapper*>(wrap_impl(global_object, static_cast<HTML::PageTransitionEvent&>(event)));
|
||||
if (is<HTML::SubmitEvent>(event))
|
||||
return static_cast<SubmitEventWrapper*>(wrap_impl(global_object, static_cast<HTML::SubmitEvent&>(event)));
|
||||
if (is<UIEvents::KeyboardEvent>(event))
|
||||
return static_cast<KeyboardEventWrapper*>(wrap_impl(global_object, static_cast<UIEvents::KeyboardEvent&>(event)));
|
||||
if (is<UIEvents::MouseEvent>(event))
|
||||
return static_cast<MouseEventWrapper*>(wrap_impl(global_object, static_cast<UIEvents::MouseEvent&>(event)));
|
||||
if (is<XHR::ProgressEvent>(event))
|
||||
|
|
|
@ -240,6 +240,7 @@ set(SOURCES
|
|||
SVG/ViewBox.cpp
|
||||
StylePropertiesModel.cpp
|
||||
UIEvents/EventNames.cpp
|
||||
UIEvents/KeyboardEvent.cpp
|
||||
UIEvents/MouseEvent.cpp
|
||||
URL/URL.cpp
|
||||
URL/URLSearchParams.cpp
|
||||
|
@ -453,6 +454,7 @@ libweb_js_wrapper(SVG/SVGGeometryElement)
|
|||
libweb_js_wrapper(SVG/SVGGraphicsElement)
|
||||
libweb_js_wrapper(SVG/SVGPathElement)
|
||||
libweb_js_wrapper(SVG/SVGSVGElement)
|
||||
libweb_js_wrapper(UIEvents/KeyboardEvent)
|
||||
libweb_js_wrapper(UIEvents/MouseEvent)
|
||||
libweb_js_wrapper(UIEvents/UIEvent)
|
||||
libweb_js_wrapper(XHR/ProgressEvent)
|
||||
|
|
|
@ -75,7 +75,6 @@ class EventHandler;
|
|||
class EventListener;
|
||||
class EventTarget;
|
||||
class HTMLCollection;
|
||||
class MouseEvent;
|
||||
class Node;
|
||||
class ParentNode;
|
||||
class Position;
|
||||
|
@ -248,6 +247,12 @@ class XMLHttpRequest;
|
|||
class XMLHttpRequestEventTarget;
|
||||
}
|
||||
|
||||
namespace Web::UIEvents {
|
||||
class MouseEvent;
|
||||
class KeyboardEvent;
|
||||
class UIEvents;
|
||||
}
|
||||
|
||||
namespace Web::URL {
|
||||
class URL;
|
||||
class URLSearchParams;
|
||||
|
@ -351,6 +356,7 @@ class HTMLUnknownElementWrapper;
|
|||
class HTMLVideoElementWrapper;
|
||||
class IdleDeadlineWrapper;
|
||||
class ImageDataWrapper;
|
||||
class KeyboardEventWrapper;
|
||||
class LocationObject;
|
||||
class MediaQueryListWrapper;
|
||||
class MessageChannelWrapper;
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
*/
|
||||
|
||||
#include <LibGUI/Event.h>
|
||||
#include <LibGUI/Window.h>
|
||||
#include <LibWeb/DOM/Range.h>
|
||||
#include <LibWeb/DOM/Text.h>
|
||||
#include <LibWeb/DOM/Window.h>
|
||||
#include <LibWeb/HTML/HTMLAnchorElement.h>
|
||||
#include <LibWeb/HTML/HTMLIFrameElement.h>
|
||||
#include <LibWeb/HTML/HTMLImageElement.h>
|
||||
|
@ -17,6 +17,7 @@
|
|||
#include <LibWeb/Page/EventHandler.h>
|
||||
#include <LibWeb/Page/Page.h>
|
||||
#include <LibWeb/UIEvents/EventNames.h>
|
||||
#include <LibWeb/UIEvents/KeyboardEvent.h>
|
||||
#include <LibWeb/UIEvents/MouseEvent.h>
|
||||
|
||||
namespace Web {
|
||||
|
@ -454,7 +455,9 @@ bool EventHandler::handle_keydown(KeyCode key, unsigned modifiers, u32 code_poin
|
|||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
auto event = UIEvents::KeyboardEvent::create_from_platform_event(UIEvents::EventNames::keydown, key, modifiers, code_point);
|
||||
// FIXME: Figure out the right event target.
|
||||
return m_frame.active_document()->window().dispatch_event(move(event));
|
||||
}
|
||||
|
||||
void EventHandler::set_mouse_event_tracking_layout_node(Layout::Node* layout_node)
|
||||
|
|
|
@ -15,6 +15,7 @@ namespace Web::UIEvents::EventNames {
|
|||
|
||||
#define ENUMERATE_UI_EVENTS \
|
||||
__ENUMERATE_UI_EVENT(click) \
|
||||
__ENUMERATE_UI_EVENT(keydown) \
|
||||
__ENUMERATE_UI_EVENT(mousedown) \
|
||||
__ENUMERATE_UI_EVENT(mouseenter) \
|
||||
__ENUMERATE_UI_EVENT(mouseleave) \
|
||||
|
|
110
Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp
Normal file
110
Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.cpp
Normal file
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/CharacterTypes.h>
|
||||
#include <LibWeb/UIEvents/KeyboardEvent.h>
|
||||
|
||||
namespace Web::UIEvents {
|
||||
|
||||
// https://www.w3.org/TR/uievents/#determine-keydown-keyup-keyCode
|
||||
static unsigned long determine_key_code(KeyCode platform_key, u32 code_point)
|
||||
{
|
||||
// If input key when pressed without modifiers would insert a numerical character (0-9), return the ASCII code of that numerical character.
|
||||
if (is_ascii_digit(code_point))
|
||||
return code_point;
|
||||
|
||||
// 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);
|
||||
|
||||
// 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
|
||||
switch (platform_key) {
|
||||
case KeyCode::Key_Backspace:
|
||||
return 8;
|
||||
case KeyCode::Key_Tab:
|
||||
return 9;
|
||||
case KeyCode::Key_Return:
|
||||
return 13;
|
||||
case KeyCode::Key_Shift:
|
||||
return 16;
|
||||
case KeyCode::Key_Control:
|
||||
return 17;
|
||||
case KeyCode::Key_Alt:
|
||||
return 18;
|
||||
case KeyCode::Key_CapsLock:
|
||||
return 20;
|
||||
case KeyCode::Key_Escape:
|
||||
return 27;
|
||||
case KeyCode::Key_Space:
|
||||
return 32;
|
||||
case KeyCode::Key_PageUp:
|
||||
return 33;
|
||||
case KeyCode::Key_PageDown:
|
||||
return 34;
|
||||
case KeyCode::Key_End:
|
||||
return 35;
|
||||
case KeyCode::Key_Home:
|
||||
return 36;
|
||||
case KeyCode::Key_Left:
|
||||
return 37;
|
||||
case KeyCode::Key_Up:
|
||||
return 38;
|
||||
case KeyCode::Key_Right:
|
||||
return 39;
|
||||
case KeyCode::Key_Down:
|
||||
return 40;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Return the virtual key code from the operating system.
|
||||
return platform_key;
|
||||
}
|
||||
|
||||
NonnullRefPtr<KeyboardEvent> KeyboardEvent::create_from_platform_event(FlyString event_name, KeyCode platform_key, unsigned modifiers, u32 code_point)
|
||||
{
|
||||
// FIXME: Figure out what these should actually contain.
|
||||
String event_key = key_code_to_string(platform_key);
|
||||
String event_code = "FIXME";
|
||||
|
||||
auto key_code = determine_key_code(platform_key, code_point);
|
||||
return KeyboardEvent::create(move(event_name), move(event_key), move(event_code), 0, modifiers & Mod_Ctrl, modifiers & Mod_Shift, modifiers & Mod_Alt, false, false, false, key_code, code_point);
|
||||
}
|
||||
|
||||
KeyboardEvent::KeyboardEvent(FlyString event_name, String key, String code, unsigned long location, bool ctrl_key, bool shift_key, bool alt_key, bool meta_key, bool repeat, bool is_composing, unsigned long key_code, unsigned long char_code)
|
||||
: UIEvent(move(event_name))
|
||||
, m_key(move(key))
|
||||
, m_code(move(code))
|
||||
, m_location(location)
|
||||
, m_ctrl_key(ctrl_key)
|
||||
, m_shift_key(shift_key)
|
||||
, m_alt_key(alt_key)
|
||||
, m_meta_key(meta_key)
|
||||
, m_repeat(repeat)
|
||||
, m_is_composing(is_composing)
|
||||
, m_key_code(key_code)
|
||||
, m_char_code(char_code)
|
||||
{
|
||||
}
|
||||
|
||||
KeyboardEvent::~KeyboardEvent()
|
||||
{
|
||||
}
|
||||
|
||||
bool KeyboardEvent::get_modifier_state(String const& key_arg)
|
||||
{
|
||||
if (key_arg == "Alt")
|
||||
return m_alt_key;
|
||||
if (key_arg == "Control")
|
||||
return m_ctrl_key;
|
||||
if (key_arg == "Shift")
|
||||
return m_shift_key;
|
||||
if (key_arg == "Meta")
|
||||
return m_meta_key;
|
||||
return false;
|
||||
}
|
||||
}
|
62
Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h
Normal file
62
Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/TypeCasts.h>
|
||||
#include <Kernel/API/KeyCode.h>
|
||||
#include <LibWeb/UIEvents/UIEvent.h>
|
||||
|
||||
namespace Web::UIEvents {
|
||||
|
||||
// https://www.w3.org/TR/uievents/#interface-keyboardevent
|
||||
class KeyboardEvent final : public UIEvent {
|
||||
public:
|
||||
using WrapperType = Bindings::KeyboardEventWrapper;
|
||||
|
||||
static NonnullRefPtr<KeyboardEvent> create(FlyString event_name, String key, String code, unsigned long location, bool ctrl_key, bool shift_key, bool alt_key, bool meta_key, bool repeat, bool is_composing, unsigned long key_code, unsigned long char_code)
|
||||
{
|
||||
return adopt_ref(*new KeyboardEvent(move(event_name), move(key), move(code), location, ctrl_key, shift_key, alt_key, meta_key, repeat, is_composing, key_code, char_code));
|
||||
}
|
||||
|
||||
static NonnullRefPtr<KeyboardEvent> create_from_platform_event(FlyString event_name, KeyCode, unsigned modifiers, u32 code_point);
|
||||
|
||||
virtual ~KeyboardEvent() override;
|
||||
|
||||
unsigned long key_code() const { return m_key_code; }
|
||||
unsigned long char_code() const { return m_char_code; }
|
||||
|
||||
String key() const { return m_key; }
|
||||
String code() const { return m_code; }
|
||||
unsigned long location() const { return m_location; }
|
||||
|
||||
bool ctrl_key() const { return m_ctrl_key; }
|
||||
bool shift_key() const { return m_shift_key; }
|
||||
bool alt_key() const { return m_alt_key; }
|
||||
bool meta_key() const { return m_meta_key; }
|
||||
|
||||
bool repeat() const { return m_repeat; }
|
||||
bool is_composing() const { return m_is_composing; }
|
||||
|
||||
bool get_modifier_state(String const& key_arg);
|
||||
|
||||
private:
|
||||
KeyboardEvent(FlyString event_name, String key, String code, unsigned long location, bool ctrl_key, bool shift_key, bool alt_key, bool meta_key, bool repeat, bool is_composing, unsigned long key_code, unsigned long char_code);
|
||||
|
||||
String m_key;
|
||||
String m_code;
|
||||
unsigned long m_location { 0 };
|
||||
bool m_ctrl_key { false };
|
||||
bool m_shift_key { false };
|
||||
bool m_alt_key { false };
|
||||
bool m_meta_key { false };
|
||||
bool m_repeat { false };
|
||||
bool m_is_composing { false };
|
||||
unsigned long m_key_code { 0 };
|
||||
unsigned long m_char_code { 0 };
|
||||
};
|
||||
|
||||
}
|
30
Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.idl
Normal file
30
Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.idl
Normal file
|
@ -0,0 +1,30 @@
|
|||
[Exposed=Window]
|
||||
interface KeyboardEvent : UIEvent {
|
||||
|
||||
// FIXME: Implement this.
|
||||
// constructor(DOMString type, optional KeyboardEventInit eventInitDict = {});
|
||||
|
||||
// KeyLocationCode
|
||||
const unsigned long DOM_KEY_LOCATION_STANDARD = 0x00;
|
||||
const unsigned long DOM_KEY_LOCATION_LEFT = 0x01;
|
||||
const unsigned long DOM_KEY_LOCATION_RIGHT = 0x02;
|
||||
const unsigned long DOM_KEY_LOCATION_NUMPAD = 0x03;
|
||||
|
||||
readonly attribute DOMString key;
|
||||
readonly attribute DOMString code;
|
||||
readonly attribute unsigned long location;
|
||||
|
||||
readonly attribute boolean ctrlKey;
|
||||
readonly attribute boolean shiftKey;
|
||||
readonly attribute boolean altKey;
|
||||
readonly attribute boolean metaKey;
|
||||
|
||||
readonly attribute boolean repeat;
|
||||
readonly attribute boolean isComposing;
|
||||
|
||||
readonly attribute unsigned long charCode;
|
||||
readonly attribute unsigned long keyCode;
|
||||
|
||||
boolean getModifierState(DOMString keyArg);
|
||||
|
||||
};
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
namespace Web::UIEvents {
|
||||
|
||||
class MouseEvent final : public UIEvents::UIEvent {
|
||||
class MouseEvent final : public UIEvent {
|
||||
public:
|
||||
using WrapperType = Bindings::MouseEventWrapper;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue