C++ Input & Movement

From TRCCompSci - AQA Computer Science
Revision as of 13:17, 14 June 2019 by Admin (talk | contribs) (Touch Input)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Keyboard Input

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
    // move left...
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
    // move right...
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
    // quit...
}

Mouse Input

if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
    // left click...
}
// get global mouse position
sf::Vector2i position = sf::Mouse::getPosition();
// set mouse position relative to a window
sf::Mouse::setPosition(sf::Vector2i(100, 200), window);

Touch Input

if (sf::Touch::isDown(0))
{
    // touch 0 is down
}
// get global position of touch 1
sf::Vector2i globalPos = sf::Touch::getPosition(1);
// get position of touch 1 relative to a window
sf::Vector2i relativePos = sf::Touch::getPosition(1, window);

Joystick Input

// 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::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::Y);