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