input.dox 33 KB

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