Class | SFML::Input |
In: |
window/main.cpp
|
Parent: | Object |
The SFML::Input class provides a way to access the state of keys, mouse buttons, mouse position, joystick buttons and jostick axis.
SFML::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 SFML::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 SFML::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 SFML::Input cannot be created directly, they must be retrieved from a window (SFML::Window) with the SFML::Window#input method.
Usage example:
# Retrieve the input object attached to our window input = window.input # Move an entity according to the current keys state offset = 5.0 * window.frameTime # 5 pixels/sec entity.move( -offset, 0 ) if input.keyDown?( SFML::Key::Left ) entity.move( offset, 0 ) if input.keyDown?( SFML::Key::Right ) entity.move( 0, -offset ) if input.keyDown?( SFML::Key::Up ) entity.move( 0, offset ) if input.keyDown?( SFML::Key::Down )
This will create a new input object. though it will not receive any updates of events. You should aquire an input object from the SFML::Window#input method.
The returned position is in the range [-100, 100], except the POV which is an angle and is thus defined in [0, 360].