SDL_keyboard.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * # CategoryKeyboard
  20. *
  21. * SDL keyboard management.
  22. */
  23. #ifndef SDL_keyboard_h_
  24. #define SDL_keyboard_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. #include <SDL3/SDL_keycode.h>
  28. #include <SDL3/SDL_properties.h>
  29. #include <SDL3/SDL_rect.h>
  30. #include <SDL3/SDL_scancode.h>
  31. #include <SDL3/SDL_video.h>
  32. #include <SDL3/SDL_begin_code.h>
  33. /* Set up for C function definitions, even when using C++ */
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. /**
  38. * This is a unique ID for a keyboard for the time it is connected to the
  39. * system, and is never reused for the lifetime of the application.
  40. *
  41. * If the keyboard is disconnected and reconnected, it will get a new ID.
  42. *
  43. * The value 0 is an invalid ID.
  44. *
  45. * \since This datatype is available since SDL 3.0.0.
  46. */
  47. typedef Uint32 SDL_KeyboardID;
  48. /* Function prototypes */
  49. /**
  50. * Return whether a keyboard is currently connected.
  51. *
  52. * \returns true if a keyboard is connected, false otherwise.
  53. *
  54. * \since This function is available since SDL 3.0.0.
  55. *
  56. * \sa SDL_GetKeyboards
  57. */
  58. extern SDL_DECLSPEC bool SDLCALL SDL_HasKeyboard(void);
  59. /**
  60. * Get a list of currently connected keyboards.
  61. *
  62. * Note that this will include any device or virtual driver that includes
  63. * keyboard functionality, including some mice, KVM switches, motherboard
  64. * power buttons, etc. You should wait for input from a device before you
  65. * consider it actively in use.
  66. *
  67. * \param count a pointer filled in with the number of keyboards returned, may
  68. * be NULL.
  69. * \returns a 0 terminated array of keyboards instance IDs or NULL on failure;
  70. * call SDL_GetError() for more information. This should be freed
  71. * with SDL_free() when it is no longer needed.
  72. *
  73. * \since This function is available since SDL 3.0.0.
  74. *
  75. * \sa SDL_GetKeyboardNameForID
  76. * \sa SDL_HasKeyboard
  77. */
  78. extern SDL_DECLSPEC SDL_KeyboardID * SDLCALL SDL_GetKeyboards(int *count);
  79. /**
  80. * Get the name of a keyboard.
  81. *
  82. * This function returns "" if the keyboard doesn't have a name.
  83. *
  84. * \param instance_id the keyboard instance ID.
  85. * \returns the name of the selected keyboard or NULL on failure; call
  86. * SDL_GetError() for more information.
  87. *
  88. * \since This function is available since SDL 3.0.0.
  89. *
  90. * \sa SDL_GetKeyboards
  91. */
  92. extern SDL_DECLSPEC const char * SDLCALL SDL_GetKeyboardNameForID(SDL_KeyboardID instance_id);
  93. /**
  94. * Query the window which currently has keyboard focus.
  95. *
  96. * \returns the window with keyboard focus.
  97. *
  98. * \since This function is available since SDL 3.0.0.
  99. */
  100. extern SDL_DECLSPEC SDL_Window * SDLCALL SDL_GetKeyboardFocus(void);
  101. /**
  102. * Get a snapshot of the current state of the keyboard.
  103. *
  104. * The pointer returned is a pointer to an internal SDL array. It will be
  105. * valid for the whole lifetime of the application and should not be freed by
  106. * the caller.
  107. *
  108. * A array element with a value of true means that the key is pressed and a
  109. * value of false means that it is not. Indexes into this array are obtained
  110. * by using SDL_Scancode values.
  111. *
  112. * Use SDL_PumpEvents() to update the state array.
  113. *
  114. * This function gives you the current state after all events have been
  115. * processed, so if a key or button has been pressed and released before you
  116. * process events, then the pressed state will never show up in the
  117. * SDL_GetKeyboardState() calls.
  118. *
  119. * Note: This function doesn't take into account whether shift has been
  120. * pressed or not.
  121. *
  122. * \param numkeys if non-NULL, receives the length of the returned array.
  123. * \returns a pointer to an array of key states.
  124. *
  125. * \since This function is available since SDL 3.0.0.
  126. *
  127. * \sa SDL_PumpEvents
  128. * \sa SDL_ResetKeyboard
  129. */
  130. extern SDL_DECLSPEC const bool * SDLCALL SDL_GetKeyboardState(int *numkeys);
  131. /**
  132. * Clear the state of the keyboard.
  133. *
  134. * This function will generate key up events for all pressed keys.
  135. *
  136. * \since This function is available since SDL 3.0.0.
  137. *
  138. * \sa SDL_GetKeyboardState
  139. */
  140. extern SDL_DECLSPEC void SDLCALL SDL_ResetKeyboard(void);
  141. /**
  142. * Get the current key modifier state for the keyboard.
  143. *
  144. * \returns an OR'd combination of the modifier keys for the keyboard. See
  145. * SDL_Keymod for details.
  146. *
  147. * \since This function is available since SDL 3.0.0.
  148. *
  149. * \sa SDL_GetKeyboardState
  150. * \sa SDL_SetModState
  151. */
  152. extern SDL_DECLSPEC SDL_Keymod SDLCALL SDL_GetModState(void);
  153. /**
  154. * Set the current key modifier state for the keyboard.
  155. *
  156. * The inverse of SDL_GetModState(), SDL_SetModState() allows you to impose
  157. * modifier key states on your application. Simply pass your desired modifier
  158. * states into `modstate`. This value may be a bitwise, OR'd combination of
  159. * SDL_Keymod values.
  160. *
  161. * This does not change the keyboard state, only the key modifier flags that
  162. * SDL reports.
  163. *
  164. * \param modstate the desired SDL_Keymod for the keyboard.
  165. *
  166. * \since This function is available since SDL 3.0.0.
  167. *
  168. * \sa SDL_GetModState
  169. */
  170. extern SDL_DECLSPEC void SDLCALL SDL_SetModState(SDL_Keymod modstate);
  171. /**
  172. * Get the key code corresponding to the given scancode according to the
  173. * current keyboard layout.
  174. *
  175. * If you want to get the keycode as it would be delivered in key events,
  176. * including options specified in SDL_HINT_KEYCODE_OPTIONS, then you should
  177. * pass `key_event` as true. Otherwise this function simply translates the
  178. * scancode based on the given modifier state.
  179. *
  180. * \param scancode the desired SDL_Scancode to query.
  181. * \param modstate the modifier state to use when translating the scancode to
  182. * a keycode.
  183. * \param key_event true if the keycode will be used in key events.
  184. * \returns the SDL_Keycode that corresponds to the given SDL_Scancode.
  185. *
  186. * \since This function is available since SDL 3.0.0.
  187. *
  188. * \sa SDL_GetKeyName
  189. * \sa SDL_GetScancodeFromKey
  190. */
  191. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromScancode(SDL_Scancode scancode, SDL_Keymod modstate, bool key_event);
  192. /**
  193. * Get the scancode corresponding to the given key code according to the
  194. * current keyboard layout.
  195. *
  196. * Note that there may be multiple scancode+modifier states that can generate
  197. * this keycode, this will just return the first one found.
  198. *
  199. * \param key the desired SDL_Keycode to query.
  200. * \param modstate a pointer to the modifier state that would be used when the
  201. * scancode generates this key, may be NULL.
  202. * \returns the SDL_Scancode that corresponds to the given SDL_Keycode.
  203. *
  204. * \since This function is available since SDL 3.0.0.
  205. *
  206. * \sa SDL_GetKeyFromScancode
  207. * \sa SDL_GetScancodeName
  208. */
  209. extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromKey(SDL_Keycode key, SDL_Keymod *modstate);
  210. /**
  211. * Set a human-readable name for a scancode.
  212. *
  213. * \param scancode the desired SDL_Scancode.
  214. * \param name the name to use for the scancode, encoded as UTF-8. The string
  215. * is not copied, so the pointer given to this function must stay
  216. * valid while SDL is being used.
  217. * \returns true on success or false on failure; call SDL_GetError() for more
  218. * information.
  219. *
  220. * \since This function is available since SDL 3.0.0.
  221. *
  222. * \sa SDL_GetScancodeName
  223. */
  224. extern SDL_DECLSPEC bool SDLCALL SDL_SetScancodeName(SDL_Scancode scancode, const char *name);
  225. /**
  226. * Get a human-readable name for a scancode.
  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. * \sa SDL_SetScancodeName
  246. */
  247. extern SDL_DECLSPEC const char * SDLCALL SDL_GetScancodeName(SDL_Scancode scancode);
  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. */
  261. extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromName(const char *name);
  262. /**
  263. * Get a human-readable name for a key.
  264. *
  265. * If the key doesn't have a name, this function returns an empty string ("").
  266. *
  267. * \param key the desired SDL_Keycode to query.
  268. * \returns a UTF-8 encoded string of the key name.
  269. *
  270. * \since This function is available since SDL 3.0.0.
  271. *
  272. * \sa SDL_GetKeyFromName
  273. * \sa SDL_GetKeyFromScancode
  274. * \sa SDL_GetScancodeFromKey
  275. */
  276. extern SDL_DECLSPEC const char * SDLCALL SDL_GetKeyName(SDL_Keycode key);
  277. /**
  278. * Get a key code from a human-readable name.
  279. *
  280. * \param name the human-readable key name.
  281. * \returns key code, or `SDLK_UNKNOWN` if the name wasn't recognized; call
  282. * SDL_GetError() for more information.
  283. *
  284. * \since This function is available since SDL 3.0.0.
  285. *
  286. * \sa SDL_GetKeyFromScancode
  287. * \sa SDL_GetKeyName
  288. * \sa SDL_GetScancodeFromName
  289. */
  290. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromName(const char *name);
  291. /**
  292. * Start accepting Unicode text input events in a window.
  293. *
  294. * This function will enable text input (SDL_EVENT_TEXT_INPUT and
  295. * SDL_EVENT_TEXT_EDITING events) in the specified window. Please use this
  296. * function paired with SDL_StopTextInput().
  297. *
  298. * Text input events are not received by default.
  299. *
  300. * On some platforms using this function shows the screen keyboard.
  301. *
  302. * \param window the window to enable text input.
  303. * \returns true on success or false on failure; call SDL_GetError() for more
  304. * information.
  305. *
  306. * \since This function is available since SDL 3.0.0.
  307. *
  308. * \sa SDL_SetTextInputArea
  309. * \sa SDL_StartTextInputWithProperties
  310. * \sa SDL_StopTextInput
  311. * \sa SDL_TextInputActive
  312. */
  313. extern SDL_DECLSPEC bool SDLCALL SDL_StartTextInput(SDL_Window *window);
  314. /**
  315. * Text input type.
  316. *
  317. * These are the valid values for SDL_PROP_TEXTINPUT_TYPE_NUMBER. Not every
  318. * value is valid on every platform, but where a value isn't supported, a
  319. * reasonable fallback will be used.
  320. *
  321. * \since This enum is available since SDL 3.0.0.
  322. *
  323. * \sa SDL_StartTextInputWithProperties
  324. */
  325. typedef enum SDL_TextInputType
  326. {
  327. SDL_TEXTINPUT_TYPE_TEXT, /**< The input is text */
  328. SDL_TEXTINPUT_TYPE_TEXT_NAME, /**< The input is a person's name */
  329. SDL_TEXTINPUT_TYPE_TEXT_EMAIL, /**< The input is an e-mail address */
  330. SDL_TEXTINPUT_TYPE_TEXT_USERNAME, /**< The input is a username */
  331. SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_HIDDEN, /**< The input is a secure password that is hidden */
  332. SDL_TEXTINPUT_TYPE_TEXT_PASSWORD_VISIBLE, /**< The input is a secure password that is visible */
  333. SDL_TEXTINPUT_TYPE_NUMBER, /**< The input is a number */
  334. SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_HIDDEN, /**< The input is a secure PIN that is hidden */
  335. SDL_TEXTINPUT_TYPE_NUMBER_PASSWORD_VISIBLE /**< The input is a secure PIN that is visible */
  336. } SDL_TextInputType;
  337. /**
  338. * Auto capitalization type.
  339. *
  340. * These are the valid values for
  341. * SDL_PROP_TEXTINPUT_AUTOCAPITALIZATION_NUMBER. Not every value is valid on
  342. * every platform, but where a value isn't supported, a reasonable fallback
  343. * will be used.
  344. *
  345. * \since This enum is available since SDL 3.0.0.
  346. *
  347. * \sa SDL_StartTextInputWithProperties
  348. */
  349. typedef enum SDL_Capitalization
  350. {
  351. SDL_CAPITALIZE_NONE, /**< No auto-capitalization will be done */
  352. SDL_CAPITALIZE_SENTENCES, /**< The first letter of sentences will be capitalized */
  353. SDL_CAPITALIZE_WORDS, /**< The first letter of words will be capitalized */
  354. SDL_CAPITALIZE_LETTERS /**< All letters will be capitalized */
  355. } SDL_Capitalization;
  356. /**
  357. * Start accepting Unicode text input events in a window, with properties
  358. * describing the input.
  359. *
  360. * This function will enable text input (SDL_EVENT_TEXT_INPUT and
  361. * SDL_EVENT_TEXT_EDITING events) in the specified window. Please use this
  362. * function paired with SDL_StopTextInput().
  363. *
  364. * Text input events are not received by default.
  365. *
  366. * On some platforms using this function shows the screen keyboard.
  367. *
  368. * These are the supported properties:
  369. *
  370. * - `SDL_PROP_TEXTINPUT_TYPE_NUMBER` - an SDL_TextInputType value that
  371. * describes text being input, defaults to SDL_TEXTINPUT_TYPE_TEXT.
  372. * - `SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER` - an SDL_Capitalization value
  373. * that describes how text should be capitalized, defaults to
  374. * SDL_CAPITALIZE_SENTENCES for normal text entry, SDL_CAPITALIZE_WORDS for
  375. * SDL_TEXTINPUT_TYPE_TEXT_NAME, and SDL_CAPITALIZE_NONE for e-mail
  376. * addresses, usernames, and passwords.
  377. * - `SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN` - true to enable auto completion
  378. * and auto correction, defaults to true.
  379. * - `SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN` - true if multiple lines of text
  380. * are allowed. This defaults to true if SDL_HINT_RETURN_KEY_HIDES_IME is
  381. * "0" or is not set, and defaults to false if SDL_HINT_RETURN_KEY_HIDES_IME
  382. * is "1".
  383. *
  384. * On Android you can directly specify the input type:
  385. *
  386. * - `SDL_PROP_TEXTINPUT_ANDROID_INPUTTYPE_NUMBER` - the text input type to
  387. * use, overriding other properties. This is documented at
  388. * https://developer.android.com/reference/android/text/InputType
  389. *
  390. * \param window the window to enable text input.
  391. * \param props the properties to use.
  392. * \returns true on success or false on failure; call SDL_GetError() for more
  393. * information.
  394. *
  395. * \since This function is available since SDL 3.0.0.
  396. *
  397. * \sa SDL_SetTextInputArea
  398. * \sa SDL_StartTextInput
  399. * \sa SDL_StopTextInput
  400. * \sa SDL_TextInputActive
  401. */
  402. extern SDL_DECLSPEC bool SDLCALL SDL_StartTextInputWithProperties(SDL_Window *window, SDL_PropertiesID props);
  403. #define SDL_PROP_TEXTINPUT_TYPE_NUMBER "SDL.textinput.type"
  404. #define SDL_PROP_TEXTINPUT_CAPITALIZATION_NUMBER "SDL.textinput.capitalization"
  405. #define SDL_PROP_TEXTINPUT_AUTOCORRECT_BOOLEAN "SDL.textinput.autocorrect"
  406. #define SDL_PROP_TEXTINPUT_MULTILINE_BOOLEAN "SDL.textinput.multiline"
  407. #define SDL_PROP_TEXTINPUT_ANDROID_INPUTTYPE_NUMBER "SDL.textinput.android.inputtype"
  408. /**
  409. * Check whether or not Unicode text input events are enabled for a window.
  410. *
  411. * \param window the window to check.
  412. * \returns true if text input events are enabled else false.
  413. *
  414. * \since This function is available since SDL 3.0.0.
  415. *
  416. * \sa SDL_StartTextInput
  417. */
  418. extern SDL_DECLSPEC bool SDLCALL SDL_TextInputActive(SDL_Window *window);
  419. /**
  420. * Stop receiving any text input events in a window.
  421. *
  422. * If SDL_StartTextInput() showed the screen keyboard, this function will hide
  423. * it.
  424. *
  425. * \param window the window to disable text input.
  426. * \returns true on success or false on failure; call SDL_GetError() for more
  427. * information.
  428. *
  429. * \since This function is available since SDL 3.0.0.
  430. *
  431. * \sa SDL_StartTextInput
  432. */
  433. extern SDL_DECLSPEC bool SDLCALL SDL_StopTextInput(SDL_Window *window);
  434. /**
  435. * Dismiss the composition window/IME without disabling the subsystem.
  436. *
  437. * \param window the window to affect.
  438. * \returns true on success or false on failure; call SDL_GetError() for more
  439. * information.
  440. *
  441. * \since This function is available since SDL 3.0.0.
  442. *
  443. * \sa SDL_StartTextInput
  444. * \sa SDL_StopTextInput
  445. */
  446. extern SDL_DECLSPEC bool SDLCALL SDL_ClearComposition(SDL_Window *window);
  447. /**
  448. * Set the area used to type Unicode text input.
  449. *
  450. * Native input methods may place a window with word suggestions near the
  451. * cursor, without covering the text being entered.
  452. *
  453. * \param window the window for which to set the text input area.
  454. * \param rect the SDL_Rect representing the text input area, in window
  455. * coordinates, or NULL to clear it.
  456. * \param cursor the offset of the current cursor location relative to
  457. * `rect->x`, in window coordinates.
  458. * \returns true on success or false on failure; call SDL_GetError() for more
  459. * information.
  460. *
  461. * \since This function is available since SDL 3.0.0.
  462. *
  463. * \sa SDL_GetTextInputArea
  464. * \sa SDL_StartTextInput
  465. */
  466. extern SDL_DECLSPEC bool SDLCALL SDL_SetTextInputArea(SDL_Window *window, const SDL_Rect *rect, int cursor);
  467. /**
  468. * Get the area used to type Unicode text input.
  469. *
  470. * This returns the values previously set by SDL_SetTextInputArea().
  471. *
  472. * \param window the window for which to query the text input area.
  473. * \param rect a pointer to an SDL_Rect filled in with the text input area,
  474. * may be NULL.
  475. * \param cursor a pointer to the offset of the current cursor location
  476. * relative to `rect->x`, may be NULL.
  477. * \returns true on success or false on failure; call SDL_GetError() for more
  478. * information.
  479. *
  480. * \since This function is available since SDL 3.0.0.
  481. *
  482. * \sa SDL_SetTextInputArea
  483. */
  484. extern SDL_DECLSPEC bool SDLCALL SDL_GetTextInputArea(SDL_Window *window, SDL_Rect *rect, int *cursor);
  485. /**
  486. * Check whether the platform has screen keyboard support.
  487. *
  488. * \returns true if the platform has some screen keyboard support or false if
  489. * not.
  490. *
  491. * \since This function is available since SDL 3.0.0.
  492. *
  493. * \sa SDL_StartTextInput
  494. * \sa SDL_ScreenKeyboardShown
  495. */
  496. extern SDL_DECLSPEC bool SDLCALL SDL_HasScreenKeyboardSupport(void);
  497. /**
  498. * Check whether the screen keyboard is shown for given window.
  499. *
  500. * \param window the window for which screen keyboard should be queried.
  501. * \returns true if screen keyboard is shown or false if not.
  502. *
  503. * \since This function is available since SDL 3.0.0.
  504. *
  505. * \sa SDL_HasScreenKeyboardSupport
  506. */
  507. extern SDL_DECLSPEC bool SDLCALL SDL_ScreenKeyboardShown(SDL_Window *window);
  508. /* Ends C function definitions when using C++ */
  509. #ifdef __cplusplus
  510. }
  511. #endif
  512. #include <SDL3/SDL_close_code.h>
  513. #endif /* SDL_keyboard_h_ */