Coverage Report

Created: 2025-03-04 07:22

/src/serenity/Userland/Libraries/LibWeb/UIEvents/KeyboardEvent.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <AK/FlyString.h>
10
#include <AK/TypeCasts.h>
11
#include <LibWeb/UIEvents/EventModifier.h>
12
#include <LibWeb/UIEvents/KeyCode.h>
13
#include <LibWeb/UIEvents/UIEvent.h>
14
15
namespace Web::UIEvents {
16
17
struct KeyboardEventInit : public EventModifierInit {
18
    String key;
19
    String code;
20
    u32 location { 0 };
21
    bool repeat { false };
22
    bool is_composing { false };
23
    u32 key_code { 0 };
24
    u32 char_code { 0 };
25
};
26
27
enum class DOMKeyLocation {
28
    Standard = 0,
29
    Left = 1,
30
    Right = 2,
31
    Numpad = 3,
32
};
33
34
// https://www.w3.org/TR/uievents/#interface-keyboardevent
35
class KeyboardEvent final : public UIEvent {
36
    WEB_PLATFORM_OBJECT(KeyboardEvent, UIEvent);
37
    JS_DECLARE_ALLOCATOR(KeyboardEvent);
38
39
public:
40
    [[nodiscard]] static JS::NonnullGCPtr<KeyboardEvent> create(JS::Realm&, FlyString const& event_name, KeyboardEventInit const& = {});
41
    [[nodiscard]] static JS::NonnullGCPtr<KeyboardEvent> create_from_platform_event(JS::Realm&, FlyString const& event_name, KeyCode, unsigned modifiers, u32 code_point);
42
    static WebIDL::ExceptionOr<JS::NonnullGCPtr<KeyboardEvent>> construct_impl(JS::Realm&, FlyString const& event_name, KeyboardEventInit const&);
43
44
    virtual ~KeyboardEvent() override;
45
46
0
    u32 key_code() const { return m_key_code; }
47
0
    u32 char_code() const { return m_char_code; }
48
49
0
    String key() const { return m_key; }
50
0
    String code() const { return m_code; }
51
0
    u32 location() const { return m_location; }
52
53
0
    bool ctrl_key() const { return m_ctrl_key; }
54
0
    bool shift_key() const { return m_shift_key; }
55
0
    bool alt_key() const { return m_alt_key; }
56
0
    bool meta_key() const { return m_meta_key; }
57
58
0
    bool repeat() const { return m_repeat; }
59
0
    bool is_composing() const { return m_is_composing; }
60
61
    bool get_modifier_state(String const& key_arg) const;
62
63
0
    virtual u32 which() const override { return m_key_code; }
64
65
    void init_keyboard_event(String const& type, bool bubbles, bool cancelable, HTML::Window* view, String const& key, WebIDL::UnsignedLong location, bool ctrl_key, bool alt_key, bool shift_key, bool meta_key);
66
67
private:
68
    KeyboardEvent(JS::Realm&, FlyString const& event_name, KeyboardEventInit const& event_init);
69
70
    virtual void initialize(JS::Realm&) override;
71
72
    String m_key;
73
    String m_code;
74
    u32 m_location { 0 };
75
    bool m_ctrl_key { false };
76
    bool m_shift_key { false };
77
    bool m_alt_key { false };
78
    bool m_meta_key { false };
79
    bool m_modifier_alt_graph { false };
80
    bool m_modifier_caps_lock { false };
81
    bool m_modifier_fn { false };
82
    bool m_modifier_fn_lock { false };
83
    bool m_modifier_hyper { false };
84
    bool m_modifier_num_lock { false };
85
    bool m_modifier_scroll_lock { false };
86
    bool m_modifier_super { false };
87
    bool m_modifier_symbol { false };
88
    bool m_modifier_symbol_lock { false };
89
    bool m_repeat { false };
90
    bool m_is_composing { false };
91
    u32 m_key_code { 0 };
92
    u32 m_char_code { 0 };
93
};
94
95
}