docs
Loading...
Searching...
No Matches
KeyEvent.h
Go to the documentation of this file.
1///
2/// Copyright (C) 2026 Ultralight, Inc. All rights reserved.
3/// A license is required for commercial use. https://ultralig.ht
4///
5#pragma once
8#include <Ultralight/String.h>
9#ifdef __OBJC__
10#import <AppKit/NSEvent.h>
11#endif
12
13namespace ultralight {
14
15///
16/// Internal platform key event structure.
17///
18class PlatformKeyEvent;
19
20///
21/// Keyboard event representing a change in keyboard state.
22///
23/// @see View::FireKeyEvent
24///
26 public:
27 ///
28 /// The various KeyEvent types.
29 ///
30 enum Type {
31 ///
32 /// Key-Down event type. This type combines a key press with its text input: it triggers
33 /// editing shortcuts (eg, Ctrl+C for copy) and, when `text` is set, also inserts that text.
34 ///
35 /// @warning You should probably use kType_RawKeyDown instead (followed by a kType_Char event
36 /// for any text). This type is only here for historic compatibility with WebCore's
37 /// key event types.
38 ///
40
41 ///
42 /// Key-Up event type. Use this when a physical key is released.
43 ///
45
46 ///
47 /// Raw Key-Down type. Use this when a physical key is pressed.
48 ///
50
51 ///
52 /// Character input event type. Use this when the OS generates text from a physical key being
53 /// pressed (for example, this maps to WM_CHAR on Windows).
54 ///
56 };
57
58 ///
59 /// Creates an empty KeyEvent, you will need to initialize its members
60 /// yourself. This is useful for synthesizing your own keyboard events.
61 ///
63
64#ifdef _WIN32
65 ///
66 /// Create a KeyEvent directly from a Windows keyboard event.
67 ///
68 KeyEvent(Type type, uintptr_t wparam, intptr_t lparam, bool is_system_key);
69#endif
70
71#ifdef __OBJC__
72 ///
73 /// Create a KeyEvent directly from a macOS NSEvent.
74 ///
75 KeyEvent(NSEvent* evt);
76#endif
77
78 /// Copy constructor
79 KeyEvent(const KeyEvent& other);
80
81 /// Copy assignment
82 KeyEvent& operator=(const KeyEvent& other);
83
84 /// Move constructor
85 KeyEvent(KeyEvent&& other) noexcept;
86
87 /// Move assignment
88 KeyEvent& operator=(KeyEvent&& other) noexcept;
89
91
92 ///
93 /// An enumeration of the different keyboard modifiers.
94 ///
95 enum Modifiers : uint8_t {
96 /// Whether or not an ALT key is down
97 kMod_AltKey = 1 << 0,
98
99 /// Whether or not a Control key is down
100 kMod_CtrlKey = 1 << 1,
101
102 /// Whether or not a meta key (Command-key on Mac, Windows-key on Win) is down
103 kMod_MetaKey = 1 << 2,
104
105 /// Whether or not a Shift key is down
107 };
108
109 ///
110 /// The type of this KeyEvent.
111 ///
113
114 ///
115 /// The current state of the keyboard. Modifiers may be OR'd together to represent multiple
116 /// values.
117 ///
118 unsigned modifiers;
119
120 ///
121 /// The virtual key-code associated with this keyboard event. This is either directly from the
122 /// event (ie, WPARAM on Windows) or via a mapping function. You can see a full list of the
123 /// possible virtual key-codes in KeyCodes.h
124 ///
126
127 ///
128 /// The actual key-code generated by the platform. The DOM spec primarily uses Windows-equivalent
129 /// codes (hence virtualKeyCode above) but it helps to also specify the platform-specific
130 /// key-code as well.
131 ///
133
134 ///
135 /// This is a string identifying the key that was pressed. This can be generated from the
136 /// virtual_key_code via the GetKeyIdentifierFromVirtualKeyCode() utility function. You can find
137 /// the full list of key identifiers at:
138 /// <https://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/keyset.html>
139 ///
141
142 ///
143 /// The actual text generated by this keyboard event. This is usually only a single character.
144 ///
146
147 ///
148 /// The text generated by this keyboard event before all modifiers except shift are applied. This
149 /// is used internally for working out shortcut keys. This is usually only a single character.
150 ///
152
153 ///
154 /// Whether or not this is a keypad event.
155 ///
157
158 ///
159 /// Whether or not this was generated as the result of an auto-repeat (eg, holding down a key).
160 ///
162
163 ///
164 /// Whether or not the pressed key is a "system key". This is a Windows-only concept and should
165 /// be "false" for all non-Windows platforms. For more information, see the following link:
166 /// <http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx>
167 ///
169
170 ///
171 /// Internal, platform-specific key event data (set by the library's own platform
172 /// integrations). Leave this null when creating events yourself.
173 ///
174 PlatformKeyEvent* platform_event = nullptr;
175};
176
177///
178/// Utility function for generating a key identifier string from a virtual
179/// key-code.
180///
181/// @param virtual_key_code The virtual key-code to generate the key identifier from.
182///
183/// @param key_identifier_result The string to store the result in.
184///
186 String& key_identifier_result);
187
188///
189/// Utility function for generating a key string from a virtual key-code.
190///
191/// @param virtual_key_code The virtual key-code to generate the key string from.
192///
193/// @param shift Whether or not the shift key is currently pressed.
194///
195/// @param key_result The string to store the result in.
196///
197/// @note This function assumes US keyboard layout.
198///
199/// @see <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values>
200///
201void UExport GetKeyFromVirtualKeyCode(int virtual_key_code, bool shift, String& key_result);
202
203} // namespace ultralight
#define UExport
Definition Exports.h:22
bool is_auto_repeat
Whether or not this was generated as the result of an auto-repeat (eg, holding down a key).
Definition KeyEvent.h:161
Type
The various KeyEvent types.
Definition KeyEvent.h:30
@ kType_RawKeyDown
Raw Key-Down type.
Definition KeyEvent.h:49
@ kType_Char
Character input event type.
Definition KeyEvent.h:55
@ kType_KeyDown
Key-Down event type.
Definition KeyEvent.h:39
@ kType_KeyUp
Key-Up event type.
Definition KeyEvent.h:44
String key_identifier
This is a string identifying the key that was pressed.
Definition KeyEvent.h:140
bool is_keypad
Whether or not this is a keypad event.
Definition KeyEvent.h:156
KeyEvent & operator=(KeyEvent &&other) noexcept
Move assignment.
bool is_system_key
Whether or not the pressed key is a "system key".
Definition KeyEvent.h:168
Modifiers
An enumeration of the different keyboard modifiers.
Definition KeyEvent.h:95
@ kMod_AltKey
Whether or not an ALT key is down.
Definition KeyEvent.h:97
@ kMod_CtrlKey
Whether or not a Control key is down.
Definition KeyEvent.h:100
@ kMod_ShiftKey
Whether or not a Shift key is down.
Definition KeyEvent.h:106
@ kMod_MetaKey
Whether or not a meta key (Command-key on Mac, Windows-key on Win) is down.
Definition KeyEvent.h:103
String unmodified_text
The text generated by this keyboard event before all modifiers except shift are applied.
Definition KeyEvent.h:151
String text
The actual text generated by this keyboard event.
Definition KeyEvent.h:145
KeyEvent(const KeyEvent &other)
Copy constructor.
int virtual_key_code
The virtual key-code associated with this keyboard event.
Definition KeyEvent.h:125
PlatformKeyEvent * platform_event
Internal, platform-specific key event data (set by the library's own platform integrations).
Definition KeyEvent.h:174
KeyEvent(KeyEvent &&other) noexcept
Move constructor.
Type type
The type of this KeyEvent.
Definition KeyEvent.h:112
KeyEvent()
Creates an empty KeyEvent, you will need to initialize its members yourself.
KeyEvent & operator=(const KeyEvent &other)
Copy assignment.
int native_key_code
The actual key-code generated by the platform.
Definition KeyEvent.h:132
unsigned modifiers
The current state of the keyboard.
Definition KeyEvent.h:118
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:31
Root namespace for every public Ultralight type, function, and enumeration.
void GetKeyIdentifierFromVirtualKeyCode(int virtual_key_code, String &key_identifier_result)
Utility function for generating a key identifier string from a virtual key-code.
void GetKeyFromVirtualKeyCode(int virtual_key_code, bool shift, String &key_result)
Utility function for generating a key string from a virtual key-code.