SDL_keyboard.h 16 KB

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