Give access to the real-time state of the joysticks.
sf::Joystick provides an interface to the state of the joysticks. Each joystick is identified by an index that is passed to the functions in this namespace.
This namespace 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:
- 8 joysticks (sf::Joystick::Count)
- 32 buttons per joystick (sf::Joystick::ButtonCount)
- 8 axes per joystick (sf::Joystick::AxisCount)
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:
SFML_WINDOW_API bool isButtonPressed(unsigned int joystick, unsigned int button)
Check if a joystick button is pressed.
SFML_WINDOW_API bool isConnected(unsigned int joystick)
Check if a joystick is connected.
@ X
The X axis.
Definition Joystick.hpp:56
@ Y
The Y axis.
Definition Joystick.hpp:57
SFML_WINDOW_API bool hasAxis(unsigned int joystick, Axis axis)
Check if a joystick supports a given axis.
SFML_WINDOW_API unsigned int getButtonCount(unsigned int joystick)
Return the number of buttons supported by a joystick.
SFML_WINDOW_API float getAxisPosition(unsigned int joystick, Axis axis)
Get the current position of a joystick axis.
- See also
- sf::Keyboard, sf::Mouse