sf::Input Class Reference

Give access to the real-time states of keyboard, mouse and joysticks. More...

#include <Input.hpp>

Inheritance diagram for sf::Input:
sf::NonCopyable

List of all members.

Public Member Functions

 Input ()
 Default constructor.
bool IsKeyDown (Key::Code key) const
 Get the current state of a key (pressed or released).
bool IsMouseButtonDown (Mouse::Button button) const
 Get the current state of a mouse button (pressed or released).
bool IsJoystickButtonDown (unsigned int joystick, unsigned int button) const
 Get the current state of a joystick button (pressed or released).
int GetMouseX () const
 Get the current mouse X position.
int GetMouseY () const
 Get the current mouse Y position.
float GetJoystickAxis (unsigned int joystick, Joy::Axis axis) const
 Get the current position of a joystick axis.

Friends

class Window

Detailed Description

Give access to the real-time states of keyboard, mouse and joysticks.

sf::Input provides a way to access the state of keys, mouse buttons, mouse position, joystick buttons and jostick axis.

sf::Input provides the same informations as the event system, but these informations can be accessed at any time, which is more convenient in many situations.

For example, to move an entity you can decide to catch the sf::Event::KeyPressed event on arrow keys. But if you do so, you will only receive one event when the key gets pressed (or repeated events if you activated this feature), thus the entity will not move smoothly. The best solution here is to use sf::Input::IsKeyDown so that you can update your entity's position at every iteration of your game loop, not only when you catch a KeyPressed event.

Note that instances of sf::Input cannot be created directly, they must be retrieved from a window (sf::Window) with its GetInput() function.

Usage example:

 // Retrieve the input object attached to our window
 const sf::Input& input = window.GetInput();

 // Move an entity according to the current keys state
 float offset = 5 * window.GetFrameTime(); // 5 pixels/sec
 if (input.IsKeyDown(sf::Key::Left))  entity.Move(-offset, 0);
 if (input.IsKeyDown(sf::Key::Right)) entity.Move( offset, 0);
 if (input.IsKeyDown(sf::Key::Up))    entity.Move(0, -offset);
 if (input.IsKeyDown(sf::Key::Down))  entity.Move(0,  offset);

Definition at line 45 of file Input.hpp.


Constructor & Destructor Documentation

sf::Input::Input (  ) 

Default constructor.

Definition at line 34 of file Input.cpp.


Member Function Documentation

float sf::Input::GetJoystickAxis ( unsigned int  joystick,
Joy::Axis  axis 
) const

Get the current position of a joystick axis.

The returned position is in the range [-100, 100], except the POV which is an angle and is thus defined in [0, 360].

Parameters:
joystick Index of the joystick to test (0 or 1)
axis Axis to test
Returns:
Current axis position

Definition at line 81 of file Input.cpp.

int sf::Input::GetMouseX (  )  const

Get the current mouse X position.

The returned position is relative to the left border of the owner window.

Returns:
Current mouse left position

Definition at line 67 of file Input.cpp.

int sf::Input::GetMouseY (  )  const

Get the current mouse Y position.

The returned position is relative to the top border of the owner window.

Returns:
Current mouse top position

Definition at line 74 of file Input.cpp.

bool sf::Input::IsJoystickButtonDown ( unsigned int  joystick,
unsigned int  button 
) const

Get the current state of a joystick button (pressed or released).

Parameters:
joystick Index of the joystick to test (0 or 1)
button Index of the button to test
Returns:
True if button is down, false if button is up

Definition at line 57 of file Input.cpp.

bool sf::Input::IsKeyDown ( Key::Code  key  )  const

Get the current state of a key (pressed or released).

Parameters:
key Code of the key to test
Returns:
True if key is down, false if key is up

Definition at line 43 of file Input.cpp.

bool sf::Input::IsMouseButtonDown ( Mouse::Button  button  )  const

Get the current state of a mouse button (pressed or released).

Parameters:
button Code of the mouse button to check
Returns:
True if button is down, false if button is up

Definition at line 50 of file Input.cpp.


The documentation for this class was generated from the following files: