SFML 3.0.1
Loading...
Searching...
No Matches
Classes | Enumerations | Functions | Variables
sf::Joystick Namespace Reference
Window module

Give access to the real-time state of the joysticks. More...

## Classes
struct | Identification
| Structure holding a joystick's identification. More...

## Enumerations
enum class | Axis { X , Y , Z , R , U , V , PovX , PovY }
| Axes supported by SFML joysticks. More...

## Functions
bool | isConnected (unsigned int joystick)
| Check if a joystick is connected.
unsigned int | getButtonCount (unsigned int joystick)
| Return the number of buttons supported by a joystick.
bool | hasAxis (unsigned int joystick, Axis axis)
| Check if a joystick supports a given axis.
bool | isButtonPressed (unsigned int joystick, unsigned int button)
| Check if a joystick button is pressed.
float | getAxisPosition (unsigned int joystick, Axis axis)
| Get the current position of a joystick axis.
Identification | getIdentification (unsigned int joystick)
| Get the joystick information.
void | update ()
| Update the states of all joysticks.

## Variables
static constexpr unsigned int | Count {8}
| Constants related to joysticks capabilities.
static constexpr unsigned int | ButtonCount {32}
| Maximum number of supported buttons.
static constexpr unsigned int | AxisCount {8}
| Maximum number of supported axes.

## Detailed Description

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:

// Is joystick #0 connected?
bool connected = sf::Joystick::isConnected(0);
// How many buttons does joystick #0 support?
unsigned int buttons = sf::Joystick::getButtonCount(0);
// Does joystick #0 define a X axis?
bool hasX = sf::Joystick::hasAxis(0, sf::Joystick::Axis::X);
// Is button #2 pressed on joystick #0?
bool pressed = sf::Joystick::isButtonPressed(0, 2);
// What's the current position of the Y axis on joystick #0?
float position = sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::Y);
sf::Joystick::getButtonCount
unsigned int getButtonCount(unsigned int joystick)
Return the number of buttons supported by a joystick.
sf::Joystick::Axis::X
@ X
The X axis.
Definition Joystick.hpp:56
sf::Joystick::Axis::Y
@ Y
The Y axis.
Definition Joystick.hpp:57
sf::Joystick::getAxisPosition
float getAxisPosition(unsigned int joystick, Axis axis)
Get the current position of a joystick axis.
sf::Joystick::isConnected
bool isConnected(unsigned int joystick)
Check if a joystick is connected.
sf::Joystick::isButtonPressed
bool isButtonPressed(unsigned int joystick, unsigned int button)
Check if a joystick button is pressed.
sf::Joystick::hasAxis
bool hasAxis(unsigned int joystick, Axis axis)
Check if a joystick supports a given axis.

See also
  sf::Keyboard, sf::Mouse

## Enumeration Type Documentation

## ◆ Axis

enum class sf::Joystick::Axis
| strong

Axes supported by SFML joysticks.

Enumerator
X | The X axis.
Y | The Y axis.
Z | The Z axis.
R | The R axis.
U | The U axis.
V | The V axis.
PovX | The X axis of the point-of-view hat.
PovY | The Y axis of the point-of-view hat.

Definition at line 54 of file Joystick.hpp.

## Function Documentation

## ◆ getAxisPosition()

float sf::Joystick::getAxisPosition | ( | unsigned int | joystick,
|  | Axis | axis )
| nodiscard

Get the current position of a joystick axis.

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

Parameters

  joystick | Index of the joystick
  axis | Axis to check

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

## ◆ getButtonCount()

unsigned int sf::Joystick::getButtonCount | ( | unsigned int | joystick | ) |
| nodiscard

Return the number of buttons supported by a joystick.

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

Parameters

  joystick | Index of the joystick

Returns
  Number of buttons supported by the joystick

## ◆ getIdentification()

Identification sf::Joystick::getIdentification | ( | unsigned int | joystick | ) |
| nodiscard

Get the joystick information.

Parameters

  joystick | Index of the joystick

Returns
  Structure containing joystick information.

## ◆ hasAxis()

bool sf::Joystick::hasAxis | ( | unsigned int | joystick,
|  | Axis | axis )
| nodiscard

Check if a joystick supports a given axis.

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

Parameters

  joystick | Index of the joystick
  axis | Axis to check

Returns
  true if the joystick supports the axis, false otherwise

## ◆ isButtonPressed()

bool sf::Joystick::isButtonPressed | ( | unsigned int | joystick,
|  | unsigned int | button )
| nodiscard

Check if a joystick button is pressed.

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

Parameters

  joystick | Index of the joystick
  button | Button to check

Returns
  true if the button is pressed, false otherwise

## ◆ isConnected()

bool sf::Joystick::isConnected | ( | unsigned int | joystick | ) |
| nodiscard

Check if a joystick is connected.

Parameters

  joystick | Index of the joystick to check

Returns
  true if the joystick is connected, false otherwise

## ◆ update()

void sf::Joystick::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.

## Variable Documentation

## ◆ AxisCount

unsigned int sf::Joystick::AxisCount {8}
| static constexpr

Maximum number of supported axes.

Definition at line 47 of file Joystick.hpp.

## ◆ ButtonCount

unsigned int sf::Joystick::ButtonCount {32}
| static constexpr

Maximum number of supported buttons.

Definition at line 46 of file Joystick.hpp.

## ◆ Count

unsigned int sf::Joystick::Count {8}
| static constexpr

Constants related to joysticks capabilities.

Maximum number of supported joysticks

Definition at line 45 of file Joystick.hpp.