SDL_keyboard.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. * Include file for SDL keyboard event handling
  22. */
  23. #ifndef SDL_keyboard_h_
  24. #define SDL_keyboard_h_
  25. #include "SDL_stdinc.h"
  26. #include "SDL_error.h"
  27. #include "SDL_keycode.h"
  28. #include "SDL_video.h"
  29. #include "begin_code.h"
  30. /* Set up for C function definitions, even when using C++ */
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /**
  35. * The SDL keysym structure, used in key events.
  36. *
  37. * If you are looking for translated character input, see the SDL_TEXTINPUT
  38. * event.
  39. */
  40. typedef struct SDL_Keysym
  41. {
  42. SDL_Scancode scancode; /**< SDL physical key code - see SDL_Scancode for details */
  43. SDL_Keycode sym; /**< SDL virtual key code - see SDL_Keycode for details */
  44. Uint16 mod; /**< current key modifiers */
  45. Uint32 unused;
  46. } SDL_Keysym;
  47. /* Function prototypes */
  48. /**
  49. * Query the window which currently has keyboard focus.
  50. *
  51. * \returns the window with keyboard focus.
  52. *
  53. * \since This function is available since SDL 2.0.0.
  54. */
  55. extern DECLSPEC SDL_Window * SDLCALL SDL_GetKeyboardFocus(void);
  56. /**
  57. * Get a snapshot of the current state of the keyboard.
  58. *
  59. * The pointer returned is a pointer to an internal SDL array. It will be
  60. * valid for the whole lifetime of the application and should not be freed by
  61. * the caller.
  62. *
  63. * A array element with a value of 1 means that the key is pressed and a value
  64. * of 0 means that it is not. Indexes into this array are obtained by using
  65. * SDL_Scancode values.
  66. *
  67. * Use SDL_PumpEvents() to update the state array.
  68. *
  69. * This function gives you the current state after all events have been
  70. * processed, so if a key or button has been pressed and released before you
  71. * process events, then the pressed state will never show up in the
  72. * SDL_GetKeyboardState() calls.
  73. *
  74. * Note: This function doesn't take into account whether shift has been
  75. * pressed or not.
  76. *
  77. * \param numkeys if non-NULL, receives the length of the returned array.
  78. * \returns a pointer to an array of key states.
  79. *
  80. * \since This function is available since SDL 2.0.0.
  81. *
  82. * \sa SDL_PumpEvents
  83. * \sa SDL_ResetKeyboard
  84. */
  85. extern DECLSPEC const Uint8 *SDLCALL SDL_GetKeyboardState(int *numkeys);
  86. /**
  87. * Clear the state of the keyboard
  88. *
  89. * This function will generate key up events for all pressed keys.
  90. *
  91. * \since This function is available since SDL 2.24.0.
  92. *
  93. * \sa SDL_GetKeyboardState
  94. */
  95. extern DECLSPEC void SDLCALL SDL_ResetKeyboard(void);
  96. /**
  97. * Get the current key modifier state for the keyboard.
  98. *
  99. * \returns an OR'd combination of the modifier keys for the keyboard. See
  100. * SDL_Keymod for details.
  101. *
  102. * \since This function is available since SDL 2.0.0.
  103. *
  104. * \sa SDL_GetKeyboardState
  105. * \sa SDL_SetModState
  106. */
  107. extern DECLSPEC SDL_Keymod SDLCALL SDL_GetModState(void);
  108. /**
  109. * Set the current key modifier state for the keyboard.
  110. *
  111. * The inverse of SDL_GetModState(), SDL_SetModState() allows you to impose
  112. * modifier key states on your application. Simply pass your desired modifier
  113. * states into `modstate`. This value may be a bitwise, OR'd combination of
  114. * SDL_Keymod values.
  115. *
  116. * This does not change the keyboard state, only the key modifier flags that
  117. * SDL reports.
  118. *
  119. * \param modstate the desired SDL_Keymod for the keyboard.
  120. *
  121. * \since This function is available since SDL 2.0.0.
  122. *
  123. * \sa SDL_GetModState
  124. */
  125. extern DECLSPEC void SDLCALL SDL_SetModState(SDL_Keymod modstate);
  126. /**
  127. * Get the key code corresponding to the given scancode according to the
  128. * current keyboard layout.
  129. *
  130. * See SDL_Keycode for details.
  131. *
  132. * \param scancode the desired SDL_Scancode to query.
  133. * \returns the SDL_Keycode that corresponds to the given SDL_Scancode.
  134. *
  135. * \since This function is available since SDL 2.0.0.
  136. *
  137. * \sa SDL_GetKeyName
  138. * \sa SDL_GetScancodeFromKey
  139. */
  140. extern DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromScancode(SDL_Scancode scancode);
  141. /**
  142. * Get the scancode corresponding to the given key code according to the
  143. * current keyboard layout.
  144. *
  145. * See SDL_Scancode for details.
  146. *
  147. * \param key the desired SDL_Keycode to query.
  148. * \returns the SDL_Scancode that corresponds to the given SDL_Keycode.
  149. *
  150. * \since This function is available since SDL 2.0.0.
  151. *
  152. * \sa SDL_GetKeyFromScancode
  153. * \sa SDL_GetScancodeName
  154. */
  155. extern DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromKey(SDL_Keycode key);
  156. /**
  157. * Get a human-readable name for a scancode.
  158. *
  159. * See SDL_Scancode for details.
  160. *
  161. * **Warning**: The returned name is by design not stable across platforms,
  162. * e.g. the name for `SDL_SCANCODE_LGUI` is "Left GUI" under Linux but "Left
  163. * Windows" under Microsoft Windows, and some scancodes like
  164. * `SDL_SCANCODE_NONUSBACKSLASH` don't have any name at all. There are even
  165. * scancodes that share names, e.g. `SDL_SCANCODE_RETURN` and
  166. * `SDL_SCANCODE_RETURN2` (both called "Return"). This function is therefore
  167. * unsuitable for creating a stable cross-platform two-way mapping between
  168. * strings and scancodes.
  169. *
  170. * \param scancode the desired SDL_Scancode to query.
  171. * \returns a pointer to the name for the scancode. If the scancode doesn't
  172. * have a name this function returns an empty string ("").
  173. *
  174. * \since This function is available since SDL 2.0.0.
  175. *
  176. * \sa SDL_GetScancodeFromKey
  177. * \sa SDL_GetScancodeFromName
  178. */
  179. extern DECLSPEC const char *SDLCALL SDL_GetScancodeName(SDL_Scancode scancode);
  180. /**
  181. * Get a scancode from a human-readable name.
  182. *
  183. * \param name the human-readable scancode name.
  184. * \returns the SDL_Scancode, or `SDL_SCANCODE_UNKNOWN` if the name wasn't
  185. * recognized; call SDL_GetError() for more information.
  186. *
  187. * \since This function is available since SDL 2.0.0.
  188. *
  189. * \sa SDL_GetKeyFromName
  190. * \sa SDL_GetScancodeFromKey
  191. * \sa SDL_GetScancodeName
  192. */
  193. extern DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromName(const char *name);
  194. /**
  195. * Get a human-readable name for a key.
  196. *
  197. * See SDL_Scancode and SDL_Keycode for details.
  198. *
  199. * \param key the desired SDL_Keycode to query.
  200. * \returns a pointer to a UTF-8 string that stays valid at least until the
  201. * next call to this function. If you need it around any longer, you
  202. * must copy it. If the key doesn't have a name, this function
  203. * returns an empty string ("").
  204. *
  205. * \since This function is available since SDL 2.0.0.
  206. *
  207. * \sa SDL_GetKeyFromName
  208. * \sa SDL_GetKeyFromScancode
  209. * \sa SDL_GetScancodeFromKey
  210. */
  211. extern DECLSPEC const char *SDLCALL SDL_GetKeyName(SDL_Keycode key);
  212. /**
  213. * Get a key code from a human-readable name.
  214. *
  215. * \param name the human-readable key name.
  216. * \returns key code, or `SDLK_UNKNOWN` if the name wasn't recognized; call
  217. * SDL_GetError() for more information.
  218. *
  219. * \since This function is available since SDL 2.0.0.
  220. *
  221. * \sa SDL_GetKeyFromScancode
  222. * \sa SDL_GetKeyName
  223. * \sa SDL_GetScancodeFromName
  224. */
  225. extern DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromName(const char *name);
  226. /**
  227. * Start accepting Unicode text input events.
  228. *
  229. * This function will start accepting Unicode text input events in the focused
  230. * SDL window, and start emitting SDL_TextInputEvent (SDL_TEXTINPUT) and
  231. * SDL_TextEditingEvent (SDL_TEXTEDITING) events. Please use this function in
  232. * pair with SDL_StopTextInput().
  233. *
  234. * On some platforms using this function activates the screen keyboard.
  235. *
  236. * On desktop platforms, SDL_StartTextInput() is implicitly called on SDL
  237. * video subsystem initialization which will cause SDL_TextInputEvent and
  238. * SDL_TextEditingEvent to begin emitting.
  239. *
  240. * \since This function is available since SDL 2.0.0.
  241. *
  242. * \sa SDL_SetTextInputRect
  243. * \sa SDL_StopTextInput
  244. */
  245. extern DECLSPEC void SDLCALL SDL_StartTextInput(void);
  246. /**
  247. * Check whether or not Unicode text input events are enabled.
  248. *
  249. * \returns SDL_TRUE if text input events are enabled else SDL_FALSE.
  250. *
  251. * \since This function is available since SDL 2.0.0.
  252. *
  253. * \sa SDL_StartTextInput
  254. */
  255. extern DECLSPEC SDL_bool SDLCALL SDL_IsTextInputActive(void);
  256. /**
  257. * Stop receiving any text input events.
  258. *
  259. * \since This function is available since SDL 2.0.0.
  260. *
  261. * \sa SDL_StartTextInput
  262. */
  263. extern DECLSPEC void SDLCALL SDL_StopTextInput(void);
  264. /**
  265. * Dismiss the composition window/IME without disabling the subsystem.
  266. *
  267. * \since This function is available since SDL 2.0.22.
  268. *
  269. * \sa SDL_StartTextInput
  270. * \sa SDL_StopTextInput
  271. */
  272. extern DECLSPEC void SDLCALL SDL_ClearComposition(void);
  273. /**
  274. * Returns if an IME Composite or Candidate window is currently shown.
  275. *
  276. * \since This function is available since SDL 2.0.22.
  277. */
  278. extern DECLSPEC SDL_bool SDLCALL SDL_IsTextInputShown(void);
  279. /**
  280. * Set the rectangle used to type Unicode text inputs.
  281. *
  282. * Native input methods will place a window with word suggestions near it,
  283. * without covering the text being inputted.
  284. *
  285. * To start text input in a given location, this function is intended to be
  286. * called before SDL_StartTextInput, although some platforms support moving
  287. * the rectangle even while text input (and a composition) is active.
  288. *
  289. * Note: If you want to use the system native IME window, try setting hint
  290. * **SDL_HINT_IME_SHOW_UI** to **1**, otherwise this function won't give you
  291. * any feedback.
  292. *
  293. * \param rect the SDL_Rect structure representing the rectangle to receive
  294. * text (ignored if NULL).
  295. *
  296. * \since This function is available since SDL 2.0.0.
  297. *
  298. * \sa SDL_StartTextInput
  299. */
  300. extern DECLSPEC void SDLCALL SDL_SetTextInputRect(const SDL_Rect *rect);
  301. /**
  302. * Check whether the platform has screen keyboard support.
  303. *
  304. * \returns SDL_TRUE if the platform has some screen keyboard support or
  305. * SDL_FALSE if not.
  306. *
  307. * \since This function is available since SDL 2.0.0.
  308. *
  309. * \sa SDL_StartTextInput
  310. * \sa SDL_IsScreenKeyboardShown
  311. */
  312. extern DECLSPEC SDL_bool SDLCALL SDL_HasScreenKeyboardSupport(void);
  313. /**
  314. * Check whether the screen keyboard is shown for given window.
  315. *
  316. * \param window the window for which screen keyboard should be queried.
  317. * \returns SDL_TRUE if screen keyboard is shown or SDL_FALSE if not.
  318. *
  319. * \since This function is available since SDL 2.0.0.
  320. *
  321. * \sa SDL_HasScreenKeyboardSupport
  322. */
  323. extern DECLSPEC SDL_bool SDLCALL SDL_IsScreenKeyboardShown(SDL_Window *window);
  324. /* Ends C function definitions when using C++ */
  325. #ifdef __cplusplus
  326. }
  327. #endif
  328. #include "close_code.h"
  329. #endif /* SDL_keyboard_h_ */
  330. /* vi: set ts=4 sw=4 expandtab: */