input.dox 32 KB

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