input.dox 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*!
  2. @page input_guide Input guide
  3. @tableofcontents
  4. This guide introduces the input related functions of GLFW. For details on
  5. a specific function in this category, see the @ref input. There are also guides
  6. for the other areas of GLFW.
  7. - @ref intro_guide
  8. - @ref window_guide
  9. - @ref context_guide
  10. - @ref vulkan_guide
  11. - @ref monitor_guide
  12. GLFW provides many kinds of input. While some can only be polled, like time, or
  13. only received via callbacks, like scrolling, many provide both callbacks and
  14. polling. Callbacks are more work to use than polling but is less CPU intensive
  15. and guarantees that you do not miss state changes.
  16. All input callbacks receive a window handle. By using the
  17. [window user pointer](@ref window_userptr), you can access non-global structures
  18. or objects from your callbacks.
  19. To get a better feel for how the various events callbacks behave, run the
  20. `events` test program. It register every callback supported by GLFW and prints
  21. out all arguments provided for every event, along with time and sequence
  22. information.
  23. @section events Event processing
  24. GLFW needs to poll the window system for events both to provide input to the
  25. application and to prove to the window system that the application hasn't locked
  26. up. Event processing is normally done each frame after
  27. [buffer swapping](@ref buffer_swap). Even when you have no windows, event
  28. polling needs to be done in order to receive monitor and joystick connection
  29. events.
  30. There are three functions for processing pending events. @ref glfwPollEvents,
  31. processes only those events that have already been received and then returns
  32. immediately.
  33. @code
  34. glfwPollEvents();
  35. @endcode
  36. This is the best choice when rendering continuously, like most games do.
  37. If you only need to update the contents of the window when you receive new
  38. input, @ref glfwWaitEvents is a better choice.
  39. @code
  40. glfwWaitEvents();
  41. @endcode
  42. It puts the thread to sleep until at least one event has been received and then
  43. processes all received events. This saves a great deal of CPU cycles and is
  44. useful for, for example, editing tools. There must be at least one GLFW window
  45. for this function to sleep.
  46. If you want to wait for events but have UI elements or other tasks that need
  47. periodic updates, @ref glfwWaitEventsTimeout lets you specify a timeout.
  48. @code
  49. glfwWaitEventsTimeout(0.7);
  50. @endcode
  51. It puts the thread to sleep until at least one event has been received, or until
  52. the specified number of seconds have elapsed. It then processes any received
  53. events.
  54. If the main thread is sleeping in @ref glfwWaitEvents, you can wake it from
  55. another thread by posting an empty event to the event queue with @ref
  56. glfwPostEmptyEvent.
  57. @code
  58. glfwPostEmptyEvent();
  59. @endcode
  60. Do not assume that callbacks will _only_ be called in response to the above
  61. functions. While it is necessary to process events in one or more of the ways
  62. above, window systems that require GLFW to register callbacks of its own can
  63. pass events to GLFW in response to many window system function calls. GLFW will
  64. pass those events on to the application callbacks before returning.
  65. For example, on Windows the system function that @ref glfwSetWindowSize is
  66. implemented with will send window size events directly to the event callback
  67. that every window has and that GLFW implements for its windows. If you have set
  68. a [window size callback](@ref window_size) GLFW will call it in turn with the
  69. new size before everything returns back out of the @ref glfwSetWindowSize call.
  70. @section input_keyboard Keyboard input
  71. GLFW divides keyboard input into two categories; key events and character
  72. events. Key events relate to actual physical keyboard keys, whereas character
  73. events relate to the Unicode code points generated by pressing some of them.
  74. Keys and characters do not map 1:1. A single key press may produce several
  75. characters, and a single character may require several keys to produce. This
  76. may not be the case on your machine, but your users are likely not all using the
  77. same keyboard layout, input method or even operating system as you.
  78. @subsection input_key Key input
  79. If you wish to be notified when a physical key is pressed or released or when it
  80. repeats, set a key callback.
  81. @code
  82. glfwSetKeyCallback(window, key_callback);
  83. @endcode
  84. The callback function receives the [keyboard key](@ref keys), platform-specific
  85. scancode, key action and [modifier bits](@ref mods).
  86. @code
  87. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  88. {
  89. if (key == GLFW_KEY_E && action == GLFW_PRESS)
  90. activate_airship();
  91. }
  92. @endcode
  93. The action is one of `GLFW_PRESS`, `GLFW_REPEAT` or `GLFW_RELEASE`. The key
  94. will be `GLFW_KEY_UNKNOWN` if GLFW lacks a key token for it, for example
  95. _E-mail_ and _Play_ keys.
  96. The scancode is unique for every key, regardless of whether it has a key token.
  97. Scancodes are platform-specific but consistent over time, so keys will have
  98. different scancodes depending on the platform but they are safe to save to disk.
  99. You can query the scancode for any [named key](@ref keys) on the current
  100. platform with @ref glfwGetKeyScancode.
  101. @code
  102. const int scancode = glfwGetKeyScancode(GLFW_KEY_X);
  103. set_key_mapping(scancode, swap_weapons);
  104. @endcode
  105. The last reported state for every [named key](@ref keys) is also saved in
  106. per-window state arrays that can be polled with @ref glfwGetKey.
  107. @code
  108. int state = glfwGetKey(window, GLFW_KEY_E);
  109. if (state == GLFW_PRESS)
  110. {
  111. activate_airship();
  112. }
  113. @endcode
  114. The returned state is one of `GLFW_PRESS` or `GLFW_RELEASE`.
  115. This function only returns cached key event state. It does not poll the
  116. system for the current physical state of the key.
  117. @anchor GLFW_STICKY_KEYS
  118. Whenever you poll state, you risk missing the state change you are looking for.
  119. If a pressed key is released again before you poll its state, you will have
  120. missed the key press. The recommended solution for this is to use a
  121. key callback, but there is also the `GLFW_STICKY_KEYS` input mode.
  122. @code
  123. glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
  124. @endcode
  125. When sticky keys mode is enabled, the pollable state of a key will remain
  126. `GLFW_PRESS` until the state of that key is polled with @ref glfwGetKey. Once
  127. it has been polled, if a key release event had been processed in the meantime,
  128. the state will reset to `GLFW_RELEASE`, otherwise it will remain `GLFW_PRESS`.
  129. The `GLFW_KEY_LAST` constant holds the highest value of any
  130. [named key](@ref keys).
  131. @subsection input_char Text input
  132. GLFW supports text input in the form of a stream of
  133. [Unicode code points](https://en.wikipedia.org/wiki/Unicode), as produced by the
  134. operating system text input system. Unlike key input, text input obeys keyboard
  135. layouts and modifier keys and supports composing characters using
  136. [dead keys](https://en.wikipedia.org/wiki/Dead_key). Once received, you can
  137. encode the code points into UTF-8 or any other encoding you prefer.
  138. Because an `unsigned int` is 32 bits long on all platforms supported by GLFW,
  139. you can treat the code point argument as native endian UTF-32.
  140. There are two callbacks for receiving Unicode code points. If you wish to
  141. offer regular text input, set a character callback.
  142. @code
  143. glfwSetCharCallback(window, character_callback);
  144. @endcode
  145. The callback function receives Unicode code points for key events that would
  146. have led to regular text input and generally behaves as a standard text field on
  147. that platform.
  148. @code
  149. void character_callback(GLFWwindow* window, unsigned int codepoint)
  150. {
  151. }
  152. @endcode
  153. If you wish to receive even those Unicode code points generated with modifier
  154. key combinations that a plain text field would ignore, or just want to know
  155. exactly what modifier keys were used, set a character with modifiers callback.
  156. @code
  157. glfwSetCharModsCallback(window, charmods_callback);
  158. @endcode
  159. The callback function receives Unicode code points and
  160. [modifier bits](@ref mods).
  161. @code
  162. void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods)
  163. {
  164. }
  165. @endcode
  166. @subsection input_key_name Key names
  167. If you wish to refer to keys by name, you can query the keyboard layout
  168. dependent name of printable keys with @ref glfwGetKeyName.
  169. @code
  170. const char* key_name = glfwGetKeyName(GLFW_KEY_W, 0);
  171. show_tutorial_hint("Press %s to move forward", key_name);
  172. @endcode
  173. This function can handle both [keys and scancodes](@ref input_key). If the
  174. specified key is `GLFW_KEY_UNKNOWN` then the scancode is used, otherwise it is
  175. ignored. This matches the behavior of the key callback, meaning the callback
  176. arguments can always be passed unmodified to this function.
  177. @section input_mouse Mouse input
  178. Mouse input comes in many forms, including cursor motion, button presses and
  179. scrolling offsets. The cursor appearance can also be changed, either to
  180. a custom image or a standard cursor shape from the system theme.
  181. @subsection cursor_pos Cursor position
  182. If you wish to be notified when the cursor moves over the window, set a cursor
  183. position callback.
  184. @code
  185. glfwSetCursorPosCallback(window, cursor_pos_callback);
  186. @endcode
  187. The callback functions receives the cursor position, measured in screen
  188. coordinates but relative to the top-left corner of the window client area. On
  189. platforms that provide it, the full sub-pixel cursor position is passed on.
  190. @code
  191. static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
  192. {
  193. }
  194. @endcode
  195. The cursor position is also saved per-window and can be polled with @ref
  196. glfwGetCursorPos.
  197. @code
  198. double xpos, ypos;
  199. glfwGetCursorPos(window, &xpos, &ypos);
  200. @endcode
  201. @subsection cursor_mode Cursor mode
  202. @anchor GLFW_CURSOR
  203. The `GLFW_CURSOR` input mode provides several cursor modes for special forms of
  204. mouse motion input. By default, the cursor mode is `GLFW_CURSOR_NORMAL`,
  205. meaning the regular arrow cursor (or another cursor set with @ref glfwSetCursor)
  206. is used and cursor motion is not limited.
  207. If you wish to implement mouse motion based camera controls or other input
  208. schemes that require unlimited mouse movement, set the cursor mode to
  209. `GLFW_CURSOR_DISABLED`.
  210. @code
  211. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  212. @endcode
  213. This will hide the cursor and lock it to the specified window. GLFW will then
  214. take care of all the details of cursor re-centering and offset calculation and
  215. providing the application with a virtual cursor position. This virtual position
  216. is provided normally via both the cursor position callback and through polling.
  217. @note You should not implement your own version of this functionality using
  218. other features of GLFW. It is not supported and will not work as robustly as
  219. `GLFW_CURSOR_DISABLED`.
  220. If you just wish the cursor to become hidden when it is over a window, set
  221. the cursor mode to `GLFW_CURSOR_HIDDEN`.
  222. @code
  223. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  224. @endcode
  225. This mode puts no limit on the motion of the cursor.
  226. To exit out of either of these special modes, restore the `GLFW_CURSOR_NORMAL`
  227. cursor mode.
  228. @code
  229. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  230. @endcode
  231. @subsection cursor_object Cursor objects
  232. GLFW supports creating both custom and system theme cursor images, encapsulated
  233. as @ref GLFWcursor objects. They are created with @ref glfwCreateCursor or @ref
  234. glfwCreateStandardCursor and destroyed with @ref glfwDestroyCursor, or @ref
  235. glfwTerminate, if any remain.
  236. @subsubsection cursor_custom Custom cursor creation
  237. A custom cursor is created with @ref glfwCreateCursor, which returns a handle to
  238. the created cursor object. For example, this creates a 16x16 white square
  239. cursor with the hot-spot in the upper-left corner:
  240. @code
  241. unsigned char pixels[16 * 16 * 4];
  242. memset(pixels, 0xff, sizeof(pixels));
  243. GLFWimage image;
  244. image.width = 16;
  245. image.height = 16;
  246. image.pixels = pixels;
  247. GLFWcursor* cursor = glfwCreateCursor(&image, 0, 0);
  248. @endcode
  249. If cursor creation fails, `NULL` will be returned, so it is necessary to check
  250. the return value.
  251. The image data is 32-bit, little-endian, non-premultiplied RGBA, i.e. eight bits
  252. per channel with the red channel first. The pixels are arranged canonically as
  253. sequential rows, starting from the top-left corner.
  254. @subsubsection cursor_standard Standard cursor creation
  255. A cursor with a [standard shape](@ref shapes) from the current system cursor
  256. theme can be can be created with @ref glfwCreateStandardCursor.
  257. @code
  258. GLFWcursor* cursor = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
  259. @endcode
  260. These cursor objects behave in the exact same way as those created with @ref
  261. glfwCreateCursor except that the system cursor theme provides the actual image.
  262. @subsubsection cursor_destruction Cursor destruction
  263. When a cursor is no longer needed, destroy it with @ref glfwDestroyCursor.
  264. @code
  265. glfwDestroyCursor(cursor);
  266. @endcode
  267. Cursor destruction always succeeds. If the cursor is current for any window,
  268. that window will revert to the default cursor. This does not affect the cursor
  269. mode. All remaining cursors remaining are destroyed when @ref glfwTerminate is
  270. called.
  271. @subsubsection cursor_set Cursor setting
  272. A cursor can be set as current for a window with @ref glfwSetCursor.
  273. @code
  274. glfwSetCursor(window, cursor);
  275. @endcode
  276. Once set, the cursor image will be used as long as the system cursor is over the
  277. client area of the window and the [cursor mode](@ref cursor_mode) is set
  278. to `GLFW_CURSOR_NORMAL`.
  279. A single cursor may be set for any number of windows.
  280. To revert to the default cursor, set the cursor of that window to `NULL`.
  281. @code
  282. glfwSetCursor(window, NULL);
  283. @endcode
  284. When a cursor is destroyed, any window that has it set will revert to the
  285. default cursor. This does not affect the cursor mode.
  286. @subsection cursor_enter Cursor enter/leave events
  287. If you wish to be notified when the cursor enters or leaves the client area of
  288. a window, set a cursor enter/leave callback.
  289. @code
  290. glfwSetCursorEnterCallback(window, cursor_enter_callback);
  291. @endcode
  292. The callback function receives the new classification of the cursor.
  293. @code
  294. void cursor_enter_callback(GLFWwindow* window, int entered)
  295. {
  296. if (entered)
  297. {
  298. // The cursor entered the client area of the window
  299. }
  300. else
  301. {
  302. // The cursor left the client area of the window
  303. }
  304. }
  305. @endcode
  306. @subsection input_mouse_button Mouse button input
  307. If you wish to be notified when a mouse button is pressed or released, set
  308. a mouse button callback.
  309. @code
  310. glfwSetMouseButtonCallback(window, mouse_button_callback);
  311. @endcode
  312. The callback function receives the [mouse button](@ref buttons), button action
  313. and [modifier bits](@ref mods).
  314. @code
  315. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
  316. {
  317. if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
  318. popup_menu();
  319. }
  320. @endcode
  321. The action is one of `GLFW_PRESS` or `GLFW_RELEASE`.
  322. Mouse button states for [named buttons](@ref buttons) are also saved in
  323. per-window state arrays that can be polled with @ref glfwGetMouseButton.
  324. @code
  325. int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
  326. if (state == GLFW_PRESS)
  327. {
  328. upgrade_cow();
  329. }
  330. @endcode
  331. The returned state is one of `GLFW_PRESS` or `GLFW_RELEASE`.
  332. This function only returns cached mouse button event state. It does not poll
  333. the system for the current state of the mouse button.
  334. @anchor GLFW_STICKY_MOUSE_BUTTONS
  335. Whenever you poll state, you risk missing the state change you are looking for.
  336. If a pressed mouse button is released again before you poll its state, you will have
  337. missed the button press. The recommended solution for this is to use a
  338. mouse button callback, but there is also the `GLFW_STICKY_MOUSE_BUTTONS`
  339. input mode.
  340. @code
  341. glfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, 1);
  342. @endcode
  343. When sticky mouse buttons mode is enabled, the pollable state of a mouse button
  344. will remain `GLFW_PRESS` until the state of that button is polled with @ref
  345. glfwGetMouseButton. Once it has been polled, if a mouse button release event
  346. had been processed in the meantime, the state will reset to `GLFW_RELEASE`,
  347. otherwise it will remain `GLFW_PRESS`.
  348. The `GLFW_MOUSE_BUTTON_LAST` constant holds the highest value of any
  349. [named button](@ref buttons).
  350. @subsection scrolling Scroll input
  351. If you wish to be notified when the user scrolls, whether with a mouse wheel or
  352. touchpad gesture, set a scroll callback.
  353. @code
  354. glfwSetScrollCallback(window, scroll_callback);
  355. @endcode
  356. The callback function receives two-dimensional scroll offsets.
  357. @code
  358. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
  359. {
  360. }
  361. @endcode
  362. A simple mouse wheel, being vertical, provides offsets along the Y-axis.
  363. @section joystick Joystick input
  364. The joystick functions expose connected joysticks and controllers, with both
  365. referred to as joysticks. It supports up to sixteen joysticks, ranging from
  366. `GLFW_JOYSTICK_1`, `GLFW_JOYSTICK_2` up to and including `GLFW_JOYSTICK_16` or
  367. `GLFW_JOYSTICK_LAST`. You can test whether a [joystick](@ref joysticks) is
  368. present with @ref glfwJoystickPresent.
  369. @code
  370. int present = glfwJoystickPresent(GLFW_JOYSTICK_1);
  371. @endcode
  372. When GLFW is initialized, detected joysticks are added to to the beginning of
  373. the array. Once a joystick is detected, it keeps its assigned ID until it is
  374. disconnected or the library is terminated, so as joysticks are connected and
  375. disconnected, there may appear gaps in the IDs.
  376. Joystick axis, button and hat state is updated when polled and does not require
  377. a window to be created or events to be processed. However, if you want joystick
  378. connection and disconnection events reliably delivered to the
  379. [joystick callback](@ref joystick_event) then you must
  380. [process events](@ref events).
  381. To see all the properties of all connected joysticks in real-time, run the
  382. `joysticks` test program.
  383. @subsection joystick_axis Joystick axis states
  384. The positions of all axes of a joystick are returned by @ref
  385. glfwGetJoystickAxes. See the reference documentation for the lifetime of the
  386. returned array.
  387. @code
  388. int count;
  389. const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_5, &count);
  390. @endcode
  391. Each element in the returned array is a value between -1.0 and 1.0.
  392. @subsection joystick_button Joystick button states
  393. The states of all buttons of a joystick are returned by @ref
  394. glfwGetJoystickButtons. See the reference documentation for the lifetime of the
  395. returned array.
  396. @code
  397. int count;
  398. const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_3, &count);
  399. @endcode
  400. Each element in the returned array is either `GLFW_PRESS` or `GLFW_RELEASE`.
  401. For backward compatibility with earlier versions that did not have @ref
  402. glfwGetJoystickHats, the button array by default also includes all hats. See
  403. the reference documentation for @ref glfwGetJoystickButtons for details.
  404. @subsection joystick_hat Joystick hat states
  405. The states of all hats are returned by @ref glfwGetJoystickHats. See the
  406. reference documentation for the lifetime of the returned array.
  407. @code
  408. int count;
  409. const unsigned char* hats = glfwGetJoystickHats(GLFW_JOYSTICK_7, &count);
  410. @endcode
  411. Each element in the returned array is one of the following:
  412. Name | Value
  413. --------------------- | --------------------------------
  414. `GLFW_HAT_CENTERED` | 0
  415. `GLFW_HAT_UP` | 1
  416. `GLFW_HAT_RIGHT` | 2
  417. `GLFW_HAT_DOWN` | 4
  418. `GLFW_HAT_LEFT` | 8
  419. `GLFW_HAT_RIGHT_UP` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_UP`
  420. `GLFW_HAT_RIGHT_DOWN` | `GLFW_HAT_RIGHT` \| `GLFW_HAT_DOWN`
  421. `GLFW_HAT_LEFT_UP` | `GLFW_HAT_LEFT` \| `GLFW_HAT_UP`
  422. `GLFW_HAT_LEFT_DOWN` | `GLFW_HAT_LEFT` \| `GLFW_HAT_DOWN`
  423. The diagonal directions are bitwise combinations of the primary (up, right, down
  424. and left) directions and you can test for these individually by ANDing it with
  425. the corresponding direction.
  426. @code
  427. if (hats[2] & GLFW_HAT_RIGHT)
  428. {
  429. // State of hat 2 could be right-up, right or right-down
  430. }
  431. @endcode
  432. For backward compatibility with earlier versions that did not have @ref
  433. glfwGetJoystickHats, all hats are by default also included in the button array.
  434. See the reference documentation for @ref glfwGetJoystickButtons for details.
  435. @subsection joystick_name Joystick name
  436. The human-readable, UTF-8 encoded name of a joystick is returned by @ref
  437. glfwGetJoystickName. See the reference documentation for the lifetime of the
  438. returned string.
  439. @code
  440. const char* name = glfwGetJoystickName(GLFW_JOYSTICK_4);
  441. @endcode
  442. Joystick names are not guaranteed to be unique. Two joysticks of the same model
  443. and make may have the same name. Only the [joystick token](@ref joysticks) is
  444. guaranteed to be unique, and only until that joystick is disconnected.
  445. @subsection joystick_event Joystick configuration changes
  446. If you wish to be notified when a joystick is connected or disconnected, set
  447. a joystick callback.
  448. @code
  449. glfwSetJoystickCallback(joystick_callback);
  450. @endcode
  451. The callback function receives the ID of the joystick that has been connected
  452. and disconnected and the event that occurred.
  453. @code
  454. void joystick_callback(int jid, int event)
  455. {
  456. if (event == GLFW_CONNECTED)
  457. {
  458. // The joystick was connected
  459. }
  460. else if (event == GLFW_DISCONNECTED)
  461. {
  462. // The joystick was disconnected
  463. }
  464. }
  465. @endcode
  466. For joystick connection and disconnection events to be delivered on all
  467. platforms, you need to call one of the [event processing](@ref events)
  468. functions. Joystick disconnection may also be detected and the callback
  469. called by joystick functions. The function will then return whatever it
  470. returns for a disconnected joystick.
  471. @subsection gamepad Gamepad input
  472. The joystick functions provide unlabeled axes, buttons and hats, with no
  473. indication of where they are located on the device. Their order may also vary
  474. between platforms even with the same device.
  475. To solve this problem the SDL community crowdsourced the
  476. [SDL_GameControllerDB](https://github.com/gabomdq/SDL_GameControllerDB) project,
  477. a database of mappings from many different devices to an Xbox-like gamepad.
  478. GLFW supports this mapping format and contains a copy of the mappings
  479. available at the time of release. See @ref gamepad_mapping for how to update
  480. this at runtime. Mappings will be assigned to joysticks automatically any time
  481. a joystick is connected or the mappings are updated.
  482. You can check whether a joystick is both present and has a gamepad mapping with
  483. @ref glfwJoystickIsGamepad.
  484. @code
  485. if (glfwJoystickIsGamepad(GLFW_JOYSTICK_2))
  486. {
  487. // Use as gamepad
  488. }
  489. @endcode
  490. If you are only interested in gamepad input you can use this function instead of
  491. @ref glfwJoystickPresent.
  492. You can query the human-readable name provided by the gamepad mapping with @ref
  493. glfwGetGamepadName. This may or may not be the same as the
  494. [joystick name](@ref joystick_name).
  495. @code
  496. const char* name = glfwGetGamepadName(GLFW_JOYSTICK_7);
  497. @endcode
  498. To retrieve the gamepad state of a joystick, call @ref glfwGetGamepadState.
  499. @code
  500. GLFWgamepadstate state;
  501. if (glfwGetGamepadState(GLFW_JOYSTICK_3, &state))
  502. {
  503. if (state.buttons[GLFW_GAMEPAD_BUTTON_A])
  504. {
  505. input_jump();
  506. }
  507. input_speed(state.axes[GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER]);
  508. }
  509. @endcode
  510. The @ref GLFWgamepadstate struct has two arrays; one for button states and one
  511. for axis states. The values for each button and axis are the same as for the
  512. @ref glfwGetJoystickButtons and @ref glfwGetJoystickAxes functions, i.e.
  513. `GLFW_PRESS` or `GLFW_RELEASE` for buttons and -1.0 to 1.0 inclusive for axes.
  514. The sizes of the arrays and the positions within each array are fixed.
  515. The [button indices](@ref gamepad_buttons) are `GLFW_GAMEPAD_BUTTON_A`,
  516. `GLFW_GAMEPAD_BUTTON_B`, `GLFW_GAMEPAD_BUTTON_X`, `GLFW_GAMEPAD_BUTTON_Y`,
  517. `GLFW_GAMEPAD_BUTTON_LEFT_BUMPER`, `GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER`,
  518. `GLFW_GAMEPAD_BUTTON_BACK`, `GLFW_GAMEPAD_BUTTON_START`,
  519. `GLFW_GAMEPAD_BUTTON_GUIDE`, `GLFW_GAMEPAD_BUTTON_LEFT_THUMB`,
  520. `GLFW_GAMEPAD_BUTTON_RIGHT_THUMB`, `GLFW_GAMEPAD_BUTTON_DPAD_UP`,
  521. `GLFW_GAMEPAD_BUTTON_DPAD_RIGHT`, `GLFW_GAMEPAD_BUTTON_DPAD_DOWN` and
  522. `GLFW_GAMEPAD_BUTTON_DPAD_LEFT`.
  523. For those who prefer, there are also the `GLFW_GAMEPAD_BUTTON_CROSS`,
  524. `GLFW_GAMEPAD_BUTTON_CIRCLE`, `GLFW_GAMEPAD_BUTTON_SQUARE` and
  525. `GLFW_GAMEPAD_BUTTON_TRIANGLE` aliases for the A, B, X and Y button indices.
  526. The [axis indices](@ref gamepad_axes) are `GLFW_GAMEPAD_AXIS_LEFT_X`,
  527. `GLFW_GAMEPAD_AXIS_LEFT_Y`, `GLFW_GAMEPAD_AXIS_RIGHT_X`,
  528. `GLFW_GAMEPAD_AXIS_RIGHT_Y`, `GLFW_GAMEPAD_AXIS_LEFT_TRIGGER` and
  529. `GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER`.
  530. The `GLFW_GAMEPAD_BUTTON_LAST` and `GLFW_GAMEPAD_AXIS_LAST` constants equal
  531. the largest available index for each array.
  532. @subsection gamepad_mapping Gamepad mappings
  533. GLFW contains a copy of the mappings available in
  534. [SDL_GameControllerDB](https://github.com/gabomdq/SDL_GameControllerDB) at the
  535. time of release. Newer ones can be added at runtime with @ref
  536. glfwUpdateGamepadMappings.
  537. @code
  538. const char* mappings = load_file_contents("gamecontrollerdb.txt");
  539. glfwUpdateGamepadMappings(mappings);
  540. @endcode
  541. This function supports everything from single lines up to and including the
  542. unmodified contents of the whole `gamecontrollerdb.txt` file.
  543. Below is a description of the mapping format. Please keep in mind that __this
  544. description is not authoritative__. The format is defined by the SDL and
  545. SDL_GameControllerDB projects and their documentation and code takes precedence.
  546. Each mapping is a single line of comma-separated values describing the GUID,
  547. name and layout of the gamepad. Lines that do not begin with a hexadecimal
  548. digit are ignored.
  549. The first value is always the gamepad GUID, a 32 character long hexadecimal
  550. string that typically identifies its make, model, revision and the type of
  551. connection to the computer. When this information is not available, the GUID is
  552. generated using the gamepad name. GLFW uses the SDL 2.0.5+ GUID format but can
  553. convert from the older formats.
  554. The second value is always the human-readable name of the gamepad.
  555. All subsequent values are in the form `<field>:<value>` and describe the layout
  556. of the mapping. These fields may not all be present and may occur in any order.
  557. The button fields are `a`, `b`, `c`, `d`, `back`, `start`, `guide`, `dpup`,
  558. `dpright`, `dpdown`, `dpleft`, `leftshoulder`, `rightshoulder`, `leftstick` and
  559. `rightstick`.
  560. The axis fields are `leftx`, `lefty`, `rightx`, `righty`, `lefttrigger` and
  561. `righttrigger`.
  562. The value of an axis or button field can be a joystick button, a joystick axis,
  563. a hat bitmask or empty. Joystick buttons are specified as `bN`, for example
  564. `b2` for the third button. Joystick axes are specified as `aN`, for example
  565. `a7` for the eighth button. Joystick hat bit masks are specified as `hN.N`, for
  566. example `h0.8` for left on the first hat. More than one bit may be set in the
  567. mask.
  568. The hat bit mask match the [hat states](@ref hat_state) in the joystick
  569. functions.
  570. There is also the special `platform` field that specifies which platform the
  571. mapping is valid for. Possible values are `Windows`, `Mac OS X` and `Linux`.
  572. Mappings without this field will always be considered valid.
  573. Below is an example of what a gamepad mapping might look like. It is the
  574. one built into GLFW for Xbox controllers accessed via the XInput API on Windows.
  575. This example has been broken into several lines to fit on the page, but real
  576. gamepad mappings must be a single line.
  577. @code{.unparsed}
  578. 78696e70757401000000000000000000,XInput Gamepad (GLFW),platform:Windows,a:b0,
  579. b:b1,x:b2,y:b3,leftshoulder:b4,rightshoulder:b5,back:b6,start:b7,leftstick:b8,
  580. rightstick:b9,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,
  581. righttrigger:a5,dpup:h0.1,dpright:h0.2,dpdown:h0.4,dpleft:h0.8,
  582. @endcode
  583. @note GLFW does not yet support the range and inversion modifiers `+`, `-` and
  584. `~` that were recently added to SDL.
  585. @section time Time input
  586. GLFW provides high-resolution time input, in seconds, with @ref glfwGetTime.
  587. @code
  588. double seconds = glfwGetTime();
  589. @endcode
  590. It returns the number of seconds since the timer was started when the library
  591. was initialized with @ref glfwInit. The platform-specific time sources used
  592. usually have micro- or nanosecond resolution.
  593. You can modify the reference time with @ref glfwSetTime.
  594. @code
  595. glfwSetTime(4.0);
  596. @endcode
  597. This sets the timer to the specified time, in seconds.
  598. You can also access the raw timer value, measured in 1&nbsp;/&nbsp;frequency
  599. seconds, with @ref glfwGetTimerValue.
  600. @code
  601. uint64_t value = glfwGetTimerValue();
  602. @endcode
  603. The frequency of the raw timer varies depending on what time sources are
  604. available on the machine. You can query its frequency, in Hz, with @ref
  605. glfwGetTimerFrequency.
  606. @code
  607. uint64_t freqency = glfwGetTimerFrequency();
  608. @endcode
  609. @section clipboard Clipboard input and output
  610. If the system clipboard contains a UTF-8 encoded string or if it can be
  611. converted to one, you can retrieve it with @ref glfwGetClipboardString. See the
  612. reference documentation for the lifetime of the returned string.
  613. @code
  614. const char* text = glfwGetClipboardString(window);
  615. if (text)
  616. {
  617. insert_text(text);
  618. }
  619. @endcode
  620. If the clipboard is empty or if its contents could not be converted, `NULL` is
  621. returned.
  622. The contents of the system clipboard can be set to a UTF-8 encoded string with
  623. @ref glfwSetClipboardString.
  624. @code
  625. glfwSetClipboardString(window, "A string with words in it");
  626. @endcode
  627. The clipboard functions take a window handle argument because some window
  628. systems require a window to communicate with the system clipboard. Any valid
  629. window may be used.
  630. @section path_drop Path drop input
  631. If you wish to receive the paths of files and/or directories dropped on
  632. a window, set a file drop callback.
  633. @code
  634. glfwSetDropCallback(window, drop_callback);
  635. @endcode
  636. The callback function receives an array of paths encoded as UTF-8.
  637. @code
  638. void drop_callback(GLFWwindow* window, int count, const char** paths)
  639. {
  640. int i;
  641. for (i = 0; i < count; i++)
  642. handle_dropped_file(paths[i]);
  643. }
  644. @endcode
  645. The path array and its strings are only valid until the file drop callback
  646. returns, as they may have been generated specifically for that event. You need
  647. to make a deep copy of the array if you want to keep the paths.
  648. */