SDL_keyboard.h 13 KB

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