input.dox 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. /*!
  2. @page input_guide Input guide
  3. @tableofcontents
  4. This guide introduces the input related functions of GLFW. For details on
  5. a specific function in this category, see the @ref input. There are also guides
  6. for the other areas of GLFW.
  7. - @ref intro_guide
  8. - @ref window_guide
  9. - @ref context_guide
  10. - @ref vulkan_guide
  11. - @ref monitor_guide
  12. GLFW provides many kinds of input. While some can only be polled, like time, or
  13. only received via callbacks, like scrolling, many provide both callbacks and
  14. polling. Callbacks are more work to use than polling but is less CPU intensive
  15. and guarantees that you do not miss state changes.
  16. All input callbacks receive a window handle. By using the
  17. [window user pointer](@ref window_userptr), you can access non-global structures
  18. or objects from your callbacks.
  19. To get a better feel for how the various events callbacks behave, run the
  20. `events` test program. It register every callback supported by GLFW and prints
  21. out all arguments provided for every event, along with time and sequence
  22. information.
  23. @section events Event processing
  24. GLFW needs to poll the window system for events both to provide input to the
  25. application and to prove to the window system that the application hasn't locked
  26. up. Event processing is normally done each frame after
  27. [buffer swapping](@ref buffer_swap). Even when you have no windows, event
  28. polling needs to be done in order to receive monitor and joystick connection
  29. events.
  30. There are three functions for processing pending events. @ref glfwPollEvents,
  31. processes only those events that have already been received and then returns
  32. immediately.
  33. @code
  34. glfwPollEvents();
  35. @endcode
  36. This is the best choice when rendering continuously, like most games do.
  37. If you only need to update the contents of the window when you receive new
  38. input, @ref glfwWaitEvents is a better choice.
  39. @code
  40. glfwWaitEvents();
  41. @endcode
  42. It puts the thread to sleep until at least one event has been received and then
  43. processes all received events. This saves a great deal of CPU cycles and is
  44. useful for, for example, editing tools. There must be at least one GLFW window
  45. for this function to sleep.
  46. If you want to wait for events but have UI elements or other tasks that need
  47. periodic updates, @ref glfwWaitEventsTimeout lets you specify a timeout.
  48. @code
  49. glfwWaitEventsTimeout(0.7);
  50. @endcode
  51. It puts the thread to sleep until at least one event has been received, or until
  52. the specified number of seconds have elapsed. It then processes any received
  53. events.
  54. If the main thread is sleeping in @ref glfwWaitEvents, you can wake it from
  55. another thread by posting an empty event to the event queue with @ref
  56. glfwPostEmptyEvent.
  57. @code
  58. glfwPostEmptyEvent();
  59. @endcode
  60. Do not assume that callbacks will _only_ be called in response to the above
  61. functions. While it is necessary to process events in one or more of the ways
  62. above, window systems that require GLFW to register callbacks of its own can
  63. pass events to GLFW in response to many window system function calls. GLFW will
  64. pass those events on to the application callbacks before returning.
  65. For example, on Windows the system function that @ref glfwSetWindowSize is
  66. implemented with will send window size events directly to the event callback
  67. that every window has and that GLFW implements for its windows. If you have set
  68. a [window size callback](@ref window_size) GLFW will call it in turn with the
  69. new size before everything returns back out of the @ref glfwSetWindowSize call.
  70. @section input_keyboard Keyboard input
  71. GLFW divides keyboard input into two categories; key events and character
  72. events. Key events relate to actual physical keyboard keys, whereas character
  73. events relate to the Unicode code points generated by pressing some of them.
  74. Keys and characters do not map 1:1. A single key press may produce several
  75. characters, and a single character may require several keys to produce. This
  76. may not be the case on your machine, but your users are likely not all using the
  77. same keyboard layout, input method or even operating system as you.
  78. @subsection input_key Key input
  79. If you wish to be notified when a physical key is pressed or released or when it
  80. repeats, set a key callback.
  81. @code
  82. glfwSetKeyCallback(window, key_callback);
  83. @endcode
  84. The callback function receives the [keyboard key](@ref keys), platform-specific
  85. scancode, key action and [modifier bits](@ref mods).
  86. @code
  87. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  88. {
  89. if (key == GLFW_KEY_E && action == GLFW_PRESS)
  90. activate_airship();
  91. }
  92. @endcode
  93. The action is one of `GLFW_PRESS`, `GLFW_REPEAT` or `GLFW_RELEASE`. The key
  94. will be `GLFW_KEY_UNKNOWN` if GLFW lacks a key token for it, for example
  95. _E-mail_ and _Play_ keys.
  96. The scancode is unique for every key, regardless of whether it has a key token.
  97. Scancodes are platform-specific but consistent over time, so keys will have
  98. different scancodes depending on the platform but they are safe to save to disk.
  99. You can query the scancode for any [named key](@ref keys) on the current
  100. platform with @ref glfwGetKeyScancode.
  101. @code
  102. const int scancode = glfwGetKeyScancode(GLFW_KEY_X);
  103. set_key_mapping(scancode, swap_weapons);
  104. @endcode
  105. The last reported state for every [named key](@ref keys) is also saved in
  106. per-window state arrays that can be polled with @ref glfwGetKey.
  107. @code
  108. int state = glfwGetKey(window, GLFW_KEY_E);
  109. if (state == GLFW_PRESS)
  110. activate_airship();
  111. @endcode
  112. The returned state is one of `GLFW_PRESS` or `GLFW_RELEASE`.
  113. This function only returns cached key event state. It does not poll the
  114. system for the current physical state of the key.
  115. Whenever you poll state, you risk missing the state change you are looking for.
  116. If a pressed key is released again before you poll its state, you will have
  117. missed the key press. The recommended solution for this is to use a
  118. key callback, but there is also the `GLFW_STICKY_KEYS` input mode.
  119. @code
  120. glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
  121. @endcode
  122. When sticky keys mode is enabled, the pollable state of a key will remain
  123. `GLFW_PRESS` until the state of that key is polled with @ref glfwGetKey. Once
  124. it has been polled, if a key release event had been processed in the meantime,
  125. the state will reset to `GLFW_RELEASE`, otherwise it will remain `GLFW_PRESS`.
  126. The `GLFW_KEY_LAST` constant holds the highest value of any
  127. [named key](@ref keys).
  128. @subsection input_char Text input
  129. GLFW supports text input in the form of a stream of
  130. [Unicode code points](https://en.wikipedia.org/wiki/Unicode), as produced by the
  131. operating system text input system. Unlike key input, text input obeys keyboard
  132. layouts and modifier keys and supports composing characters using
  133. [dead keys](https://en.wikipedia.org/wiki/Dead_key). Once received, you can
  134. encode the code points into
  135. [UTF-8](https://en.wikipedia.org/wiki/UTF-8) or any other encoding you prefer.
  136. Because an `unsigned int` is 32 bits long on all platforms supported by GLFW,
  137. you can treat the code point argument as native endian
  138. [UTF-32](https://en.wikipedia.org/wiki/UTF-32).
  139. There are two callbacks for receiving Unicode code points. If you wish to
  140. offer regular text input, set a character callback.
  141. @code
  142. glfwSetCharCallback(window, character_callback);
  143. @endcode
  144. The callback function receives Unicode code points for key events that would
  145. have led to regular text input and generally behaves as a standard text field on
  146. that platform.
  147. @code
  148. void character_callback(GLFWwindow* window, unsigned int codepoint)
  149. {
  150. }
  151. @endcode
  152. If you wish to receive even those Unicode code points generated with modifier
  153. key combinations that a plain text field would ignore, or just want to know
  154. exactly what modifier keys were used, set a character with modifiers callback.
  155. @code
  156. glfwSetCharModsCallback(window, charmods_callback);
  157. @endcode
  158. The callback function receives Unicode code points and
  159. [modifier bits](@ref mods).
  160. @code
  161. void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods)
  162. {
  163. }
  164. @endcode
  165. @subsection input_key_name Key names
  166. If you wish to refer to keys by name, you can query the keyboard layout
  167. dependent name of printable keys with @ref glfwGetKeyName.
  168. @code
  169. const char* key_name = glfwGetKeyName(GLFW_KEY_W, 0);
  170. show_tutorial_hint("Press %s to move forward", key_name);
  171. @endcode
  172. This function can handle both [keys and scancodes](@ref input_key). If the
  173. specified key is `GLFW_KEY_UNKNOWN` then the scancode is used, otherwise it is
  174. ignored. This matches the behavior of the key callback, meaning the callback
  175. arguments can always be passed unmodified to this function.
  176. @section input_mouse Mouse input
  177. Mouse input comes in many forms, including cursor motion, button presses and
  178. scrolling offsets. The cursor appearance can also be changed, either to
  179. a custom image or a standard cursor shape from the system theme.
  180. @subsection cursor_pos Cursor position
  181. If you wish to be notified when the cursor moves over the window, set a cursor
  182. position callback.
  183. @code
  184. glfwSetCursorPosCallback(window, cursor_pos_callback);
  185. @endcode
  186. The callback functions receives the cursor position, measured in screen
  187. coordinates but relative to the top-left corner of the window client area. On
  188. platforms that provide it, the full sub-pixel cursor position is passed on.
  189. @code
  190. static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
  191. {
  192. }
  193. @endcode
  194. The cursor position is also saved per-window and can be polled with @ref
  195. glfwGetCursorPos.
  196. @code
  197. double xpos, ypos;
  198. glfwGetCursorPos(window, &xpos, &ypos);
  199. @endcode
  200. @subsection cursor_mode Cursor modes
  201. The `GLFW_CURSOR` input mode provides several cursor modes for special forms of
  202. mouse motion input. By default, the cursor mode is `GLFW_CURSOR_NORMAL`,
  203. meaning the regular arrow cursor (or another cursor set with @ref glfwSetCursor)
  204. is used and cursor motion is not limited.
  205. If you wish to implement mouse motion based camera controls or other input
  206. schemes that require unlimited mouse movement, set the cursor mode to
  207. `GLFW_CURSOR_DISABLED`.
  208. @code
  209. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
  210. @endcode
  211. This will hide the cursor and lock it to the specified window. GLFW will then
  212. take care of all the details of cursor re-centering and offset calculation and
  213. providing the application with a virtual cursor position. This virtual position
  214. is provided normally via both the cursor position callback and through polling.
  215. @note You should not implement your own version of this functionality using
  216. other features of GLFW. It is not supported and will not work as robustly as
  217. `GLFW_CURSOR_DISABLED`.
  218. If you just wish the cursor to become hidden when it is over a window, set
  219. the cursor mode to `GLFW_CURSOR_HIDDEN`.
  220. @code
  221. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  222. @endcode
  223. This mode puts no limit on the motion of the cursor.
  224. To exit out of either of these special modes, restore the `GLFW_CURSOR_NORMAL`
  225. cursor mode.
  226. @code
  227. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  228. @endcode
  229. @subsection cursor_object Cursor objects
  230. GLFW supports creating both custom and system theme cursor images, encapsulated
  231. as @ref GLFWcursor objects. They are created with @ref glfwCreateCursor or @ref
  232. glfwCreateStandardCursor and destroyed with @ref glfwDestroyCursor, or @ref
  233. glfwTerminate, if any remain.
  234. @subsubsection cursor_custom Custom cursor creation
  235. A custom cursor is created with @ref glfwCreateCursor, which returns a handle to
  236. the created cursor object. For example, this creates a 16x16 white square
  237. cursor with the hot-spot in the upper-left corner:
  238. @code
  239. unsigned char pixels[16 * 16 * 4];
  240. memset(pixels, 0xff, sizeof(pixels));
  241. GLFWimage image;
  242. image.width = 16;
  243. image.height = 16;
  244. image.pixels = pixels;
  245. GLFWcursor* cursor = glfwCreateCursor(&image, 0, 0);
  246. @endcode
  247. If cursor creation fails, `NULL` will be returned, so it is necessary to check
  248. the return value.
  249. The image data is 32-bit, little-endian, non-premultiplied RGBA, i.e. eight bits
  250. per channel. The pixels are arranged canonically as sequential rows, starting
  251. from the top-left corner.
  252. @subsubsection cursor_standard Standard cursor creation
  253. A cursor with a [standard shape](@ref shapes) from the current system cursor
  254. theme can be can be created with @ref glfwCreateStandardCursor.
  255. @code
  256. GLFWcursor* cursor = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
  257. @endcode
  258. These cursor objects behave in the exact same way as those created with @ref
  259. glfwCreateCursor except that the system cursor theme provides the actual image.
  260. @subsubsection cursor_destruction Cursor destruction
  261. When a cursor is no longer needed, destroy it with @ref glfwDestroyCursor.
  262. @code
  263. glfwDestroyCursor(cursor);
  264. @endcode
  265. Cursor destruction always succeeds. All cursors remaining when @ref
  266. glfwTerminate is called are destroyed as well.
  267. @subsubsection cursor_set Cursor setting
  268. A cursor can be set as current for a window with @ref glfwSetCursor.
  269. @code
  270. glfwSetCursor(window, cursor);
  271. @endcode
  272. Once set, the cursor image will be used as long as the system cursor is over the
  273. client area of the window and the [cursor mode](@ref cursor_mode) is set
  274. to `GLFW_CURSOR_NORMAL`.
  275. A single cursor may be set for any number of windows.
  276. To remove a cursor from a window, set the cursor of that window to `NULL`.
  277. @code
  278. glfwSetCursor(window, NULL);
  279. @endcode
  280. When a cursor is destroyed, it is removed from any window where it is set. This
  281. does not affect the cursor modes of those windows.
  282. @subsection cursor_enter Cursor enter/leave events
  283. If you wish to be notified when the cursor enters or leaves the client area of
  284. a window, set a cursor enter/leave callback.
  285. @code
  286. glfwSetCursorEnterCallback(window, cursor_enter_callback);
  287. @endcode
  288. The callback function receives the new classification of the cursor.
  289. @code
  290. void cursor_enter_callback(GLFWwindow* window, int entered)
  291. {
  292. if (entered)
  293. {
  294. // The cursor entered the client area of the window
  295. }
  296. else
  297. {
  298. // The cursor left the client area of the window
  299. }
  300. }
  301. @endcode
  302. @subsection input_mouse_button Mouse button input
  303. If you wish to be notified when a mouse button is pressed or released, set
  304. a mouse button callback.
  305. @code
  306. glfwSetMouseButtonCallback(window, mouse_button_callback);
  307. @endcode
  308. The callback function receives the [mouse button](@ref buttons), button action
  309. and [modifier bits](@ref mods).
  310. @code
  311. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
  312. {
  313. if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
  314. popup_menu();
  315. }
  316. @endcode
  317. The action is one of `GLFW_PRESS` or `GLFW_RELEASE`.
  318. Mouse button states for [named buttons](@ref buttons) are also saved in
  319. per-window state arrays that can be polled with @ref glfwGetMouseButton.
  320. @code
  321. int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
  322. if (state == GLFW_PRESS)
  323. upgrade_cow();
  324. @endcode
  325. The returned state is one of `GLFW_PRESS` or `GLFW_RELEASE`.
  326. This function only returns cached mouse button event state. It does not poll
  327. the system for the current state of the mouse button.
  328. Whenever you poll state, you risk missing the state change you are looking for.
  329. If a pressed mouse button is released again before you poll its state, you will have
  330. missed the button press. The recommended solution for this is to use a
  331. mouse button callback, but there is also the `GLFW_STICKY_MOUSE_BUTTONS`
  332. input mode.
  333. @code
  334. glfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, 1);
  335. @endcode
  336. When sticky mouse buttons mode is enabled, the pollable state of a mouse button
  337. will remain `GLFW_PRESS` until the state of that button is polled with @ref
  338. glfwGetMouseButton. Once it has been polled, if a mouse button release event
  339. had been processed in the meantime, the state will reset to `GLFW_RELEASE`,
  340. otherwise it will remain `GLFW_PRESS`.
  341. The `GLFW_MOUSE_BUTTON_LAST` constant holds the highest value of any
  342. [named button](@ref buttons).
  343. @subsection scrolling Scroll input
  344. If you wish to be notified when the user scrolls, whether with a mouse wheel or
  345. touchpad gesture, set a scroll callback.
  346. @code
  347. glfwSetScrollCallback(window, scroll_callback);
  348. @endcode
  349. The callback function receives two-dimensional scroll offsets.
  350. @code
  351. void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
  352. {
  353. }
  354. @endcode
  355. A simple mouse wheel, being vertical, provides offsets along the Y-axis.
  356. @section joystick Joystick input
  357. The joystick functions expose connected joysticks and controllers, with both
  358. referred to as joysticks. It supports up to sixteen joysticks, ranging from
  359. `GLFW_JOYSTICK_1`, `GLFW_JOYSTICK_2` up to `GLFW_JOYSTICK_LAST`. You can test
  360. whether a [joystick](@ref joysticks) is present with @ref glfwJoystickPresent.
  361. @code
  362. int present = glfwJoystickPresent(GLFW_JOYSTICK_1);
  363. @endcode
  364. When GLFW is initialized, detected joysticks are added to to the beginning of
  365. the array, starting with `GLFW_JOYSTICK_1`. Once a joystick is detected, it
  366. keeps its assigned index until it is disconnected, so as joysticks are connected
  367. and disconnected, they will become spread out.
  368. Joystick state is updated as needed when a joystick function is called and does
  369. not require a window to be created or @ref glfwPollEvents or @ref glfwWaitEvents
  370. to be called.
  371. To see all the properties of all connected joysticks in real-time, run the
  372. `joysticks` test program.
  373. @subsection joystick_axis Joystick axis states
  374. The positions of all axes of a joystick are returned by @ref
  375. glfwGetJoystickAxes. See the reference documentation for the lifetime of the
  376. returned array.
  377. @code
  378. int count;
  379. const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &count);
  380. @endcode
  381. Each element in the returned array is a value between -1.0 and 1.0.
  382. @subsection joystick_button Joystick button states
  383. The states of all buttons of a joystick are returned by @ref
  384. glfwGetJoystickButtons. See the reference documentation for the lifetime of the
  385. returned array.
  386. @code
  387. int count;
  388. const unsigned char* axes = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &count);
  389. @endcode
  390. Each element in the returned array is either `GLFW_PRESS` or `GLFW_RELEASE`.
  391. @subsection joystick_name Joystick name
  392. The human-readable, UTF-8 encoded name of a joystick is returned by @ref
  393. glfwGetJoystickName. See the reference documentation for the lifetime of the
  394. returned string.
  395. @code
  396. const char* name = glfwGetJoystickName(GLFW_JOYSTICK_1);
  397. @endcode
  398. Joystick names are not guaranteed to be unique. Two joysticks of the same model
  399. and make may have the same name. Only the [joystick token](@ref joysticks) is
  400. guaranteed to be unique, and only until that joystick is disconnected.
  401. @subsection joystick_event Joystick configuration changes
  402. If you wish to be notified when a joystick is connected or disconnected, set
  403. a joystick callback.
  404. @code
  405. glfwSetJoystickCallback(joystick_callback);
  406. @endcode
  407. The callback function receives the ID of the joystick that has been connected
  408. and disconnected and the event that occurred.
  409. @code
  410. void joystick_callback(int jid, int event)
  411. {
  412. if (event == GLFW_CONNECTED)
  413. {
  414. // The joystick was connected
  415. }
  416. else if (event == GLFW_DISCONNECTED)
  417. {
  418. // The joystick was disconnected
  419. }
  420. }
  421. @endcode
  422. @section time Time input
  423. GLFW provides high-resolution time input, in seconds, with @ref glfwGetTime.
  424. @code
  425. double seconds = glfwGetTime();
  426. @endcode
  427. It returns the number of seconds since the timer was started when the library
  428. was initialized with @ref glfwInit. The platform-specific time sources used
  429. usually have micro- or nanosecond resolution.
  430. You can modify the reference time with @ref glfwSetTime.
  431. @code
  432. glfwSetTime(4.0);
  433. @endcode
  434. This sets the timer to the specified time, in seconds.
  435. You can also access the raw timer value, measured in 1 / frequency
  436. seconds, with @ref glfwGetTimerValue.
  437. @code
  438. uint64_t value = glfwGetTimerValue();
  439. @endcode
  440. The frequency of the raw timer varies depending on what time sources are
  441. available on the machine. You can query its frequency, in Hz, with @ref
  442. glfwGetTimerFrequency.
  443. @code
  444. uint64_t freqency = glfwGetTimerFrequency();
  445. @endcode
  446. @section clipboard Clipboard input and output
  447. If the system clipboard contains a UTF-8 encoded string or if it can be
  448. converted to one, you can retrieve it with @ref glfwGetClipboardString. See the
  449. reference documentation for the lifetime of the returned string.
  450. @code
  451. const char* text = glfwGetClipboardString(window);
  452. if (text)
  453. insert_text(text);
  454. @endcode
  455. If the clipboard is empty or if its contents could not be converted, `NULL` is
  456. returned.
  457. The contents of the system clipboard can be set to a UTF-8 encoded string with
  458. @ref glfwSetClipboardString.
  459. @code
  460. glfwSetClipboardString(window, "A string with words in it");
  461. @endcode
  462. The clipboard functions take a window handle argument because some window
  463. systems require a window to communicate with the system clipboard. Any valid
  464. window may be used.
  465. @section path_drop Path drop input
  466. If you wish to receive the paths of files and/or directories dropped on
  467. a window, set a file drop callback.
  468. @code
  469. glfwSetDropCallback(window, drop_callback);
  470. @endcode
  471. The callback function receives an array of paths encoded as UTF-8.
  472. @code
  473. void drop_callback(GLFWwindow* window, int count, const char** paths)
  474. {
  475. int i;
  476. for (i = 0; i < count; i++)
  477. handle_dropped_file(paths[i]);
  478. }
  479. @endcode
  480. The path array and its strings are only valid until the file drop callback
  481. returns, as they may have been generated specifically for that event. You need
  482. to make a deep copy of the array if you want to keep the paths.
  483. */