Give access to the real-time states of keyboard, mouse and joysticks. More...
#include <Input.hpp>
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 |
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.
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].
joystick | Index of the joystick to test (0 or 1) | |
axis | Axis to test |
int sf::Input::GetMouseX | ( | ) | const |
int sf::Input::GetMouseY | ( | ) | const |
bool sf::Input::IsJoystickButtonDown | ( | unsigned int | joystick, | |
unsigned int | button | |||
) | const |
bool sf::Input::IsKeyDown | ( | Key::Code | key | ) | const |
bool sf::Input::IsMouseButtonDown | ( | Mouse::Button | button | ) | const |