docs
Loading...
Searching...
No Matches
CAPI_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
6///
7/// @file CAPI_KeyEvent.h
8///
9/// Key event interface.
10///
11/// `#include <Ultralight/CAPI/CAPI_KeyEvent.h>`
12///
13/// This file defines the C API for various key events.
14///
15#ifndef ULTRALIGHT_CAPI_KEYEVENT_H
16#define ULTRALIGHT_CAPI_KEYEVENT_H
17
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
24/******************************************************************************
25 * Key Event
26 ******************************************************************************/
27
28///
29/// Create a key event.
30///
31/// @param type The type of key event (see ULKeyEventType).
32/// @param modifiers Keyboard modifiers, a bitwise OR of ULKeyEventModifiers values.
33/// @param virtual_key_code The virtual key-code associated with this event (see KeyCodes.h).
34/// @param native_key_code The platform-specific key-code generated by the OS.
35/// @param text The text generated by this key event (usually a single character).
36/// Pass an empty ULString if not applicable.
37/// @param unmodified_text The text generated before all modifiers except Shift are applied.
38/// Used internally for shortcut key processing.
39/// @param is_keypad Whether this is a keypad event.
40/// @param is_auto_repeat Whether this event was generated by holding down a key.
41/// @param is_system_key Whether the pressed key is a "system key" (Windows only, set false
42/// on other platforms).
43///
44/// @note The key identifier is derived for you from `virtual_key_code` for every type except
45/// kKeyEventType_Char, which leaves it empty. You don't need to call
46/// ulGetKeyIdentifierFromVirtualKeyCode() yourself.
47///
48/// @return A new ULKeyEvent instance. You must call ulDestroyKeyEvent() when finished (typically
49/// after passing it to ulViewFireKeyEvent()).
50///
52 int virtual_key_code, int native_key_code, ULString text,
53 ULString unmodified_text, bool is_keypad, bool is_auto_repeat,
54 bool is_system_key);
55
56#ifdef _WIN32
57///
58/// Create a key event from native Windows event.
59///
60/// @return A new ULKeyEvent instance. You must call ulDestroyKeyEvent() when finished.
61///
62ULExport ULKeyEvent ulCreateKeyEventWindows(ULKeyEventType type, uintptr_t wparam, intptr_t lparam,
63 bool is_system_key);
64#endif
65
66#ifdef __OBJC__
67///
68/// Create a key event from native macOS event.
69///
70/// @return A new ULKeyEvent instance. You must call ulDestroyKeyEvent() when finished.
71///
72ULExport ULKeyEvent ulCreateKeyEventMacOS(NSEvent* evt);
73#endif
74
75///
76/// Destroy a key event created by ulCreateKeyEvent() or a platform-specific variant.
77///
79
80///
81/// Get the type of a key event.
82///
83/// @param evt The key event.
84///
85/// @return Returns the type of key event (see ULKeyEventType).
86///
88
89///
90/// Get the keyboard modifiers of a key event.
91///
92/// @param evt The key event.
93///
94/// @return Returns a bitwise OR of ULKeyEventModifiers values.
95///
97
98///
99/// Get the virtual key-code of a key event.
100///
101/// @param evt The key event.
102///
103/// @return Returns the virtual key-code associated with this event (see KeyCodes.h).
104///
106
107///
108/// Get the platform-specific key-code of a key event.
109///
110/// @param evt The key event.
111///
112/// @return Returns the key-code generated by the OS.
113///
115
116///
117/// Get the key identifier of a key event (eg, "Enter" or "U+0041").
118///
119/// @param evt The key event.
120///
121/// @return Returns the key identifier (empty for a kKeyEventType_Char event).
122///
123/// @note Don't destroy the returned string, it is owned by the event. It stays valid until you
124/// destroy the event (for an event passed to a callback, until the callback returns). Copy
125/// it with ulCreateStringFromCopy() to keep it longer.
126///
128
129///
130/// Get the text generated by a key event (usually a single character).
131///
132/// @param evt The key event.
133///
134/// @return Returns the text, or an empty string if the event generated none.
135///
136/// @note Don't destroy the returned string, it is owned by the event. It stays valid until you
137/// destroy the event (for an event passed to a callback, until the callback returns). Copy
138/// it with ulCreateStringFromCopy() to keep it longer.
139///
141
142///
143/// Get the text a key event generated before all modifiers except Shift were applied.
144///
145/// @param evt The key event.
146///
147/// @return Returns the unmodified text, or an empty string if the event generated none.
148///
149/// @note Don't destroy the returned string, it is owned by the event. It stays valid until you
150/// destroy the event (for an event passed to a callback, until the callback returns). Copy
151/// it with ulCreateStringFromCopy() to keep it longer.
152///
154
155///
156/// Whether or not a key event came from the keypad.
157///
158/// @param evt The key event.
159///
160/// @return Returns true if this is a keypad event.
161///
163
164///
165/// Whether or not a key event was generated by holding down a key.
166///
167/// @param evt The key event.
168///
169/// @return Returns true if this event is an auto-repeat.
170///
172
173///
174/// Whether or not the pressed key is a "system key" (a Windows-only concept).
175///
176/// @param evt The key event.
177///
178/// @return Returns true if this is a system key event (always false on other platforms).
179///
181
182///
183/// Get a key identifier string from a virtual key-code.
184///
185/// @param virtual_key_code The virtual key-code to generate the key identifier from.
186///
187/// @return A ULString containing the key identifier. You must call ulDestroyString() when
188/// finished.
189///
191
192///
193/// Get a key string from a virtual key-code.
194///
195/// @param virtual_key_code The virtual key-code to generate the key string from.
196/// @param shift Whether or not the shift key is currently pressed.
197///
198/// @return A ULString containing the key string. You must call ulDestroyString() when finished.
199///
200/// @note This function assumes US keyboard layout.
201///
202ULExport ULString ulGetKeyFromVirtualKeyCode(int virtual_key_code, bool shift);
203
204#ifdef __cplusplus
205} // extern "C"
206#endif
207
208#endif // ULTRALIGHT_CAPI_KEYEVENT_H
ULString ulGetKeyIdentifierFromVirtualKeyCode(int virtual_key_code)
Get a key identifier string from a virtual key-code.
bool ulKeyEventIsKeypad(ULKeyEvent evt)
Whether or not a key event came from the keypad.
void ulDestroyKeyEvent(ULKeyEvent evt)
Destroy a key event created by ulCreateKeyEvent() or a platform-specific variant.
unsigned int ulKeyEventGetModifiers(ULKeyEvent evt)
Get the keyboard modifiers of a key event.
ULString ulGetKeyFromVirtualKeyCode(int virtual_key_code, bool shift)
Get a key string from a virtual key-code.
ULString ulKeyEventGetKeyIdentifier(ULKeyEvent evt)
Get the key identifier of a key event (eg, "Enter" or "U+0041").
bool ulKeyEventIsSystemKey(ULKeyEvent evt)
Whether or not the pressed key is a "system key" (a Windows-only concept).
ULString ulKeyEventGetText(ULKeyEvent evt)
Get the text generated by a key event (usually a single character).
int ulKeyEventGetVirtualKeyCode(ULKeyEvent evt)
Get the virtual key-code of a key event.
ULKeyEventType ulKeyEventGetType(ULKeyEvent evt)
Get the type of a key event.
bool ulKeyEventIsAutoRepeat(ULKeyEvent evt)
Whether or not a key event was generated by holding down a key.
ULKeyEvent ulCreateKeyEvent(ULKeyEventType type, unsigned int modifiers, int virtual_key_code, int native_key_code, ULString text, ULString unmodified_text, bool is_keypad, bool is_auto_repeat, bool is_system_key)
Create a key event.
int ulKeyEventGetNativeKeyCode(ULKeyEvent evt)
Get the platform-specific key-code of a key event.
ULString ulKeyEventGetUnmodifiedText(ULKeyEvent evt)
Get the text a key event generated before all modifiers except Shift were applied.
Various defines and utility functions for the C API.
struct C_String * ULString
Opaque handle to a String object.
Definition CAPI_Defines.h:96
#define ULExport
Definition CAPI_Defines.h:42
ULKeyEventType
Definition CAPI_Defines.h:247
struct C_KeyEvent * ULKeyEvent
Opaque handle to a KeyEvent object.
Definition CAPI_Defines.h:104