1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-27 04:28:52 -05:00
glfw/docs/input.dox

688 lines
22 KiB
Plaintext
Raw Normal View History

2014-04-23 07:30:11 -04:00
/*!
@page input_guide Input guide
2014-04-23 07:30:11 -04:00
@tableofcontents
This guide introduces the input related functions of GLFW. For details on
2016-08-11 13:04:21 -04:00
a specific function in this category, see the @ref input. There are also guides
for the other areas of GLFW.
2014-09-18 09:03:29 -04:00
- @ref intro_guide
- @ref window_guide
- @ref context_guide
2016-08-11 13:04:21 -04:00
- @ref vulkan_guide
- @ref monitor_guide
2014-09-18 09:03:29 -04:00
GLFW provides many kinds of input. While some can only be polled, like time, or
only received via callbacks, like scrolling, many provide both callbacks and
polling. Callbacks are more work to use than polling but is less CPU intensive
and guarantees that you do not miss state changes.
2014-09-18 09:03:29 -04:00
All input callbacks receive a window handle. By using the
[window user pointer](@ref window_userptr), you can access non-global structures
or objects from your callbacks.
To get a better feel for how the various events callbacks behave, run the
`events` test program. It register every callback supported by GLFW and prints
out all arguments provided for every event, along with time and sequence
information.
2015-01-17 19:55:25 -05:00
@section events Event processing
2014-09-18 09:03:29 -04:00
GLFW needs to poll the window system for events both to provide input to the
application and to prove to the window system that the application hasn't locked
up. Event processing is normally done each frame after
[buffer swapping](@ref buffer_swap). Even when you have no windows, event
polling needs to be done in order to receive monitor and joystick connection
events.
2014-09-18 09:03:29 -04:00
There are three functions for processing pending events. @ref glfwPollEvents,
2014-09-18 09:03:29 -04:00
processes only those events that have already been received and then returns
immediately.
@code
glfwPollEvents();
@endcode
This is the best choice when rendering continuously, like most games do.
2014-09-18 09:03:29 -04:00
If you only need to update the contents of the window when you receive new
input, @ref glfwWaitEvents is a better choice.
@code
glfwWaitEvents();
@endcode
It puts the thread to sleep until at least one event has been received and then
processes all received events. This saves a great deal of CPU cycles and is
useful for, for example, editing tools. There must be at least one GLFW window
for this function to sleep.
If you want to wait for events but have UI elements or other tasks that need
periodic updates, @ref glfwWaitEventsTimeout lets you specify a timeout.
@code
glfwWaitEventsTimeout(0.7);
@endcode
It puts the thread to sleep until at least one event has been received, or until
the specified number of seconds have elapsed. It then processes any received
events.
2014-09-18 09:03:29 -04:00
If the main thread is sleeping in @ref glfwWaitEvents, you can wake it from
another thread by posting an empty event to the event queue with @ref
glfwPostEmptyEvent.
@code
glfwPostEmptyEvent();
@endcode
Do not assume that callbacks will _only_ be called in response to the above
functions. While it is necessary to process events in one or more of the ways
above, window systems that require GLFW to register callbacks of its own can
pass events to GLFW in response to many window system function calls. GLFW will
pass those events on to the application callbacks before returning.
For example, on Windows the system function that @ref glfwSetWindowSize is
implemented with will send window size events directly to the event callback
that every window has and that GLFW implements for its windows. If you have set
a [window size callback](@ref window_size) GLFW will call it in turn with the
new size before everything returns back out of the @ref glfwSetWindowSize call.
2014-09-18 09:03:29 -04:00
@section input_keyboard Keyboard input
GLFW divides keyboard input into two categories; key events and character
events. Key events relate to actual physical keyboard keys, whereas character
events relate to the Unicode code points generated by pressing some of them.
Keys and characters do not map 1:1. A single key press may produce several
characters, and a single character may require several keys to produce. This
may not be the case on your machine, but your users are likely not all using the
same keyboard layout, input method or even operating system as you.
@subsection input_key Key input
If you wish to be notified when a physical key is pressed or released or when it
2014-10-02 11:35:10 -04:00
repeats, set a key callback.
2014-09-18 09:03:29 -04:00
@code
glfwSetKeyCallback(window, key_callback);
@endcode
The callback function receives the [keyboard key](@ref keys), platform-specific
scancode, key action and [modifier bits](@ref mods).
@code
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_E && action == GLFW_PRESS)
activate_airship();
}
@endcode
The action is one of `GLFW_PRESS`, `GLFW_REPEAT` or `GLFW_RELEASE`. The key
will be `GLFW_KEY_UNKNOWN` if GLFW lacks a key token for it, for example
_E-mail_ and _Play_ keys.
2014-09-18 09:03:29 -04:00
The scancode is unique for every key, regardless of whether it has a key token.
Scancodes are platform-specific but consistent over time, so keys will have
different scancodes depending on the platform but they are safe to save to disk.
You can query the scancode for any [named key](@ref keys) on the current
platform with @ref glfwGetKeyScancode.
@code
const int scancode = glfwGetKeyScancode(GLFW_KEY_X);
set_key_mapping(scancode, swap_weapons);
@endcode
2014-09-18 09:03:29 -04:00
The last reported state for every [named key](@ref keys) is also saved in
per-window state arrays that can be polled with @ref glfwGetKey.
2014-09-18 09:03:29 -04:00
@code
int state = glfwGetKey(window, GLFW_KEY_E);
if (state == GLFW_PRESS)
activate_airship();
@endcode
The returned state is one of `GLFW_PRESS` or `GLFW_RELEASE`.
This function only returns cached key event state. It does not poll the
system for the current physical state of the key.
2014-09-18 09:03:29 -04:00
Whenever you poll state, you risk missing the state change you are looking for.
If a pressed key is released again before you poll its state, you will have
missed the key press. The recommended solution for this is to use a
key callback, but there is also the `GLFW_STICKY_KEYS` input mode.
@code
glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
@endcode
When sticky keys mode is enabled, the pollable state of a key will remain
`GLFW_PRESS` until the state of that key is polled with @ref glfwGetKey. Once
it has been polled, if a key release event had been processed in the meantime,
the state will reset to `GLFW_RELEASE`, otherwise it will remain `GLFW_PRESS`.
The `GLFW_KEY_LAST` constant holds the highest value of any
[named key](@ref keys).
2014-10-07 17:37:59 -04:00
@subsection input_char Text input
2014-09-18 09:03:29 -04:00
2014-10-07 17:37:59 -04:00
GLFW supports text input in the form of a stream of
[Unicode code points](https://en.wikipedia.org/wiki/Unicode), as produced by the
operating system text input system. Unlike key input, text input obeys keyboard
layouts and modifier keys and supports composing characters using
[dead keys](https://en.wikipedia.org/wiki/Dead_key). Once received, you can
encode the code points into
[UTF-8](https://en.wikipedia.org/wiki/UTF-8) or any other encoding you prefer.
Because an `unsigned int` is 32 bits long on all platforms supported by GLFW,
you can treat the code point argument as native endian
[UTF-32](https://en.wikipedia.org/wiki/UTF-32).
There are two callbacks for receiving Unicode code points. If you wish to
offer regular text input, set a character callback.
2014-09-18 09:03:29 -04:00
@code
glfwSetCharCallback(window, character_callback);
@endcode
The callback function receives Unicode code points for key events that would
2014-10-07 17:37:59 -04:00
have led to regular text input and generally behaves as a standard text field on
that platform.
2014-09-18 09:03:29 -04:00
@code
void character_callback(GLFWwindow* window, unsigned int codepoint)
{
}
@endcode
2014-10-07 17:37:59 -04:00
If you wish to receive even those Unicode code points generated with modifier
key combinations that a plain text field would ignore, or just want to know
exactly what modifier keys were used, set a character with modifiers callback.
2014-09-18 09:03:29 -04:00
@code
2015-01-17 19:55:25 -05:00
glfwSetCharModsCallback(window, charmods_callback);
2014-09-18 09:03:29 -04:00
@endcode
The callback function receives Unicode code points and
[modifier bits](@ref mods).
@code
void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods)
{
}
@endcode
2014-04-23 07:30:11 -04:00
2015-07-02 08:24:50 -04:00
@subsection input_key_name Key names
If you wish to refer to keys by name, you can query the keyboard layout
dependent name of printable keys with @ref glfwGetKeyName.
@code
const char* key_name = glfwGetKeyName(GLFW_KEY_W, 0);
show_tutorial_hint("Press %s to move forward", key_name);
@endcode
This function can handle both [keys and scancodes](@ref input_key). If the
specified key is `GLFW_KEY_UNKNOWN` then the scancode is used, otherwise it is
ignored. This matches the behavior of the key callback, meaning the callback
arguments can always be passed unmodified to this function.
2014-04-23 07:30:11 -04:00
@section input_mouse Mouse input
2015-01-17 19:55:25 -05:00
Mouse input comes in many forms, including cursor motion, button presses and
scrolling offsets. The cursor appearance can also be changed, either to
a custom image or a standard cursor shape from the system theme.
2014-09-18 09:03:29 -04:00
2015-01-17 19:55:25 -05:00
@subsection cursor_pos Cursor position
2014-09-18 09:03:29 -04:00
2015-01-17 19:55:25 -05:00
If you wish to be notified when the cursor moves over the window, set a cursor
position callback.
2014-09-18 09:03:29 -04:00
@code
glfwSetCursorPosCallback(window, cursor_pos_callback);
@endcode
The callback functions receives the cursor position, measured in screen
coordinates but relative to the top-left corner of the window client area. On
platforms that provide it, the full sub-pixel cursor position is passed on.
2014-09-18 09:03:29 -04:00
@code
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
}
@endcode
The cursor position is also saved per-window and can be polled with @ref
glfwGetCursorPos.
@code
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
@endcode
2015-01-17 19:55:25 -05:00
@subsection cursor_mode Cursor modes
2014-09-18 09:03:29 -04:00
The `GLFW_CURSOR` input mode provides several cursor modes for special forms of
2015-01-17 19:55:25 -05:00
mouse motion input. By default, the cursor mode is `GLFW_CURSOR_NORMAL`,
meaning the regular arrow cursor (or another cursor set with @ref glfwSetCursor)
is used and cursor motion is not limited.
2014-09-18 09:03:29 -04:00
If you wish to implement mouse motion based camera controls or other input
schemes that require unlimited mouse movement, set the cursor mode to
`GLFW_CURSOR_DISABLED`.
@code
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
@endcode
This will hide the cursor and lock it to the specified window. GLFW will then
take care of all the details of cursor re-centering and offset calculation and
providing the application with a virtual cursor position. This virtual position
2015-01-17 19:55:25 -05:00
is provided normally via both the cursor position callback and through polling.
2014-09-18 09:03:29 -04:00
@note You should not implement your own version of this functionality using
2015-01-17 19:55:25 -05:00
other features of GLFW. It is not supported and will not work as robustly as
`GLFW_CURSOR_DISABLED`.
2014-09-18 09:03:29 -04:00
If you just wish the cursor to become hidden when it is over a window, set
the cursor mode to `GLFW_CURSOR_HIDDEN`.
@code
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
@endcode
This mode puts no limit on the motion of the cursor.
To exit out of either of these special modes, restore the `GLFW_CURSOR_NORMAL`
cursor mode.
@code
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
@endcode
2015-01-17 19:55:25 -05:00
@subsection cursor_object Cursor objects
2014-09-18 09:03:29 -04:00
2015-01-17 19:55:25 -05:00
GLFW supports creating both custom and system theme cursor images, encapsulated
as @ref GLFWcursor objects. They are created with @ref glfwCreateCursor or @ref
glfwCreateStandardCursor and destroyed with @ref glfwDestroyCursor, or @ref
glfwTerminate, if any remain.
2014-09-18 09:03:29 -04:00
2015-01-17 19:55:25 -05:00
@subsubsection cursor_custom Custom cursor creation
2014-09-18 09:03:29 -04:00
2015-01-17 19:55:25 -05:00
A custom cursor is created with @ref glfwCreateCursor, which returns a handle to
the created cursor object. For example, this creates a 16x16 white square
cursor with the hot-spot in the upper-left corner:
2014-09-18 09:03:29 -04:00
@code
unsigned char pixels[16 * 16 * 4];
memset(pixels, 0xff, sizeof(pixels));
GLFWimage image;
image.width = 16;
image.height = 16;
image.pixels = pixels;
GLFWcursor* cursor = glfwCreateCursor(&image, 0, 0);
@endcode
2014-10-02 11:35:10 -04:00
If cursor creation fails, `NULL` will be returned, so it is necessary to check
the return value.
The image data is 32-bit, little-endian, non-premultiplied RGBA, i.e. eight bits
per channel. The pixels are arranged canonically as sequential rows, starting
from the top-left corner.
2014-09-18 09:03:29 -04:00
2015-01-17 19:55:25 -05:00
@subsubsection cursor_standard Standard cursor creation
A cursor with a [standard shape](@ref shapes) from the current system cursor
theme can be can be created with @ref glfwCreateStandardCursor.
@code
GLFWcursor* cursor = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
@endcode
These cursor objects behave in the exact same way as those created with @ref
glfwCreateCursor except that the system cursor theme provides the actual image.
@subsubsection cursor_destruction Cursor destruction
2014-09-18 09:03:29 -04:00
When a cursor is no longer needed, destroy it with @ref glfwDestroyCursor.
@code
glfwDestroyCursor(cursor);
@endcode
2014-10-02 11:35:10 -04:00
Cursor destruction always succeeds. All cursors remaining when @ref
2014-09-18 09:03:29 -04:00
glfwTerminate is called are destroyed as well.
2015-01-17 19:55:25 -05:00
@subsubsection cursor_set Cursor setting
2014-09-18 09:03:29 -04:00
A cursor can be set as current for a window with @ref glfwSetCursor.
@code
glfwSetCursor(window, cursor);
@endcode
Once set, the cursor image will be used as long as the system cursor is over the
2015-01-17 19:55:25 -05:00
client area of the window and the [cursor mode](@ref cursor_mode) is set
2014-09-18 09:03:29 -04:00
to `GLFW_CURSOR_NORMAL`.
A single cursor may be set for any number of windows.
To remove a cursor from a window, set the cursor of that window to `NULL`.
@code
glfwSetCursor(window, NULL);
@endcode
When a cursor is destroyed, it is removed from any window where it is set. This
does not affect the cursor modes of those windows.
2015-01-17 19:55:25 -05:00
@subsection cursor_enter Cursor enter/leave events
2014-09-18 09:03:29 -04:00
If you wish to be notified when the cursor enters or leaves the client area of
2014-10-02 11:35:10 -04:00
a window, set a cursor enter/leave callback.
2014-09-18 09:03:29 -04:00
@code
glfwSetCursorEnterCallback(window, cursor_enter_callback);
@endcode
The callback function receives the new classification of the cursor.
@code
void cursor_enter_callback(GLFWwindow* window, int entered)
{
if (entered)
{
// The cursor entered the client area of the window
}
else
{
// The cursor left the client area of the window
}
}
@endcode
@subsection input_mouse_button Mouse button input
If you wish to be notified when a mouse button is pressed or released, set
2014-10-02 11:35:10 -04:00
a mouse button callback.
2014-09-18 09:03:29 -04:00
@code
glfwSetMouseButtonCallback(window, mouse_button_callback);
@endcode
The callback function receives the [mouse button](@ref buttons), button action
and [modifier bits](@ref mods).
@code
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
popup_menu();
}
@endcode
The action is one of `GLFW_PRESS` or `GLFW_RELEASE`.
Mouse button states for [named buttons](@ref buttons) are also saved in
per-window state arrays that can be polled with @ref glfwGetMouseButton.
@code
int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);
if (state == GLFW_PRESS)
upgrade_cow();
@endcode
The returned state is one of `GLFW_PRESS` or `GLFW_RELEASE`.
This function only returns cached mouse button event state. It does not poll
the system for the current state of the mouse button.
Whenever you poll state, you risk missing the state change you are looking for.
If a pressed mouse button is released again before you poll its state, you will have
missed the button press. The recommended solution for this is to use a
mouse button callback, but there is also the `GLFW_STICKY_MOUSE_BUTTONS`
input mode.
@code
glfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, 1);
@endcode
When sticky mouse buttons mode is enabled, the pollable state of a mouse button
will remain `GLFW_PRESS` until the state of that button is polled with @ref
glfwGetMouseButton. Once it has been polled, if a mouse button release event
had been processed in the meantime, the state will reset to `GLFW_RELEASE`,
otherwise it will remain `GLFW_PRESS`.
The `GLFW_MOUSE_BUTTON_LAST` constant holds the highest value of any
[named button](@ref buttons).
2015-01-17 19:55:25 -05:00
@subsection scrolling Scroll input
2014-09-18 09:03:29 -04:00
If you wish to be notified when the user scrolls, whether with a mouse wheel or
2014-10-02 11:35:10 -04:00
touchpad gesture, set a scroll callback.
2014-09-18 09:03:29 -04:00
@code
glfwSetScrollCallback(window, scroll_callback);
@endcode
The callback function receives two-dimensional scroll offsets.
@code
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
}
@endcode
A simple mouse wheel, being vertical, provides offsets along the Y-axis.
2015-01-17 19:55:25 -05:00
@section joystick Joystick input
2014-04-23 07:30:11 -04:00
2014-09-18 09:03:29 -04:00
The joystick functions expose connected joysticks and controllers, with both
referred to as joysticks. It supports up to sixteen joysticks, ranging from
`GLFW_JOYSTICK_1`, `GLFW_JOYSTICK_2` up to `GLFW_JOYSTICK_LAST`. You can test
whether a [joystick](@ref joysticks) is present with @ref glfwJoystickPresent.
@code
int present = glfwJoystickPresent(GLFW_JOYSTICK_1);
@endcode
When GLFW is initialized, detected joysticks are added to to the beginning of
the array, starting with `GLFW_JOYSTICK_1`. Once a joystick is detected, it
keeps its assigned index until it is disconnected, so as joysticks are connected
and disconnected, they will become spread out.
Joystick state is updated as needed when a joystick function is called and does
not require a window to be created or @ref glfwPollEvents or @ref glfwWaitEvents
to be called.
To see all the properties of all connected joysticks in real-time, run the
`joysticks` test program.
2014-09-18 09:03:29 -04:00
2015-01-17 19:55:25 -05:00
@subsection joystick_axis Joystick axis states
2014-09-18 09:03:29 -04:00
The positions of all axes of a joystick are returned by @ref
glfwGetJoystickAxes. See the reference documentation for the lifetime of the
returned array.
@code
int count;
const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &count);
@endcode
Each element in the returned array is a value between -1.0 and 1.0.
2015-01-17 19:55:25 -05:00
@subsection joystick_button Joystick button states
2014-09-18 09:03:29 -04:00
The states of all buttons of a joystick are returned by @ref
glfwGetJoystickButtons. See the reference documentation for the lifetime of the
returned array.
@code
int count;
const unsigned char* axes = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &count);
@endcode
Each element in the returned array is either `GLFW_PRESS` or `GLFW_RELEASE`.
2015-01-17 19:55:25 -05:00
@subsection joystick_name Joystick name
2014-09-18 09:03:29 -04:00
The human-readable, UTF-8 encoded name of a joystick is returned by @ref
glfwGetJoystickName. See the reference documentation for the lifetime of the
returned string.
@code
const char* name = glfwGetJoystickName(GLFW_JOYSTICK_1);
@endcode
Joystick names are not guaranteed to be unique. Two joysticks of the same model
and make may have the same name. Only the [joystick token](@ref joysticks) is
guaranteed to be unique, and only until that joystick is disconnected.
2015-12-13 11:38:50 -05:00
@subsection joystick_event Joystick configuration changes
If you wish to be notified when a joystick is connected or disconnected, set
a joystick callback.
@code
glfwSetJoystickCallback(joystick_callback);
@endcode
The callback function receives the ID of the joystick that has been connected
and disconnected and the event that occurred.
@code
2016-10-09 21:24:07 -04:00
void joystick_callback(int jid, int event)
2015-12-13 11:38:50 -05:00
{
if (event == GLFW_CONNECTED)
{
// The joystick was connected
}
else if (event == GLFW_DISCONNECTED)
{
// The joystick was disconnected
}
}
@endcode
For joystick connection and disconnection events to be delivered on all
platforms, you need to call one of the [event processing](@ref events)
functions. Joystick disconnection may also be detected and the callback
called by joystick functions. The function will then return whatever it
returns for a disconnected joystick.
2015-12-13 11:38:50 -05:00
2015-01-17 19:55:25 -05:00
@section time Time input
2014-09-18 09:03:29 -04:00
GLFW provides high-resolution time input, in seconds, with @ref glfwGetTime.
@code
double seconds = glfwGetTime();
@endcode
It returns the number of seconds since the timer was started when the library
was initialized with @ref glfwInit. The platform-specific time sources used
usually have micro- or nanosecond resolution.
You can modify the reference time with @ref glfwSetTime.
@code
glfwSetTime(4.0);
@endcode
This sets the timer to the specified time, in seconds.
2016-03-06 08:11:14 -05:00
You can also access the raw timer value, measured in 1 / frequency
seconds, with @ref glfwGetTimerValue.
@code
uint64_t value = glfwGetTimerValue();
@endcode
The frequency of the raw timer varies depending on what time sources are
available on the machine. You can query its frequency, in Hz, with @ref
glfwGetTimerFrequency.
@code
uint64_t freqency = glfwGetTimerFrequency();
@endcode
2014-09-18 09:03:29 -04:00
2015-01-17 19:55:25 -05:00
@section clipboard Clipboard input and output
2014-09-18 09:03:29 -04:00
If the system clipboard contains a UTF-8 encoded string or if it can be
converted to one, you can retrieve it with @ref glfwGetClipboardString. See the
reference documentation for the lifetime of the returned string.
@code
const char* text = glfwGetClipboardString(window);
if (text)
insert_text(text);
2014-09-18 09:03:29 -04:00
@endcode
If the clipboard is empty or if its contents could not be converted, `NULL` is
returned.
2014-09-18 09:03:29 -04:00
The contents of the system clipboard can be set to a UTF-8 encoded string with
@ref glfwSetClipboardString.
@code
glfwSetClipboardString(window, "A string with words in it");
@endcode
The clipboard functions take a window handle argument because some window
systems require a window to communicate with the system clipboard. Any valid
window may be used.
2015-01-17 19:55:25 -05:00
@section path_drop Path drop input
2014-09-18 09:03:29 -04:00
If you wish to receive the paths of files and/or directories dropped on
2014-10-02 11:35:10 -04:00
a window, set a file drop callback.
2014-09-18 09:03:29 -04:00
@code
glfwSetDropCallback(window, drop_callback);
@endcode
The callback function receives an array of paths encoded as UTF-8.
@code
void drop_callback(GLFWwindow* window, int count, const char** paths)
{
int i;
for (i = 0; i < count; i++)
handle_dropped_file(paths[i]);
}
@endcode
The path array and its strings are only valid until the file drop callback
returns, as they may have been generated specifically for that event. You need
to make a deep copy of the array if you want to keep the paths.
2014-04-23 07:30:11 -04:00
*/