Skip to content

module SF::Joystick #

Give access to the real-time state of the joysticks

SF::Joystick provides an interface to the state of the joysticks. It only contains static functions, so it's not meant to be instantiated. Instead, each joystick is identified by an index that is passed to the functions of this module.

This module allows users to query the state of joysticks at any time and directly, without having to deal with a window and its events. Compared to the JoystickMoved, JoystickButtonPressed and JoystickButtonReleased events, SF::Joystick can retrieve the state of axes and buttons of joysticks at any time (you don't need to store and update a boolean on your side in order to know if a button is pressed or released), and you always get the real state of joysticks, even if they are moved, pressed or released when your window is out of focus and no event is triggered.

SFML supports:

Unlike the keyboard or mouse, the state of joysticks is sometimes not directly available (depending on the OS), therefore an update() function must be called in order to update the current state of joysticks. When you have a window with event handling, this is done automatically, you don't need to call anything. But if you have no window, or if you want to check joysticks state before creating one, you must call SF::Joystick.update explicitly.

Usage example:

# Is joystick #0 connected?
connected = SF::Joystick.connected?(0)

# How many buttons does joystick #0 support?
buttons = SF::Joystick.get_button_count(0)

# Does joystick #0 define a X axis?
has_x = SF::Joystick.axis?(0, SF::Joystick::X)

# Is button #2 pressed on joystick #0?
pressed = SF::Joystick.button_pressed?(0, 2)

# What's the current position of the Y axis on joystick #0?
position = SF::Joystick.get_axis_position(0, SF::Joystick::Y)

See also: SF::Keyboard, SF::Mouse

Constants#

AxisCount = 8#

Maximum number of supported axes

ButtonCount = 32#

Maximum number of supported buttons

Count = 8#

Maximum number of supported joysticks

Class methods#

.axis?(joystick : Int, axis : Joystick::Axis) : Bool#

Check if a joystick supports a given axis

If the joystick is not connected, this function returns false.

  • joystick - Index of the joystick
  • axis - Axis to check

Returns: True if the joystick supports the axis, false otherwise

View source

.button_pressed?(joystick : Int, button : Int) : Bool#

Check if a joystick button is pressed

If the joystick is not connected, this function returns false.

  • joystick - Index of the joystick
  • button - Button to check

Returns: True if the button is pressed, false otherwise

View source

.connected?(joystick : Int) : Bool#

Check if a joystick is connected

  • joystick - Index of the joystick to check

Returns: True if the joystick is connected, false otherwise

View source

.get_axis_position(joystick : Int, axis : Joystick::Axis) : Float32#

Get the current position of a joystick axis

If the joystick is not connected, this function returns 0.

  • joystick - Index of the joystick
  • axis - Axis to check

Returns: Current position of the axis, in range -100 .. 100

View source

.get_button_count(joystick : Int) : Int32#

Return the number of buttons supported by a joystick

If the joystick is not connected, this function returns 0.

  • joystick - Index of the joystick

Returns: Number of buttons supported by the joystick

View source

.get_identification(joystick : Int) : Joystick::Identification#

Get the joystick information

  • joystick - Index of the joystick

Returns: Structure containing joystick information.

View source

.update#

Update the states of all joysticks

This function is used internally by SFML, so you normally don't have to call it explicitly. However, you may need to call it if you have no window yet (or no window at all): in this case the joystick states are not updated automatically.

View source