SDL 3.0
SDL_keyboard.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * \file SDL_keyboard.h
24 *
25 * Include file for SDL keyboard event handling
26 */
27
28#ifndef SDL_keyboard_h_
29#define SDL_keyboard_h_
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33#include <SDL3/SDL_keycode.h>
34#include <SDL3/SDL_video.h>
35
36#include <SDL3/SDL_begin_code.h>
37/* Set up for C function definitions, even when using C++ */
38#ifdef __cplusplus
39extern "C" {
40#endif
41
43
44/**
45 * The SDL keysym structure, used in key events.
46 *
47 * If you are looking for translated character input, see the
48 * ::SDL_EVENT_TEXT_INPUT event.
49 *
50 * \since This struct is available since SDL 3.0.0.
51 */
52typedef struct SDL_Keysym
53{
54 SDL_Scancode scancode; /**< SDL physical key code - see ::SDL_Scancode for details */
55 SDL_Keycode sym; /**< SDL virtual key code - see ::SDL_Keycode for details */
56 Uint16 mod; /**< current key modifiers */
59
60/* Function prototypes */
61
62/**
63 * Return whether a keyboard is currently connected.
64 *
65 * \returns SDL_TRUE if a keyboard is connected, SDL_FALSE otherwise.
66 *
67 * \since This function is available since SDL 3.0.0.
68 *
69 * \sa SDL_GetKeyboards
70 */
71extern DECLSPEC SDL_bool SDLCALL SDL_HasKeyboard(void);
72
73/**
74 * Get a list of currently connected keyboards.
75 *
76 * Note that this will include any device or virtual driver that includes
77 * keyboard functionality, including some mice, KVM switches, motherboard
78 * power buttons, etc. You should wait for input from a device before you
79 * consider it actively in use.
80 *
81 * \param count a pointer filled in with the number of keyboards returned
82 * \returns a 0 terminated array of keyboards instance IDs which should be
83 * freed with SDL_free(), or NULL on error; call SDL_GetError() for
84 * more details.
85 *
86 * \since This function is available since SDL 3.0.0.
87 *
88 * \sa SDL_GetKeyboardInstanceName
89 * \sa SDL_HasKeyboard
90 */
91extern DECLSPEC SDL_KeyboardID *SDLCALL SDL_GetKeyboards(int *count);
92
93/**
94 * Get the name of a keyboard.
95 *
96 * This function returns "" if the keyboard doesn't have a name.
97 *
98 * \param instance_id the keyboard instance ID
99 * \returns the name of the selected keyboard, or NULL on failure; call
100 * SDL_GetError() for more information.
101 *
102 * \since This function is available since SDL 3.0.0.
103 *
104 * \sa SDL_GetKeyboards
105 */
106extern DECLSPEC const char *SDLCALL SDL_GetKeyboardInstanceName(SDL_KeyboardID instance_id);
107
108/**
109 * Query the window which currently has keyboard focus.
110 *
111 * \returns the window with keyboard focus.
112 *
113 * \since This function is available since SDL 3.0.0.
114 */
115extern DECLSPEC SDL_Window * SDLCALL SDL_GetKeyboardFocus(void);
116
117/**
118 * Get a snapshot of the current state of the keyboard.
119 *
120 * The pointer returned is a pointer to an internal SDL array. It will be
121 * valid for the whole lifetime of the application and should not be freed by
122 * the caller.
123 *
124 * A array element with a value of 1 means that the key is pressed and a value
125 * of 0 means that it is not. Indexes into this array are obtained by using
126 * SDL_Scancode values.
127 *
128 * Use SDL_PumpEvents() to update the state array.
129 *
130 * This function gives you the current state after all events have been
131 * processed, so if a key or button has been pressed and released before you
132 * process events, then the pressed state will never show up in the
133 * SDL_GetKeyboardState() calls.
134 *
135 * Note: This function doesn't take into account whether shift has been
136 * pressed or not.
137 *
138 * \param numkeys if non-NULL, receives the length of the returned array
139 * \returns a pointer to an array of key states.
140 *
141 * \since This function is available since SDL 3.0.0.
142 *
143 * \sa SDL_PumpEvents
144 * \sa SDL_ResetKeyboard
145 */
146extern DECLSPEC const Uint8 *SDLCALL SDL_GetKeyboardState(int *numkeys);
147
148/**
149 * Clear the state of the keyboard.
150 *
151 * This function will generate key up events for all pressed keys.
152 *
153 * \since This function is available since SDL 3.0.0.
154 *
155 * \sa SDL_GetKeyboardState
156 */
157extern DECLSPEC void SDLCALL SDL_ResetKeyboard(void);
158
159/**
160 * Get the current key modifier state for the keyboard.
161 *
162 * \returns an OR'd combination of the modifier keys for the keyboard. See
163 * SDL_Keymod for details.
164 *
165 * \since This function is available since SDL 3.0.0.
166 *
167 * \sa SDL_GetKeyboardState
168 * \sa SDL_SetModState
169 */
170extern DECLSPEC SDL_Keymod SDLCALL SDL_GetModState(void);
171
172/**
173 * Set the current key modifier state for the keyboard.
174 *
175 * The inverse of SDL_GetModState(), SDL_SetModState() allows you to impose
176 * modifier key states on your application. Simply pass your desired modifier
177 * states into `modstate`. This value may be a bitwise, OR'd combination of
178 * SDL_Keymod values.
179 *
180 * This does not change the keyboard state, only the key modifier flags that
181 * SDL reports.
182 *
183 * \param modstate the desired SDL_Keymod for the keyboard
184 *
185 * \since This function is available since SDL 3.0.0.
186 *
187 * \sa SDL_GetModState
188 */
189extern DECLSPEC void SDLCALL SDL_SetModState(SDL_Keymod modstate);
190
191/**
192 * Get the key code corresponding to the given scancode according to the
193 * current keyboard layout.
194 *
195 * See SDL_Keycode for details.
196 *
197 * \param scancode the desired SDL_Scancode to query
198 * \returns the SDL_Keycode that corresponds to the given SDL_Scancode.
199 *
200 * \since This function is available since SDL 3.0.0.
201 *
202 * \sa SDL_GetKeyName
203 * \sa SDL_GetScancodeFromKey
204 */
205extern DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromScancode(SDL_Scancode scancode);
206
207/**
208 * Get the scancode corresponding to the given key code according to the
209 * current keyboard layout.
210 *
211 * See SDL_Scancode for details.
212 *
213 * \param key the desired SDL_Keycode to query
214 * \returns the SDL_Scancode that corresponds to the given SDL_Keycode.
215 *
216 * \since This function is available since SDL 3.0.0.
217 *
218 * \sa SDL_GetKeyFromScancode
219 * \sa SDL_GetScancodeName
220 */
222
223/**
224 * Get a human-readable name for a scancode.
225 *
226 * See SDL_Scancode for details.
227 *
228 * **Warning**: The returned name is by design not stable across platforms,
229 * e.g. the name for `SDL_SCANCODE_LGUI` is "Left GUI" under Linux but "Left
230 * Windows" under Microsoft Windows, and some scancodes like
231 * `SDL_SCANCODE_NONUSBACKSLASH` don't have any name at all. There are even
232 * scancodes that share names, e.g. `SDL_SCANCODE_RETURN` and
233 * `SDL_SCANCODE_RETURN2` (both called "Return"). This function is therefore
234 * unsuitable for creating a stable cross-platform two-way mapping between
235 * strings and scancodes.
236 *
237 * \param scancode the desired SDL_Scancode to query
238 * \returns a pointer to the name for the scancode. If the scancode doesn't
239 * have a name this function returns an empty string ("").
240 *
241 * \since This function is available since SDL 3.0.0.
242 *
243 * \sa SDL_GetScancodeFromKey
244 * \sa SDL_GetScancodeFromName
245 */
246extern DECLSPEC const char *SDLCALL SDL_GetScancodeName(SDL_Scancode scancode);
247
248/**
249 * Get a scancode from a human-readable name.
250 *
251 * \param name the human-readable scancode name
252 * \returns the SDL_Scancode, or `SDL_SCANCODE_UNKNOWN` if the name wasn't
253 * recognized; call SDL_GetError() for more information.
254 *
255 * \since This function is available since SDL 3.0.0.
256 *
257 * \sa SDL_GetKeyFromName
258 * \sa SDL_GetScancodeFromKey
259 * \sa SDL_GetScancodeName
260 */
261extern DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromName(const char *name);
262
263/**
264 * Get a human-readable name for a key.
265 *
266 * See SDL_Scancode and SDL_Keycode for details.
267 *
268 * \param key the desired SDL_Keycode to query
269 * \returns a pointer to a UTF-8 string that stays valid at least until the
270 * next call to this function. If you need it around any longer, you
271 * must copy it. If the key doesn't have a name, this function
272 * returns an empty string ("").
273 *
274 * \since This function is available since SDL 3.0.0.
275 *
276 * \sa SDL_GetKeyFromName
277 * \sa SDL_GetKeyFromScancode
278 * \sa SDL_GetScancodeFromKey
279 */
280extern DECLSPEC const char *SDLCALL SDL_GetKeyName(SDL_Keycode key);
281
282/**
283 * Get a key code from a human-readable name.
284 *
285 * \param name the human-readable key name
286 * \returns key code, or `SDLK_UNKNOWN` if the name wasn't recognized; call
287 * SDL_GetError() for more information.
288 *
289 * \since This function is available since SDL 3.0.0.
290 *
291 * \sa SDL_GetKeyFromScancode
292 * \sa SDL_GetKeyName
293 * \sa SDL_GetScancodeFromName
294 */
295extern DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromName(const char *name);
296
297/**
298 * Start accepting Unicode text input events.
299 *
300 * This function will start accepting Unicode text input events in the focused
301 * SDL window, and start emitting SDL_TextInputEvent (SDL_EVENT_TEXT_INPUT)
302 * and SDL_TextEditingEvent (SDL_EVENT_TEXT_EDITING) events. Please use this
303 * function in pair with SDL_StopTextInput().
304 *
305 * Text input events are not received by default.
306 *
307 * On some platforms using this function activates the screen keyboard.
308 *
309 * \since This function is available since SDL 3.0.0.
310 *
311 * \sa SDL_SetTextInputRect
312 * \sa SDL_StopTextInput
313 */
314extern DECLSPEC void SDLCALL SDL_StartTextInput(void);
315
316/**
317 * Check whether or not Unicode text input events are enabled.
318 *
319 * \returns SDL_TRUE if text input events are enabled else SDL_FALSE.
320 *
321 * \since This function is available since SDL 3.0.0.
322 *
323 * \sa SDL_StartTextInput
324 */
325extern DECLSPEC SDL_bool SDLCALL SDL_TextInputActive(void);
326
327/**
328 * Stop receiving any text input events.
329 *
330 * Text input events are not received by default.
331 *
332 * \since This function is available since SDL 3.0.0.
333 *
334 * \sa SDL_StartTextInput
335 */
336extern DECLSPEC void SDLCALL SDL_StopTextInput(void);
337
338/**
339 * Dismiss the composition window/IME without disabling the subsystem.
340 *
341 * \since This function is available since SDL 3.0.0.
342 *
343 * \sa SDL_StartTextInput
344 * \sa SDL_StopTextInput
345 */
346extern DECLSPEC void SDLCALL SDL_ClearComposition(void);
347
348/**
349 * Set the rectangle used to type Unicode text inputs.
350 *
351 * Native input methods will place a window with word suggestions near it,
352 * without covering the text being inputted.
353 *
354 * To start text input in a given location, this function is intended to be
355 * called before SDL_StartTextInput, although some platforms support moving
356 * the rectangle even while text input (and a composition) is active.
357 *
358 * Note: If you want to use the system native IME window, try setting hint
359 * **SDL_HINT_IME_SHOW_UI** to **1**, otherwise this function won't give you
360 * any feedback.
361 *
362 * \param rect the SDL_Rect structure representing the rectangle to receive
363 * text (ignored if NULL)
364 * \returns 0 on success or a negative error code on failure; call
365 * SDL_GetError() for more information.
366 *
367 * \since This function is available since SDL 3.0.0.
368 *
369 * \sa SDL_StartTextInput
370 */
371extern DECLSPEC int SDLCALL SDL_SetTextInputRect(const SDL_Rect *rect);
372
373/**
374 * Check whether the platform has screen keyboard support.
375 *
376 * \returns SDL_TRUE if the platform has some screen keyboard support or
377 * SDL_FALSE if not.
378 *
379 * \since This function is available since SDL 3.0.0.
380 *
381 * \sa SDL_StartTextInput
382 * \sa SDL_ScreenKeyboardShown
383 */
384extern DECLSPEC SDL_bool SDLCALL SDL_HasScreenKeyboardSupport(void);
385
386/**
387 * Check whether the screen keyboard is shown for given window.
388 *
389 * \param window the window for which screen keyboard should be queried
390 * \returns SDL_TRUE if screen keyboard is shown or SDL_FALSE if not.
391 *
392 * \since This function is available since SDL 3.0.0.
393 *
394 * \sa SDL_HasScreenKeyboardSupport
395 */
396extern DECLSPEC SDL_bool SDLCALL SDL_ScreenKeyboardShown(SDL_Window *window);
397
398/* Ends C function definitions when using C++ */
399#ifdef __cplusplus
400}
401#endif
402#include <SDL3/SDL_close_code.h>
403
404#endif /* SDL_keyboard_h_ */
SDL_Scancode SDL_GetScancodeFromName(const char *name)
SDL_bool SDL_HasScreenKeyboardSupport(void)
SDL_bool SDL_TextInputActive(void)
SDL_bool SDL_ScreenKeyboardShown(SDL_Window *window)
SDL_Keymod SDL_GetModState(void)
SDL_Scancode SDL_GetScancodeFromKey(SDL_Keycode key)
Uint32 SDL_KeyboardID
SDL_bool SDL_HasKeyboard(void)
int SDL_SetTextInputRect(const SDL_Rect *rect)
const char * SDL_GetKeyboardInstanceName(SDL_KeyboardID instance_id)
void SDL_ClearComposition(void)
void SDL_StartTextInput(void)
void SDL_ResetKeyboard(void)
SDL_Window * SDL_GetKeyboardFocus(void)
SDL_Keycode SDL_GetKeyFromScancode(SDL_Scancode scancode)
const Uint8 * SDL_GetKeyboardState(int *numkeys)
void SDL_SetModState(SDL_Keymod modstate)
const char * SDL_GetKeyName(SDL_Keycode key)
SDL_Keycode SDL_GetKeyFromName(const char *name)
void SDL_StopTextInput(void)
SDL_KeyboardID * SDL_GetKeyboards(int *count)
const char * SDL_GetScancodeName(SDL_Scancode scancode)
Sint32 SDL_Keycode
Definition SDL_keycode.h:49
SDL_Keymod
SDL_Scancode
uint8_t Uint8
Definition SDL_stdinc.h:188
uint16_t Uint16
Definition SDL_stdinc.h:206
int SDL_bool
Definition SDL_stdinc.h:170
uint32_t Uint32
Definition SDL_stdinc.h:224
struct SDL_Window SDL_Window
Definition SDL_video.h:119
SDL_Keycode sym
Uint32 unused
SDL_Scancode scancode