input.dox 29 KB

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