SDL_keyboard.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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_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
  66. * can be claimed with SDL_ClaimTemporaryMemory().
  67. *
  68. * \param count a pointer filled in with the number of keyboards returned, may
  69. * be NULL.
  70. * \returns a 0 terminated array of keyboards instance IDs or NULL on failure;
  71. * call SDL_GetError() for more information.
  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 const 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. * This returns temporary memory which will be automatically freed later, and
  85. * can be claimed with SDL_ClaimTemporaryMemory().
  86. *
  87. * \param instance_id the keyboard instance ID.
  88. * \returns the name of the selected keyboard or NULL on failure; call
  89. * SDL_GetError() for more information.
  90. *
  91. * \since This function is available since SDL 3.0.0.
  92. *
  93. * \sa SDL_GetKeyboards
  94. */
  95. extern SDL_DECLSPEC const char * SDLCALL SDL_GetKeyboardNameForID(SDL_KeyboardID instance_id);
  96. /**
  97. * Query the window which currently has keyboard focus.
  98. *
  99. * \returns the window with keyboard focus.
  100. *
  101. * \since This function is available since SDL 3.0.0.
  102. */
  103. extern SDL_DECLSPEC SDL_Window * SDLCALL SDL_GetKeyboardFocus(void);
  104. /**
  105. * Get a snapshot of the current state of the keyboard.
  106. *
  107. * The pointer returned is a pointer to an internal SDL array. It will be
  108. * valid for the whole lifetime of the application and should not be freed by
  109. * the caller.
  110. *
  111. * A array element with a value of 1 means that the key is pressed and a value
  112. * of 0 means that it is not. Indexes into this array are obtained by using
  113. * SDL_Scancode values.
  114. *
  115. * Use SDL_PumpEvents() to update the state array.
  116. *
  117. * This function gives you the current state after all events have been
  118. * processed, so if a key or button has been pressed and released before you
  119. * process events, then the pressed state will never show up in the
  120. * SDL_GetKeyboardState() calls.
  121. *
  122. * Note: This function doesn't take into account whether shift has been
  123. * pressed or not.
  124. *
  125. * \param numkeys if non-NULL, receives the length of the returned array.
  126. * \returns a pointer to an array of key states.
  127. *
  128. * \since This function is available since SDL 3.0.0.
  129. *
  130. * \sa SDL_PumpEvents
  131. * \sa SDL_ResetKeyboard
  132. */
  133. extern SDL_DECLSPEC const Uint8 * SDLCALL SDL_GetKeyboardState(int *numkeys);
  134. /**
  135. * Clear the state of the keyboard.
  136. *
  137. * This function will generate key up events for all pressed keys.
  138. *
  139. * \since This function is available since SDL 3.0.0.
  140. *
  141. * \sa SDL_GetKeyboardState
  142. */
  143. extern SDL_DECLSPEC void SDLCALL SDL_ResetKeyboard(void);
  144. /**
  145. * Get the current key modifier state for the keyboard.
  146. *
  147. * \returns an OR'd combination of the modifier keys for the keyboard. See
  148. * SDL_Keymod for details.
  149. *
  150. * \since This function is available since SDL 3.0.0.
  151. *
  152. * \sa SDL_GetKeyboardState
  153. * \sa SDL_SetModState
  154. */
  155. extern SDL_DECLSPEC SDL_Keymod SDLCALL SDL_GetModState(void);
  156. /**
  157. * Set the current key modifier state for the keyboard.
  158. *
  159. * The inverse of SDL_GetModState(), SDL_SetModState() allows you to impose
  160. * modifier key states on your application. Simply pass your desired modifier
  161. * states into `modstate`. This value may be a bitwise, OR'd combination of
  162. * SDL_Keymod values.
  163. *
  164. * This does not change the keyboard state, only the key modifier flags that
  165. * SDL reports.
  166. *
  167. * \param modstate the desired SDL_Keymod for the keyboard.
  168. *
  169. * \since This function is available since SDL 3.0.0.
  170. *
  171. * \sa SDL_GetModState
  172. */
  173. extern SDL_DECLSPEC void SDLCALL SDL_SetModState(SDL_Keymod modstate);
  174. /**
  175. * Get the key code corresponding to the given scancode according to a default
  176. * en_US keyboard layout.
  177. *
  178. * See SDL_Keycode for details.
  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. * \returns the SDL_Keycode that corresponds to the given SDL_Scancode.
  184. *
  185. * \since This function is available since SDL 3.0.0.
  186. *
  187. * \sa SDL_GetKeyName
  188. * \sa SDL_GetScancodeFromKey
  189. */
  190. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetDefaultKeyFromScancode(SDL_Scancode scancode, SDL_Keymod modstate);
  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. * \param modstate the modifier state to use when translating the scancode to
  199. * a keycode.
  200. * \returns the SDL_Keycode that corresponds to the given SDL_Scancode.
  201. *
  202. * \since This function is available since SDL 3.0.0.
  203. *
  204. * \sa SDL_GetDefaultKeyFromScancode
  205. * \sa SDL_GetKeyName
  206. * \sa SDL_GetScancodeFromKey
  207. */
  208. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromScancode(SDL_Scancode scancode, SDL_Keymod modstate);
  209. /**
  210. * Get the scancode corresponding to the given key code according to a default
  211. * en_US keyboard layout.
  212. *
  213. * Note that there may be multiple scancode+modifier states that can generate
  214. * this keycode, this will just return the first one found.
  215. *
  216. * \param key the desired SDL_Keycode to query.
  217. * \param modstate a pointer to the modifier state that would be used when the
  218. * scancode generates this key, may be NULL.
  219. * \returns the SDL_Scancode that corresponds to the given SDL_Keycode.
  220. *
  221. * \since This function is available since SDL 3.0.0.
  222. *
  223. * \sa SDL_GetScancodeFromKey
  224. * \sa SDL_GetScancodeName
  225. */
  226. extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetDefaultScancodeFromKey(SDL_Keycode key, SDL_Keymod *modstate);
  227. /**
  228. * Get the scancode corresponding to the given key code according to the
  229. * current keyboard layout.
  230. *
  231. * Note that there may be multiple scancode+modifier states that can generate
  232. * this keycode, this will just return the first one found.
  233. *
  234. * \param key the desired SDL_Keycode to query.
  235. * \param modstate a pointer to the modifier state that would be used when the
  236. * scancode generates this key, may be NULL.
  237. * \returns the SDL_Scancode that corresponds to the given SDL_Keycode.
  238. *
  239. * \since This function is available since SDL 3.0.0.
  240. *
  241. * \sa SDL_GetDefaultScancodeFromKey
  242. * \sa SDL_GetKeyFromScancode
  243. * \sa SDL_GetScancodeName
  244. */
  245. extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromKey(SDL_Keycode key, SDL_Keymod *modstate);
  246. /**
  247. * Set a human-readable name for a scancode.
  248. *
  249. * \param scancode the desired SDL_Scancode.
  250. * \param name the name to use for the scancode, encoded as UTF-8. The string
  251. * is not copied, so the pointer given to this function must stay
  252. * valid while SDL is being used.
  253. * \returns 0 on success or a negative error code on failure; call
  254. * SDL_GetError() for more information.
  255. *
  256. * \since This function is available since SDL 3.0.0.
  257. *
  258. * \sa SDL_GetScancodeName
  259. */
  260. extern SDL_DECLSPEC int SDLCALL SDL_SetScancodeName(SDL_Scancode scancode, const char *name);
  261. /**
  262. * Get a human-readable name for a scancode.
  263. *
  264. * This returns temporary memory which will be automatically freed later, and
  265. * can be claimed with SDL_ClaimTemporaryMemory().
  266. *
  267. * **Warning**: The returned name is by design not stable across platforms,
  268. * e.g. the name for `SDL_SCANCODE_LGUI` is "Left GUI" under Linux but "Left
  269. * Windows" under Microsoft Windows, and some scancodes like
  270. * `SDL_SCANCODE_NONUSBACKSLASH` don't have any name at all. There are even
  271. * scancodes that share names, e.g. `SDL_SCANCODE_RETURN` and
  272. * `SDL_SCANCODE_RETURN2` (both called "Return"). This function is therefore
  273. * unsuitable for creating a stable cross-platform two-way mapping between
  274. * strings and scancodes.
  275. *
  276. * \param scancode the desired SDL_Scancode to query.
  277. * \returns a pointer to the name for the scancode. If the scancode doesn't
  278. * have a name this function returns an empty string ("").
  279. *
  280. * \since This function is available since SDL 3.0.0.
  281. *
  282. * \sa SDL_GetScancodeFromKey
  283. * \sa SDL_GetScancodeFromName
  284. * \sa SDL_SetScancodeName
  285. */
  286. extern SDL_DECLSPEC const char * SDLCALL SDL_GetScancodeName(SDL_Scancode scancode);
  287. /**
  288. * Get a scancode from a human-readable name.
  289. *
  290. * \param name the human-readable scancode name.
  291. * \returns the SDL_Scancode, or `SDL_SCANCODE_UNKNOWN` if the name wasn't
  292. * recognized; call SDL_GetError() for more information.
  293. *
  294. * \since This function is available since SDL 3.0.0.
  295. *
  296. * \sa SDL_GetKeyFromName
  297. * \sa SDL_GetScancodeFromKey
  298. * \sa SDL_GetScancodeName
  299. */
  300. extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromName(const char *name);
  301. /**
  302. * Get a human-readable name for a key.
  303. *
  304. * Both lowercase and uppercase alphabetic keycodes have uppercase names, e.g.
  305. * SDL_Keycode 'a' and 'A' both have the name "A".
  306. *
  307. * If the key doesn't have a name, this function returns an empty string ("").
  308. *
  309. * This returns temporary memory which will be automatically freed later, and
  310. * can be claimed with SDL_ClaimTemporaryMemory().
  311. *
  312. * \param key the desired SDL_Keycode to query.
  313. * \returns a UTF-8 encoded string of the key name.
  314. *
  315. * \since This function is available since SDL 3.0.0.
  316. *
  317. * \sa SDL_GetKeyFromName
  318. * \sa SDL_GetKeyFromScancode
  319. * \sa SDL_GetScancodeFromKey
  320. */
  321. extern SDL_DECLSPEC const char * SDLCALL SDL_GetKeyName(SDL_Keycode key);
  322. /**
  323. * Get a key code from a human-readable name.
  324. *
  325. * \param name the human-readable key name.
  326. * \returns key code, or `SDLK_UNKNOWN` if the name wasn't recognized; call
  327. * SDL_GetError() for more information.
  328. *
  329. * \since This function is available since SDL 3.0.0.
  330. *
  331. * \sa SDL_GetKeyFromScancode
  332. * \sa SDL_GetKeyName
  333. * \sa SDL_GetScancodeFromName
  334. */
  335. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromName(const char *name);
  336. /**
  337. * Start accepting Unicode text input events in a window.
  338. *
  339. * This function will enable text input (SDL_EVENT_TEXT_INPUT and
  340. * SDL_EVENT_TEXT_EDITING events) in the specified window. Please use this
  341. * function paired with SDL_StopTextInput().
  342. *
  343. * Text input events are not received by default.
  344. *
  345. * On some platforms using this function shows the screen keyboard.
  346. *
  347. * \param window the window to enable text input.
  348. * \returns 0 on success or a negative error code on failure; call
  349. * SDL_GetError() for more information.
  350. *
  351. * \since This function is available since SDL 3.0.0.
  352. *
  353. * \sa SDL_SetTextInputArea
  354. * \sa SDL_StopTextInput
  355. * \sa SDL_TextInputActive
  356. */
  357. extern SDL_DECLSPEC int SDLCALL SDL_StartTextInput(SDL_Window *window);
  358. /**
  359. * Check whether or not Unicode text input events are enabled for a window.
  360. *
  361. * \param window the window to check.
  362. * \returns SDL_TRUE if text input events are enabled else SDL_FALSE.
  363. *
  364. * \since This function is available since SDL 3.0.0.
  365. *
  366. * \sa SDL_StartTextInput
  367. */
  368. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_TextInputActive(SDL_Window *window);
  369. /**
  370. * Stop receiving any text input events in a window.
  371. *
  372. * If SDL_StartTextInput() showed the screen keyboard, this function will hide
  373. * it.
  374. *
  375. * \param window the window to disable text input.
  376. * \returns 0 on success or a negative error code on failure; call
  377. * SDL_GetError() for more information.
  378. *
  379. * \since This function is available since SDL 3.0.0.
  380. *
  381. * \sa SDL_StartTextInput
  382. */
  383. extern SDL_DECLSPEC int SDLCALL SDL_StopTextInput(SDL_Window *window);
  384. /**
  385. * Dismiss the composition window/IME without disabling the subsystem.
  386. *
  387. * \param window the window to affect.
  388. * \returns 0 on success or a negative error code on failure; call
  389. * SDL_GetError() for more information.
  390. *
  391. * \since This function is available since SDL 3.0.0.
  392. *
  393. * \sa SDL_StartTextInput
  394. * \sa SDL_StopTextInput
  395. */
  396. extern SDL_DECLSPEC int SDLCALL SDL_ClearComposition(SDL_Window *window);
  397. /**
  398. * Set the area used to type Unicode text input.
  399. *
  400. * Native input methods may place a window with word suggestions near the
  401. * cursor, without covering the text being entered.
  402. *
  403. * \param window the window for which to set the text input area.
  404. * \param rect the SDL_Rect representing the text input area, in window
  405. * coordinates, or NULL to clear it.
  406. * \param cursor the offset of the current cursor location relative to
  407. * `rect->x`, in window coordinates.
  408. * \returns 0 on success or a negative error code on failure; call
  409. * SDL_GetError() for more information.
  410. *
  411. * \since This function is available since SDL 3.0.0.
  412. *
  413. * \sa SDL_GetTextInputArea
  414. * \sa SDL_StartTextInput
  415. */
  416. extern SDL_DECLSPEC int SDLCALL SDL_SetTextInputArea(SDL_Window *window, const SDL_Rect *rect, int cursor);
  417. /**
  418. * Get the area used to type Unicode text input.
  419. *
  420. * This returns the values previously set by SDL_SetTextInputArea().
  421. *
  422. * \param window the window for which to query the text input area.
  423. * \param rect a pointer to an SDL_Rect filled in with the text input area,
  424. * may be NULL.
  425. * \param cursor a pointer to the offset of the current cursor location
  426. * relative to `rect->x`, may be NULL.
  427. * \returns 0 on success or a negative error code on failure; call
  428. * SDL_GetError() for more information.
  429. *
  430. * \since This function is available since SDL 3.0.0.
  431. *
  432. * \sa SDL_SetTextInputArea
  433. */
  434. extern SDL_DECLSPEC int SDLCALL SDL_GetTextInputArea(SDL_Window *window, SDL_Rect *rect, int *cursor);
  435. /**
  436. * Check whether the platform has screen keyboard support.
  437. *
  438. * \returns SDL_TRUE if the platform has some screen keyboard support or
  439. * SDL_FALSE if not.
  440. *
  441. * \since This function is available since SDL 3.0.0.
  442. *
  443. * \sa SDL_StartTextInput
  444. * \sa SDL_ScreenKeyboardShown
  445. */
  446. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasScreenKeyboardSupport(void);
  447. /**
  448. * Check whether the screen keyboard is shown for given window.
  449. *
  450. * \param window the window for which screen keyboard should be queried.
  451. * \returns SDL_TRUE if screen keyboard is shown or SDL_FALSE if not.
  452. *
  453. * \since This function is available since SDL 3.0.0.
  454. *
  455. * \sa SDL_HasScreenKeyboardSupport
  456. */
  457. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ScreenKeyboardShown(SDL_Window *window);
  458. /* Ends C function definitions when using C++ */
  459. #ifdef __cplusplus
  460. }
  461. #endif
  462. #include <SDL3/SDL_close_code.h>
  463. #endif /* SDL_keyboard_h_ */