SDL_keyboard.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /**
  47. * The SDL keysym structure, used in key events.
  48. *
  49. * If you are looking for translated character input, see the
  50. * SDL_EVENT_TEXT_INPUT event.
  51. *
  52. * \since This struct is available since SDL 3.0.0.
  53. *
  54. * \sa SDL_Scancode
  55. * \sa SDL_Keycode
  56. */
  57. typedef struct SDL_Keysym
  58. {
  59. SDL_Scancode scancode; /**< SDL physical key code - see SDL_Scancode for details */
  60. SDL_Keycode sym; /**< SDL virtual key code - see SDL_Keycode for details */
  61. SDL_Keymod mod; /**< current key modifiers */
  62. Uint16 unused;
  63. } SDL_Keysym;
  64. /* Function prototypes */
  65. /**
  66. * Return whether a keyboard is currently connected.
  67. *
  68. * \returns SDL_TRUE if a keyboard is connected, SDL_FALSE otherwise.
  69. *
  70. * \since This function is available since SDL 3.0.0.
  71. *
  72. * \sa SDL_GetKeyboards
  73. */
  74. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasKeyboard(void);
  75. /**
  76. * Get a list of currently connected keyboards.
  77. *
  78. * Note that this will include any device or virtual driver that includes
  79. * keyboard functionality, including some mice, KVM switches, motherboard
  80. * power buttons, etc. You should wait for input from a device before you
  81. * consider it actively in use.
  82. *
  83. * \param count a pointer filled in with the number of keyboards returned.
  84. * \returns a 0 terminated array of keyboards instance IDs which should be
  85. * freed with SDL_free(), or NULL on error; call SDL_GetError() for
  86. * more details.
  87. *
  88. * \since This function is available since SDL 3.0.0.
  89. *
  90. * \sa SDL_GetKeyboardInstanceName
  91. * \sa SDL_HasKeyboard
  92. */
  93. extern SDL_DECLSPEC SDL_KeyboardID *SDLCALL SDL_GetKeyboards(int *count);
  94. /**
  95. * Get the name of a keyboard.
  96. *
  97. * This function returns "" if the keyboard doesn't have a name.
  98. *
  99. * The returned string follows the SDL_GetStringRule.
  100. *
  101. * \param instance_id the keyboard instance ID.
  102. * \returns the name of the selected keyboard, or NULL on failure; call
  103. * SDL_GetError() for more information.
  104. *
  105. * \since This function is available since SDL 3.0.0.
  106. *
  107. * \sa SDL_GetKeyboards
  108. */
  109. extern SDL_DECLSPEC const char *SDLCALL SDL_GetKeyboardInstanceName(SDL_KeyboardID instance_id);
  110. /**
  111. * Query the window which currently has keyboard focus.
  112. *
  113. * \returns the window with keyboard focus.
  114. *
  115. * \since This function is available since SDL 3.0.0.
  116. */
  117. extern SDL_DECLSPEC SDL_Window * SDLCALL SDL_GetKeyboardFocus(void);
  118. /**
  119. * Get a snapshot of the current state of the keyboard.
  120. *
  121. * The pointer returned is a pointer to an internal SDL array. It will be
  122. * valid for the whole lifetime of the application and should not be freed by
  123. * the caller.
  124. *
  125. * A array element with a value of 1 means that the key is pressed and a value
  126. * of 0 means that it is not. Indexes into this array are obtained by using
  127. * SDL_Scancode values.
  128. *
  129. * Use SDL_PumpEvents() to update the state array.
  130. *
  131. * This function gives you the current state after all events have been
  132. * processed, so if a key or button has been pressed and released before you
  133. * process events, then the pressed state will never show up in the
  134. * SDL_GetKeyboardState() calls.
  135. *
  136. * Note: This function doesn't take into account whether shift has been
  137. * pressed or not.
  138. *
  139. * \param numkeys if non-NULL, receives the length of the returned array.
  140. * \returns a pointer to an array of key states.
  141. *
  142. * \since This function is available since SDL 3.0.0.
  143. *
  144. * \sa SDL_PumpEvents
  145. * \sa SDL_ResetKeyboard
  146. */
  147. extern SDL_DECLSPEC const Uint8 *SDLCALL SDL_GetKeyboardState(int *numkeys);
  148. /**
  149. * Clear the state of the keyboard.
  150. *
  151. * This function will generate key up events for all pressed keys.
  152. *
  153. * \since This function is available since SDL 3.0.0.
  154. *
  155. * \sa SDL_GetKeyboardState
  156. */
  157. extern SDL_DECLSPEC void SDLCALL SDL_ResetKeyboard(void);
  158. /**
  159. * Get the current key modifier state for the keyboard.
  160. *
  161. * \returns an OR'd combination of the modifier keys for the keyboard. See
  162. * SDL_Keymod for details.
  163. *
  164. * \since This function is available since SDL 3.0.0.
  165. *
  166. * \sa SDL_GetKeyboardState
  167. * \sa SDL_SetModState
  168. */
  169. extern SDL_DECLSPEC SDL_Keymod SDLCALL SDL_GetModState(void);
  170. /**
  171. * Set the current key modifier state for the keyboard.
  172. *
  173. * The inverse of SDL_GetModState(), SDL_SetModState() allows you to impose
  174. * modifier key states on your application. Simply pass your desired modifier
  175. * states into `modstate`. This value may be a bitwise, OR'd combination of
  176. * SDL_Keymod values.
  177. *
  178. * This does not change the keyboard state, only the key modifier flags that
  179. * SDL reports.
  180. *
  181. * \param modstate the desired SDL_Keymod for the keyboard.
  182. *
  183. * \since This function is available since SDL 3.0.0.
  184. *
  185. * \sa SDL_GetModState
  186. */
  187. extern SDL_DECLSPEC void SDLCALL SDL_SetModState(SDL_Keymod modstate);
  188. /**
  189. * Get the key code corresponding to the given scancode according to a default
  190. * en_US keyboard layout.
  191. *
  192. * See SDL_Keycode for details.
  193. *
  194. * \param scancode the desired SDL_Scancode to query.
  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_GetKeyName
  200. * \sa SDL_GetScancodeFromKey
  201. */
  202. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetDefaultKeyFromScancode(SDL_Scancode scancode);
  203. /**
  204. * Get the key code corresponding to the given scancode according to the
  205. * current keyboard layout.
  206. *
  207. * See SDL_Keycode for details.
  208. *
  209. * \param scancode the desired SDL_Scancode to query.
  210. * \returns the SDL_Keycode that corresponds to the given SDL_Scancode.
  211. *
  212. * \since This function is available since SDL 3.0.0.
  213. *
  214. * \sa SDL_GetKeyName
  215. * \sa SDL_GetScancodeFromKey
  216. */
  217. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromScancode(SDL_Scancode scancode);
  218. /**
  219. * Get the scancode corresponding to the given key code according to the
  220. * current keyboard layout.
  221. *
  222. * See SDL_Scancode for details.
  223. *
  224. * \param key the desired SDL_Keycode to query.
  225. * \returns the SDL_Scancode that corresponds to the given SDL_Keycode.
  226. *
  227. * \since This function is available since SDL 3.0.0.
  228. *
  229. * \sa SDL_GetKeyFromScancode
  230. * \sa SDL_GetScancodeName
  231. */
  232. extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromKey(SDL_Keycode key);
  233. /**
  234. * Get a human-readable name for a scancode.
  235. *
  236. * See SDL_Scancode for details.
  237. *
  238. * The returned string follows the SDL_GetStringRule.
  239. *
  240. * **Warning**: The returned name is by design not stable across platforms,
  241. * e.g. the name for `SDL_SCANCODE_LGUI` is "Left GUI" under Linux but "Left
  242. * Windows" under Microsoft Windows, and some scancodes like
  243. * `SDL_SCANCODE_NONUSBACKSLASH` don't have any name at all. There are even
  244. * scancodes that share names, e.g. `SDL_SCANCODE_RETURN` and
  245. * `SDL_SCANCODE_RETURN2` (both called "Return"). This function is therefore
  246. * unsuitable for creating a stable cross-platform two-way mapping between
  247. * strings and scancodes.
  248. *
  249. * \param scancode the desired SDL_Scancode to query.
  250. * \returns a pointer to the name for the scancode. If the scancode doesn't
  251. * have a name this function returns an empty string ("").
  252. *
  253. * \since This function is available since SDL 3.0.0.
  254. *
  255. * \sa SDL_GetScancodeFromKey
  256. * \sa SDL_GetScancodeFromName
  257. */
  258. extern SDL_DECLSPEC const char *SDLCALL SDL_GetScancodeName(SDL_Scancode scancode);
  259. /**
  260. * Get a scancode from a human-readable name.
  261. *
  262. * \param name the human-readable scancode name.
  263. * \returns the SDL_Scancode, or `SDL_SCANCODE_UNKNOWN` if the name wasn't
  264. * recognized; call SDL_GetError() for more information.
  265. *
  266. * \since This function is available since SDL 3.0.0.
  267. *
  268. * \sa SDL_GetKeyFromName
  269. * \sa SDL_GetScancodeFromKey
  270. * \sa SDL_GetScancodeName
  271. */
  272. extern SDL_DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromName(const char *name);
  273. /**
  274. * Get a human-readable name for a key.
  275. *
  276. * See SDL_Scancode and SDL_Keycode for details.
  277. *
  278. * The returned string follows the SDL_GetStringRule.
  279. *
  280. * \param key the desired SDL_Keycode to query.
  281. * \returns a pointer to a UTF-8 string that stays valid at least until the
  282. * next call to this function. If you need it around any longer, you
  283. * must copy it. If the key doesn't have a name, this function
  284. * returns an empty string ("").
  285. *
  286. * \since This function is available since SDL 3.0.0.
  287. *
  288. * \sa SDL_GetKeyFromName
  289. * \sa SDL_GetKeyFromScancode
  290. * \sa SDL_GetScancodeFromKey
  291. */
  292. extern SDL_DECLSPEC const char *SDLCALL SDL_GetKeyName(SDL_Keycode key);
  293. /**
  294. * Get a key code from a human-readable name.
  295. *
  296. * \param name the human-readable key name.
  297. * \returns key code, or `SDLK_UNKNOWN` if the name wasn't recognized; call
  298. * SDL_GetError() for more information.
  299. *
  300. * \since This function is available since SDL 3.0.0.
  301. *
  302. * \sa SDL_GetKeyFromScancode
  303. * \sa SDL_GetKeyName
  304. * \sa SDL_GetScancodeFromName
  305. */
  306. extern SDL_DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromName(const char *name);
  307. /**
  308. * Start accepting Unicode text input events.
  309. *
  310. * This function will start accepting Unicode text input events in the focused
  311. * SDL window, and start emitting SDL_TextInputEvent (SDL_EVENT_TEXT_INPUT)
  312. * and SDL_TextEditingEvent (SDL_EVENT_TEXT_EDITING) events. Please use this
  313. * function in pair with SDL_StopTextInput().
  314. *
  315. * Text input events are not received by default.
  316. *
  317. * On some platforms using this function activates the screen keyboard.
  318. *
  319. * \since This function is available since SDL 3.0.0.
  320. *
  321. * \sa SDL_SetTextInputRect
  322. * \sa SDL_StopTextInput
  323. */
  324. extern SDL_DECLSPEC void SDLCALL SDL_StartTextInput(void);
  325. /**
  326. * Check whether or not Unicode text input events are enabled.
  327. *
  328. * \returns SDL_TRUE if text input events are enabled else SDL_FALSE.
  329. *
  330. * \since This function is available since SDL 3.0.0.
  331. *
  332. * \sa SDL_StartTextInput
  333. */
  334. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_TextInputActive(void);
  335. /**
  336. * Stop receiving any text input events.
  337. *
  338. * Text input events are not received by default.
  339. *
  340. * \since This function is available since SDL 3.0.0.
  341. *
  342. * \sa SDL_StartTextInput
  343. */
  344. extern SDL_DECLSPEC void SDLCALL SDL_StopTextInput(void);
  345. /**
  346. * Dismiss the composition window/IME without disabling the subsystem.
  347. *
  348. * \since This function is available since SDL 3.0.0.
  349. *
  350. * \sa SDL_StartTextInput
  351. * \sa SDL_StopTextInput
  352. */
  353. extern SDL_DECLSPEC void SDLCALL SDL_ClearComposition(void);
  354. /**
  355. * Set the rectangle used to type Unicode text inputs.
  356. *
  357. * Native input methods will place a window with word suggestions near it,
  358. * without covering the text being inputted.
  359. *
  360. * To start text input in a given location, this function is intended to be
  361. * called before SDL_StartTextInput, although some platforms support moving
  362. * the rectangle even while text input (and a composition) is active.
  363. *
  364. * Note: If you want to use the system native IME window, try setting hint
  365. * **SDL_HINT_IME_SHOW_UI** to **1**, otherwise this function won't give you
  366. * any feedback.
  367. *
  368. * \param rect the SDL_Rect structure representing the rectangle to receive
  369. * text (ignored if NULL).
  370. * \returns 0 on success or a negative error code on failure; call
  371. * SDL_GetError() for more information.
  372. *
  373. * \since This function is available since SDL 3.0.0.
  374. *
  375. * \sa SDL_StartTextInput
  376. */
  377. extern SDL_DECLSPEC int SDLCALL SDL_SetTextInputRect(const SDL_Rect *rect);
  378. /**
  379. * Check whether the platform has screen keyboard support.
  380. *
  381. * \returns SDL_TRUE if the platform has some screen keyboard support or
  382. * SDL_FALSE if not.
  383. *
  384. * \since This function is available since SDL 3.0.0.
  385. *
  386. * \sa SDL_StartTextInput
  387. * \sa SDL_ScreenKeyboardShown
  388. */
  389. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_HasScreenKeyboardSupport(void);
  390. /**
  391. * Check whether the screen keyboard is shown for given window.
  392. *
  393. * \param window the window for which screen keyboard should be queried.
  394. * \returns SDL_TRUE if screen keyboard is shown or SDL_FALSE if not.
  395. *
  396. * \since This function is available since SDL 3.0.0.
  397. *
  398. * \sa SDL_HasScreenKeyboardSupport
  399. */
  400. extern SDL_DECLSPEC SDL_bool SDLCALL SDL_ScreenKeyboardShown(SDL_Window *window);
  401. /* Ends C function definitions when using C++ */
  402. #ifdef __cplusplus
  403. }
  404. #endif
  405. #include <SDL3/SDL_close_code.h>
  406. #endif /* SDL_keyboard_h_ */