SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_keyboard.h
1#ifndef SDL3PP_KEYBOARD_H_
2#define SDL3PP_KEYBOARD_H_
3
4#include <SDL3/SDL_keyboard.h>
5#include "SDL3pp_keycode.h"
6#include "SDL3pp_scancode.h"
7#include "SDL3pp_stdinc.h"
8#include "SDL3pp_video.h"
9
10namespace SDL {
11
24
35using KeyboardID = SDL_KeyboardID;
36
48inline bool HasKeyboard() { return SDL_HasKeyboard(); }
49
70{
71 int count;
72 auto data = CheckError(SDL_GetKeyboards(&count));
73 return OwnArray<KeyboardID>{data, size_t(count)};
74}
75
91inline const char* GetKeyboardNameForID(KeyboardID instance_id)
92{
93 return SDL_GetKeyboardNameForID(instance_id);
94}
95
105inline WindowRef GetKeyboardFocus() { return {SDL_GetKeyboardFocus()}; }
106
137inline std::span<const bool> GetKeyboardState()
138{
139 int count;
140 auto data = SDL_GetKeyboardState(&count);
141 return std::span{data, size_t(count)};
142}
143
155inline void ResetKeyboard() { SDL_ResetKeyboard(); }
156
169inline Keymod GetModState() { return SDL_GetModState(); }
170
189inline void SetModState(Keymod modstate) { SDL_SetModState(modstate); }
190
191inline Keycode::Keycode(Scancode scancode, Keymod modstate, bool key_event)
192 : m_keycode(SDL_GetKeyFromScancode(scancode, modstate, key_event))
193{
194}
195
197 : m_keycode(SDL_GetKeyFromName(name))
198{
199}
200
201inline Scancode Keycode::GetScancode(Keymod* modstate) const
202{
203 return SDL_GetScancodeFromKey(m_keycode, modstate);
204}
205
207{
208 CheckError(SDL_SetScancodeName(m_scancode, name));
209}
210
211inline const char* Scancode::GetName() const
212{
213 return SDL_GetScancodeName(m_scancode);
214}
215
217 : m_scancode(SDL_GetScancodeFromName(name))
218{
219}
220
221inline const char* Keycode::GetName() const
222{
223 return SDL_GetKeyName(m_keycode);
224}
225
226inline void Window::StartTextInput() { CheckError(SDL_StartTextInput(get())); }
227
229{
230 CheckError(SDL_StartTextInputWithProperties(get(), props));
231}
232
244using TextInputType = SDL_TextInputType;
245
247 SDL_TEXTINPUT_TYPE_TEXT;
248
250 SDL_TEXTINPUT_TYPE_TEXT_NAME;
251
253 SDL_TEXTINPUT_TYPE_TEXT_EMAIL;
254
256 SDL_TEXTINPUT_TYPE_TEXT_USERNAME;
257
259 SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN;
261
263 SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_VISIBLE;
265
267 SDL_TEXTINPUT_TYPE_NUMBER;
268
270 SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN;
272
274 SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_VISIBLE;
276
288using Capitalization = SDL_Capitalization;
289
291 SDL_CAPITALIZE_NONE;
292
294 SDL_CAPITALIZE_SENTENCES;
296
298 SDL_CAPITALIZE_WORDS;
299
301 SDL_CAPITALIZE_LETTERS;
302
309
315constexpr auto TYPE_NUMBER = SDL_PROP_TEXTINPUT_TYPE_NUMBER;
316
322constexpr auto CAPITALIZATION_NUMBER = SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER;
323
324constexpr auto AUTOCORRECT_BOOLEAN = SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN;
325
326constexpr auto MULTILINE_BOOLEAN = SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN;
327
328constexpr auto ANDROID_INPUTTYPE_NUMBER =
329 SDL_PROP_TEXTINPUT_ANDROID_INPUTTYPE_NUMBER;
330
331} // namespace prop::TextInput
332
333inline bool Window::IsTextInputActive() const
334{
335 return SDL_TextInputActive(get());
336}
337
338inline void Window::StopTextInput() { CheckError(SDL_StopTextInput(get())); }
339
341{
342 CheckError(SDL_ClearComposition(get()));
343}
344
345inline void Window::SetTextInputArea(const RectRaw& rect, int cursor)
346{
347 CheckError(SDL_SetTextInputArea(get(), &rect, cursor));
348}
349
350inline void Window::GetTextInputArea(RectRaw* rect, int* cursor)
351{
352 CheckError(SDL_GetTextInputArea(get(), rect, cursor));
353}
354
369{
370 return SDL_HasScreenKeyboardSupport();
371}
372
374{
375 return SDL_ScreenKeyboardShown(get());
376}
377
379
380} // namespace SDL
381
382#endif /* SDL3PP_KEYBOARD_H_ */
constexpr Keycode(KeycodeRaw keycode={}) noexcept
Wraps Keycode.
Definition SDL3pp_keycode.h:129
Base class for SDL memory allocated array wrap.
Definition SDL3pp_ownPtr.h:44
constexpr RawPointer get() const noexcept
Definition SDL3pp_resource.h:53
The SDL keyboard scancode representation.
Definition SDL3pp_scancode.h:46
constexpr Scancode(ScancodeRaw scancode={}) noexcept
Wraps Scancode.
Definition SDL3pp_scancode.h:55
Helpers to use C++ strings parameters.
Definition SDL3pp_strings.h:43
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition SDL3pp_error.h:199
constexpr TextInputType TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN
The input is a secure PIN that is hidden.
Definition SDL3pp_keyboard.h:269
void SetName(StringParam name)
Set a human-readable name for a scancode.
Definition SDL3pp_keyboard.h:206
const char * GetName() const
Get a human-readable name for a key.
Definition SDL3pp_keyboard.h:221
SDL_Capitalization Capitalization
Auto capitalization type.
Definition SDL3pp_keyboard.h:288
void ResetKeyboard()
Clear the state of the keyboard.
Definition SDL3pp_keyboard.h:155
void GetTextInputArea(RectRaw *rect, int *cursor)
Get the area used to type Unicode text input.
Definition SDL3pp_keyboard.h:350
bool HasKeyboard()
Return whether a keyboard is currently connected.
Definition SDL3pp_keyboard.h:48
void ClearComposition()
Dismiss the composition window/IME without disabling the subsystem.
Definition SDL3pp_keyboard.h:340
void SetModState(Keymod modstate)
Set the current key modifier state for the keyboard.
Definition SDL3pp_keyboard.h:189
constexpr TextInputType TEXTINPUT_TYPE_TEXT_NAME
The input is a person's name.
Definition SDL3pp_keyboard.h:249
Keymod GetModState()
Get the current key modifier state for the keyboard.
Definition SDL3pp_keyboard.h:169
const char * GetKeyboardNameForID(KeyboardID instance_id)
Get the name of a keyboard.
Definition SDL3pp_keyboard.h:91
void StartTextInput()
Start accepting Unicode text input events in a window.
Definition SDL3pp_keyboard.h:226
WindowRef GetKeyboardFocus()
Query the window which currently has keyboard focus.
Definition SDL3pp_keyboard.h:105
constexpr TextInputType TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN
The input is a secure password that is hidden.
Definition SDL3pp_keyboard.h:258
constexpr TextInputType TEXTINPUT_TYPE_TEXT_EMAIL
The input is an e-mail address.
Definition SDL3pp_keyboard.h:252
constexpr Capitalization CAPITALIZE_SENTENCES
The first letter of sentences will be capitalized.
Definition SDL3pp_keyboard.h:293
OwnArray< KeyboardID > GetKeyboards()
Get a list of currently connected keyboards.
Definition SDL3pp_keyboard.h:69
SDL_KeyboardID KeyboardID
This is a unique ID for a keyboard for the time it is connected to the system, and is never reused fo...
Definition SDL3pp_keyboard.h:35
constexpr TextInputType TEXTINPUT_TYPE_TEXT_USERNAME
The input is a username.
Definition SDL3pp_keyboard.h:255
bool IsTextInputActive() const
Check whether or not Unicode text input events are enabled for a window.
Definition SDL3pp_keyboard.h:333
Scancode GetScancode(Keymod *modstate) const
Get the scancode corresponding to the given key code according to the current keyboard layout.
Definition SDL3pp_keyboard.h:201
void StopTextInput()
Stop receiving any text input events in a window.
Definition SDL3pp_keyboard.h:338
bool IsScreenKeyboardShown() const
Check whether the screen keyboard is shown for given window.
Definition SDL3pp_keyboard.h:373
constexpr TextInputType TEXTINPUT_TYPE_TEXT
The input is text.
Definition SDL3pp_keyboard.h:246
constexpr Capitalization CAPITALIZE_NONE
No auto-capitalization will be done.
Definition SDL3pp_keyboard.h:290
void SetTextInputArea(const RectRaw &rect, int cursor)
Set the area used to type Unicode text input.
Definition SDL3pp_keyboard.h:345
const char * GetName() const
Get a human-readable name for a scancode.
Definition SDL3pp_keyboard.h:211
constexpr Capitalization CAPITALIZE_WORDS
The first letter of words will be capitalized.
Definition SDL3pp_keyboard.h:297
constexpr TextInputType TEXTINPUT_TYPE_NUMBER
The input is a number.
Definition SDL3pp_keyboard.h:266
constexpr TextInputType TEXTINPUT_TYPE_TEXT_PASSWORD_VISIBLE
The input is a secure password that is visible.
Definition SDL3pp_keyboard.h:262
constexpr TextInputType TEXTINPUT_TYPE_NUMBER_PASSWORD_VISIBLE
The input is a secure PIN that is visible.
Definition SDL3pp_keyboard.h:273
SDL_TextInputType TextInputType
Text input type.
Definition SDL3pp_keyboard.h:244
constexpr Capitalization CAPITALIZE_LETTERS
All letters will be capitalized.
Definition SDL3pp_keyboard.h:300
bool HasScreenKeyboardSupport()
Check whether the platform has screen keyboard support.
Definition SDL3pp_keyboard.h:368
std::span< const bool > GetKeyboardState()
Get a snapshot of the current state of the keyboard.
Definition SDL3pp_keyboard.h:137
Uint16 Keymod
Valid key modifiers (possibly OR'd together).
Definition SDL3pp_keycode.h:34
ResourceRef< Properties > PropertiesRef
Reference for Properties.
Definition SDL3pp_properties.h:54
SDL_Rect RectRaw
Alias to raw representation for Rect.
Definition SDL3pp_rect.h:34
ResourceRef< Window > WindowRef
Reference for Window.
Definition SDL3pp_video.h:54
Properties for text input to be used on Window.StartTextInput.
Definition SDL3pp_keyboard.h:308
constexpr auto CAPITALIZATION_NUMBER
The auto-capitalization type to be used.
Definition SDL3pp_keyboard.h:322
constexpr auto TYPE_NUMBER
The type of text input to be used.
Definition SDL3pp_keyboard.h:315
Main include header for the SDL3pp library.