input.dox 32 KB

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