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