SDL_keyboard.h 16 KB

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