SDL_keyboard.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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_video.h>
  29. #include <SDL3/SDL_begin_code.h>
  30. /* Set up for C function definitions, even when using C++ */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /**
  35. * This is a unique ID for a keyboard for the time it is connected to the
  36. * system, and is never reused for the lifetime of the application.
  37. *
  38. * If the keyboard is disconnected and reconnected, it will get a new ID.
  39. *
  40. * The ID value starts at 1 and increments from there. The value 0 is an
  41. * invalid ID.
  42. *
  43. * \since This datatype is available since SDL 3.0.0.
  44. */
  45. typedef Uint32 SDL_KeyboardID;
  46. /* Function prototypes */
  47. /**
  48. * Return whether a keyboard is currently connected.
  49. *
  50. * \returns SDL_TRUE if a keyboard is connected, SDL_FALSE otherwise.
  51. *
  52. * \since This function is available since SDL 3.0.0.
  53. *
  54. * \sa SDL_GetKeyboards
  55. */
  56. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasKeyboard(void);
  57. /**
  58. * Get a list of currently connected keyboards.
  59. *
  60. * Note that this will include any device or virtual driver that includes
  61. * keyboard functionality, including some mice, KVM switches, motherboard
  62. * power buttons, etc. You should wait for input from a device before you
  63. * consider it actively in use.
  64. *
  65. * This returns temporary memory which will be automatically freed later, and can be claimed with SDL_ClaimTemporaryMemory().
  66. *
  67. * \param count a pointer filled in with the number of keyboards returned, may be NULL.
  68. * \returns a 0 terminated array of keyboards instance IDs or NULL on failure; call SDL_GetError() for
  69. * more information.
  70. *
  71. * \since This function is available since SDL 3.0.0.
  72. *
  73. * \sa SDL_GetKeyboardNameForID
  74. * \sa SDL_HasKeyboard
  75. */
  76. extern SDL_DECLSPEC const SDL_KeyboardID * SDLCALL SDL_GetKeyboards(int *count);
  77. /**
  78. * Get the name of a keyboard.
  79. *
  80. * This function returns "" if the keyboard doesn't have a name.
  81. *
  82. * This returns temporary memory which will be automatically freed later, and can be claimed with SDL_ClaimTemporaryMemory().
  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 1 means that the key is pressed and a value
  109. * of 0 means that it is not. Indexes into this array are obtained by using
  110. * 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 Uint8 * 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 a default
  173. * en_US keyboard layout.
  174. *
  175. * See SDL_Keycode for details.
  176. *
  177. * \param scancode the desired SDL_Scancode to query.
  178. * \param modstate the modifier state to use when translating the scancode to
  179. * a keycode.
  180. * \returns the SDL_Keycode that corresponds to the given SDL_Scancode.
  181. *
  182. * \since This function is available since SDL 3.0.0.
  183. *
  184. * \sa SDL_GetKeyName
  185. * \sa SDL_GetScancodeFromKey
  186. */
  187. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetDefaultKeyFromScancode(SDL_Scancode scancode, SDL_Keymod modstate);
  188. /**
  189. * Get the key code corresponding to the given scancode according to the
  190. * current keyboard layout.
  191. *
  192. * See SDL_Keycode for details.
  193. *
  194. * \param scancode the desired SDL_Scancode to query.
  195. * \param modstate the modifier state to use when translating the scancode to
  196. * a keycode.
  197. * \returns the SDL_Keycode that corresponds to the given SDL_Scancode.
  198. *
  199. * \since This function is available since SDL 3.0.0.
  200. *
  201. * \sa SDL_GetDefaultKeyFromScancode
  202. * \sa SDL_GetKeyName
  203. * \sa SDL_GetScancodeFromKey
  204. */
  205. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromScancode(SDL_Scancode scancode, SDL_Keymod modstate);
  206. /**
  207. * Get the scancode corresponding to the given key code according to a default
  208. * en_US keyboard layout.
  209. *
  210. * Note that there may be multiple scancode+modifier states that can generate
  211. * this keycode, this will just return the first one found.
  212. *
  213. * \param key the desired SDL_Keycode to query.
  214. * \param modstate a pointer to the modifier state that would be used when the
  215. * scancode generates this key, may be NULL.
  216. * \returns the SDL_Scancode that corresponds to the given SDL_Keycode.
  217. *
  218. * \since This function is available since SDL 3.0.0.
  219. *
  220. * \sa SDL_GetScancodeFromKey
  221. * \sa SDL_GetScancodeName
  222. */
  223. extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetDefaultScancodeFromKey(SDL_Keycode key, SDL_Keymod *modstate);
  224. /**
  225. * Get the scancode corresponding to the given key code according to the
  226. * current keyboard layout.
  227. *
  228. * Note that there may be multiple scancode+modifier states that can generate
  229. * this keycode, this will just return the first one found.
  230. *
  231. * \param key the desired SDL_Keycode to query.
  232. * \param modstate a pointer to the modifier state that would be used when the
  233. * scancode generates this key, may be NULL.
  234. * \returns the SDL_Scancode that corresponds to the given SDL_Keycode.
  235. *
  236. * \since This function is available since SDL 3.0.0.
  237. *
  238. * \sa SDL_GetDefaultScancodeFromKey
  239. * \sa SDL_GetKeyFromScancode
  240. * \sa SDL_GetScancodeName
  241. */
  242. extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromKey(SDL_Keycode key, SDL_Keymod *modstate);
  243. /**
  244. * Set a human-readable name for a scancode.
  245. *
  246. * \param scancode the desired SDL_Scancode.
  247. * \param name the name to use for the scancode, encoded as UTF-8. The string
  248. * is not copied, so the pointer given to this function must stay
  249. * valid while SDL is being used.
  250. * \returns 0 on success or a negative error code on failure; call
  251. * SDL_GetError() for more information.
  252. *
  253. * \since This function is available since SDL 3.0.0.
  254. *
  255. * \sa SDL_GetScancodeName
  256. */
  257. extern SDL_DECLSPEC int SDLCALL SDL_SetScancodeName(SDL_Scancode scancode, const char *name);
  258. /**
  259. * Get a human-readable name for a scancode.
  260. *
  261. * This returns temporary memory which will be automatically freed later, and can be claimed with SDL_ClaimTemporaryMemory().
  262. *
  263. * **Warning**: The returned name is by design not stable across platforms,
  264. * e.g. the name for `SDL_SCANCODE_LGUI` is "Left GUI" under Linux but "Left
  265. * Windows" under Microsoft Windows, and some scancodes like
  266. * `SDL_SCANCODE_NONUSBACKSLASH` don't have any name at all. There are even
  267. * scancodes that share names, e.g. `SDL_SCANCODE_RETURN` and
  268. * `SDL_SCANCODE_RETURN2` (both called "Return"). This function is therefore
  269. * unsuitable for creating a stable cross-platform two-way mapping between
  270. * strings and scancodes.
  271. *
  272. * \param scancode the desired SDL_Scancode to query.
  273. * \returns a pointer to the name for the scancode. If the scancode doesn't
  274. * have a name this function returns an empty string ("").
  275. *
  276. * \since This function is available since SDL 3.0.0.
  277. *
  278. * \sa SDL_GetScancodeFromKey
  279. * \sa SDL_GetScancodeFromName
  280. * \sa SDL_SetScancodeName
  281. */
  282. extern SDL_DECLSPEC const char * SDLCALL SDL_GetScancodeName(SDL_Scancode scancode);
  283. /**
  284. * Get a scancode from a human-readable name.
  285. *
  286. * \param name the human-readable scancode name.
  287. * \returns the SDL_Scancode, or `SDL_SCANCODE_UNKNOWN` if the name wasn't
  288. * recognized; call SDL_GetError() for more information.
  289. *
  290. * \since This function is available since SDL 3.0.0.
  291. *
  292. * \sa SDL_GetKeyFromName
  293. * \sa SDL_GetScancodeFromKey
  294. * \sa SDL_GetScancodeName
  295. */
  296. extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromName(const char *name);
  297. /**
  298. * Get a human-readable name for a key.
  299. *
  300. * Both lowercase and uppercase alphabetic keycodes have uppercase names, e.g.
  301. * SDL_Keycode 'a' and 'A' both have the name "A".
  302. *
  303. * If the key doesn't have a name, this function returns an empty string ("").
  304. *
  305. * This returns temporary memory which will be automatically freed later, and can be claimed with SDL_ClaimTemporaryMemory().
  306. *
  307. * \param key the desired SDL_Keycode to query.
  308. * \returns a UTF-8 encoded string of the key name.
  309. *
  310. * \since This function is available since SDL 3.0.0.
  311. *
  312. * \sa SDL_GetKeyFromName
  313. * \sa SDL_GetKeyFromScancode
  314. * \sa SDL_GetScancodeFromKey
  315. */
  316. extern SDL_DECLSPEC const char * SDLCALL SDL_GetKeyName(SDL_Keycode key);
  317. /**
  318. * Get a key code from a human-readable name.
  319. *
  320. * \param name the human-readable key name.
  321. * \returns key code, or `SDLK_UNKNOWN` if the name wasn't recognized; call
  322. * SDL_GetError() for more information.
  323. *
  324. * \since This function is available since SDL 3.0.0.
  325. *
  326. * \sa SDL_GetKeyFromScancode
  327. * \sa SDL_GetKeyName
  328. * \sa SDL_GetScancodeFromName
  329. */
  330. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromName(const char *name);
  331. /**
  332. * Start accepting Unicode text input events in a window.
  333. *
  334. * This function will enable text input (SDL_EVENT_TEXT_INPUT and
  335. * SDL_EVENT_TEXT_EDITING events) in the specified window. Please use this
  336. * function paired with SDL_StopTextInput().
  337. *
  338. * Text input events are not received by default.
  339. *
  340. * On some platforms using this function shows the screen keyboard.
  341. *
  342. * \param window the window to enable text input.
  343. * \returns 0 on success or a negative error code on failure; call
  344. * SDL_GetError() for more information.
  345. *
  346. * \since This function is available since SDL 3.0.0.
  347. *
  348. * \sa SDL_SetTextInputArea
  349. * \sa SDL_StopTextInput
  350. * \sa SDL_TextInputActive
  351. */
  352. extern SDL_DECLSPEC int SDLCALL SDL_StartTextInput(SDL_Window *window);
  353. /**
  354. * Check whether or not Unicode text input events are enabled for a window.
  355. *
  356. * \param window the window to check.
  357. * \returns SDL_TRUE if text input events are enabled else SDL_FALSE.
  358. *
  359. * \since This function is available since SDL 3.0.0.
  360. *
  361. * \sa SDL_StartTextInput
  362. */
  363. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_TextInputActive(SDL_Window *window);
  364. /**
  365. * Stop receiving any text input events in a window.
  366. *
  367. * If SDL_StartTextInput() showed the screen keyboard, this function will hide
  368. * it.
  369. *
  370. * \param window the window to disable text input.
  371. * \returns 0 on success or a negative error code on failure; call
  372. * SDL_GetError() for more information.
  373. *
  374. * \since This function is available since SDL 3.0.0.
  375. *
  376. * \sa SDL_StartTextInput
  377. */
  378. extern SDL_DECLSPEC int SDLCALL SDL_StopTextInput(SDL_Window *window);
  379. /**
  380. * Dismiss the composition window/IME without disabling the subsystem.
  381. *
  382. * \param window the window to affect.
  383. * \returns 0 on success or a negative error code on failure; call
  384. * SDL_GetError() for more information.
  385. *
  386. * \since This function is available since SDL 3.0.0.
  387. *
  388. * \sa SDL_StartTextInput
  389. * \sa SDL_StopTextInput
  390. */
  391. extern SDL_DECLSPEC int SDLCALL SDL_ClearComposition(SDL_Window *window);
  392. /**
  393. * Set the area used to type Unicode text input.
  394. *
  395. * Native input methods may place a window with word suggestions near the
  396. * cursor, without covering the text being entered.
  397. *
  398. * \param window the window for which to set the text input area.
  399. * \param rect the SDL_Rect representing the text input area, in window
  400. * coordinates, or NULL to clear it.
  401. * \param cursor the offset of the current cursor location relative to
  402. * `rect->x`, in window coordinates.
  403. * \returns 0 on success or a negative error code on failure; call
  404. * SDL_GetError() for more information.
  405. *
  406. * \since This function is available since SDL 3.0.0.
  407. *
  408. * \sa SDL_GetTextInputArea
  409. * \sa SDL_StartTextInput
  410. */
  411. extern SDL_DECLSPEC int SDLCALL SDL_SetTextInputArea(SDL_Window *window, const SDL_Rect *rect, int cursor);
  412. /**
  413. * Get the area used to type Unicode text input.
  414. *
  415. * This returns the values previously set by SDL_SetTextInputArea().
  416. *
  417. * \param window the window for which to query the text input area.
  418. * \param rect a pointer to an SDL_Rect filled in with the text input area,
  419. * may be NULL.
  420. * \param cursor a pointer to the offset of the current cursor location
  421. * relative to `rect->x`, may be NULL.
  422. * \returns 0 on success or a negative error code on failure; call
  423. * SDL_GetError() for more information.
  424. *
  425. * \since This function is available since SDL 3.0.0.
  426. *
  427. * \sa SDL_SetTextInputArea
  428. */
  429. extern SDL_DECLSPEC int SDLCALL SDL_GetTextInputArea(SDL_Window *window, SDL_Rect *rect, int *cursor);
  430. /**
  431. * Check whether the platform has screen keyboard support.
  432. *
  433. * \returns SDL_TRUE if the platform has some screen keyboard support or
  434. * SDL_FALSE if not.
  435. *
  436. * \since This function is available since SDL 3.0.0.
  437. *
  438. * \sa SDL_StartTextInput
  439. * \sa SDL_ScreenKeyboardShown
  440. */
  441. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasScreenKeyboardSupport(void);
  442. /**
  443. * Check whether the screen keyboard is shown for given window.
  444. *
  445. * \param window the window for which screen keyboard should be queried.
  446. * \returns SDL_TRUE if screen keyboard is shown or SDL_FALSE if not.
  447. *
  448. * \since This function is available since SDL 3.0.0.
  449. *
  450. * \sa SDL_HasScreenKeyboardSupport
  451. */
  452. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ScreenKeyboardShown(SDL_Window *window);
  453. /* Ends C function definitions when using C++ */
  454. #ifdef __cplusplus
  455. }
  456. #endif
  457. #include <SDL3/SDL_close_code.h>
  458. #endif /* SDL_keyboard_h_ */