Skip to content

raylib input system

Karl Hedberg edited this page Jul 12, 2023 · 6 revisions

raylib input system basically uses an event polling mechanism, centralized on raylib core module.

At the end of the game loop, EndDrawing() function is called. This function calls SwapBuffers() and PollInputEvents(), multiple input devices are scanned at that moment and states are registered.

Depending on the platform, input events are managed using different mechanisms.

Default supported input systems are: KEYBOARD, MOUSE, GAMEPAD, TOUCH

PLATFORM_DESKTOP

On this platform GLFW3 library (rglfw) is used for input management. Events polling is done using glfwPollEvents().

Input Library Details
KEYBOARD GLFW3 Values are registered through callbacks KeyCallback(), CharCallback()
MOUSE GLFW3 Values are registered through callbacks MouseButtonCallback(), MouseCursorPosCallback(), ScrollCallback()
GAMEPAD GLFW3 Values registered by (no-callbacks): glfwJoystickPresent(), glfwGetJoystickButtons(), glfwGetJoystickAxes() NOTE: Those functions do not require glfwPollEvents()
TOUCH - Not available by default. Mouse clicks are mapped to touch events using gestures API.

PLATFORM_WEB

Some inputs in this platform are directly using GLFW3 because the library has been partially ported to emscripten. Despite that, gamepad and touch inputs are directly managed using emscripten API.

WARNING: In the current implementation of GLFW in javascript, events are not handled at glfwPollEvents(). Instead, keys down and the like are set directly when a key is pressed (which ordinarily will happen outside your game loop). This means that functions such as GetKeyPressed() will have somewhat different behaviour. Specifically, in your game loop, try not to read any events after EndDrawing(). This could have different behaviour on PLATFORM_WEB. Reading events before EndDrawing() should be fine though.

Input Library Details
KEYBOARD GLFW3 Same as PLATFOM_DESKTOP, uses GLFW3 JS implementation
MOUSE GLFW3 Same as PLATFOM_DESKTOP, uses GLFW3 JS implementation
GAMEPAD Emscripten Using emscripten gamepad API: EmscriptenGamepadCallback()
TOUCH Emscripten Using emscripten touch API: EmscriptenTouchCallback()

PLATFORM_ANDROID

Android NDK provides its own events polling system: ALooper_pollAll(), called on PollInputEvents().

Using AndroidInputCallback(), all inputs can be later processed. Currently, only some inputs are processed but additional inputs could be processed

Input Library Details
KEYBOARD Android NDK Keys registered on AndroidInputCallback()
MOUSE - Mouse inputs are actually mapped with TOUCH input
GAMEPAD Android NDK Support partially implemented on AndroidInputCallback()
TOUCH Android NDK Touch points registered on AndroidInputCallback()

PLATFORM_RPI

Probably this platform inputs polling is the most complex one. Some input events are polled by multiple secondary threads.

Keyboard polling is a special case, it can use directly stdin inputs to read keycodes. In that case, stdin is being reconfigured for ASCII chars reading and reseted at the end of the program... This approach presents several problems like a standard input lose if program is not closed properly but it does not require X.org libraries or superuser rights... Second option is being implemented to avoid stdin usage.

Input Library Details
KEYBOARD - stdin is remapped to read keys using: InitKeyboard(), ProcessKeyboard(), RestoreKeyboard()
KEYBOARD libevdev Using standard input_event to read events from input device
MOUSE libevdev Threaded: InitMouse() -> EventThreadSpawn() -> EventThread(), uses input_event
GAMEPAD joystick.h Threaded: InitGamepad() -> GamepadThread(), uses js_event
TOUCH libevdev Share MOUSE implementation

Probably inputs could be further improved in all platforms, specially TOUCH inputs support and mapping with MOUSE.

Clone this wiki locally