1/*
2 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
3 * Copyright (C) 2001 Tobias Anton (anton@stud.fbi.fh-darmstadt.de)
4 * Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
5 * Copyright (C) 2003-2018 Apple Inc. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24#pragma once
25
26#include "EventModifierInit.h"
27#include "KeypressCommand.h"
28#include "UIEventWithKeyState.h"
29#include <memory>
30#include <wtf/Vector.h>
31
32namespace WebCore {
33
34class Node;
35class PlatformKeyboardEvent;
36
37class KeyboardEvent final : public UIEventWithKeyState {
38public:
39 enum KeyLocationCode {
40 DOM_KEY_LOCATION_STANDARD = 0x00,
41 DOM_KEY_LOCATION_LEFT = 0x01,
42 DOM_KEY_LOCATION_RIGHT = 0x02,
43 DOM_KEY_LOCATION_NUMPAD = 0x03
44 };
45
46 WEBCORE_EXPORT static Ref<KeyboardEvent> create(const PlatformKeyboardEvent&, RefPtr<WindowProxy>&&);
47 static Ref<KeyboardEvent> createForBindings();
48
49 struct Init : public EventModifierInit {
50 String key;
51 String code;
52 unsigned location;
53 bool repeat;
54 bool isComposing;
55
56 // Legacy.
57 String keyIdentifier;
58 Optional<unsigned> keyLocation;
59 unsigned charCode;
60 unsigned keyCode;
61 unsigned which;
62 };
63
64 static Ref<KeyboardEvent> create(const AtomString& type, const Init&);
65
66 virtual ~KeyboardEvent();
67
68 WEBCORE_EXPORT void initKeyboardEvent(const AtomString& type, bool canBubble, bool cancelable, RefPtr<WindowProxy>&&,
69 const String& keyIdentifier, unsigned location,
70 bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey = false);
71
72#if ENABLE(KEYBOARD_KEY_ATTRIBUTE)
73 const String& key() const { return m_key; }
74#endif
75#if ENABLE(KEYBOARD_CODE_ATTRIBUTE)
76 const String& code() const { return m_code; }
77#endif
78
79 const String& keyIdentifier() const { return m_keyIdentifier; }
80 unsigned location() const { return m_location; }
81 bool repeat() const { return m_repeat; }
82
83 const PlatformKeyboardEvent* underlyingPlatformEvent() const { return m_underlyingPlatformEvent.get(); }
84 PlatformKeyboardEvent* underlyingPlatformEvent() { return m_underlyingPlatformEvent.get(); }
85
86 WEBCORE_EXPORT int keyCode() const; // key code for keydown and keyup, character for keypress
87 WEBCORE_EXPORT int charCode() const; // character code for keypress, 0 for keydown and keyup
88
89 EventInterface eventInterface() const final;
90 bool isKeyboardEvent() const final;
91 int which() const final;
92
93 bool isComposing() const { return m_isComposing; }
94
95#if PLATFORM(COCOA)
96 bool handledByInputMethod() const { return m_handledByInputMethod; }
97 const Vector<KeypressCommand>& keypressCommands() const { return m_keypressCommands; }
98 Vector<KeypressCommand>& keypressCommands() { return m_keypressCommands; }
99#endif
100
101private:
102 KeyboardEvent();
103 KeyboardEvent(const PlatformKeyboardEvent&, RefPtr<WindowProxy>&&);
104 KeyboardEvent(const AtomString&, const Init&);
105
106 std::unique_ptr<PlatformKeyboardEvent> m_underlyingPlatformEvent;
107#if ENABLE(KEYBOARD_KEY_ATTRIBUTE)
108 String m_key;
109#endif
110#if ENABLE(KEYBOARD_CODE_ATTRIBUTE)
111 String m_code;
112#endif
113 String m_keyIdentifier;
114 unsigned m_location { DOM_KEY_LOCATION_STANDARD };
115 bool m_repeat { false };
116 bool m_isComposing { false };
117 Optional<unsigned> m_charCode;
118 Optional<unsigned> m_keyCode;
119 Optional<unsigned> m_which;
120
121#if PLATFORM(COCOA)
122 // Commands that were sent by AppKit when interpreting the event. Doesn't include input method commands.
123 bool m_handledByInputMethod { false };
124 Vector<KeypressCommand> m_keypressCommands;
125#endif
126};
127
128} // namespace WebCore
129
130SPECIALIZE_TYPE_TRAITS_EVENT(KeyboardEvent)
131