Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Key Released?  (Read 15510 times)

0 Members and 1 Guest are viewing this topic.

Zephilinox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Key Released?
« on: March 15, 2012, 10:21:04 pm »
I'm trying to get a functioning ping pong game working in sfml2, despite reading and re-reading the documentation, I can't figure out how to find if a key was released, so that I can stop individual paddles from moving.

Code: [Select]
switch (Event.type)
{
case sf::Event::Closed:
Window.close();
break;
            case sf::Event::MouseButtonPressed:
                Ball.setPosition(sf::Mouse::getPosition(Window).x - Texture1.getWidth() / 2, sf::Mouse::getPosition(Window).y  - Texture1.getHeight() / 2); // test, disable for release
                break;
            case sf::Event::KeyPressed:
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                    {IsMovingUp1 = true; IsMoving1 = true;}
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                    {IsMovingUp1 = false; IsMoving1 = true;}

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                    {IsMovingUp2 = true; IsMoving2 = true;}
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                    {IsMovingUp2 = false; IsMoving2 = true;}
                break;
            case sf::Event::KeyReleased: // need to figure out a way of knowing which key it was so I don't stop both paddles
                IsMoving1 = false;
                IsMoving2 = false;
                break;
default:
break;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Key Released?
« Reply #1 on: March 15, 2012, 10:38:35 pm »
Everything is explained in the tutorial.
Laurent Gomila - SFML developer

Zephilinox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Key Released?
« Reply #2 on: March 15, 2012, 10:40:36 pm »
what tutorial? I didn't think sfml2 had any tutorials.

SoulBlade

  • Newbie
  • *
  • Posts: 26
    • View Profile
Key Released?
« Reply #3 on: March 15, 2012, 10:52:42 pm »
Look in the tutorials for 1.6 or whatever.  It's pretty easy to deduce any changes and figure it all out, there really isn't much/if any modified from 1.6 to 2.0 in way of input.

Zephilinox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Key Released?
« Reply #4 on: March 15, 2012, 11:03:30 pm »
I've already looked at that, and saying the input hasn't changed really isn't true, its completely different, I've tried adapting it based on the documentation and random attempts, but I just can't get it functional, the closest I could get is this (i.e actually compile), but it stops both paddles regardless.

Code: [Select]

case sf::Event::KeyReleased: // need to figure out a way of knowing which key it was so I don't stop both paddles
                if (sf::Keyboard::W || sf::Keyboard::S)
                    IsMoving1 = false;

                if (sf::Keyboard::Up || sf::Keyboard::Down)
                    IsMoving2 = false;
                break;


all I need is a simple, single, line of code, that I can check to see if a key has been released.

SoulBlade

  • Newbie
  • *
  • Posts: 26
    • View Profile
Key Released?
« Reply #5 on: March 15, 2012, 11:10:08 pm »
If you're trying to get asynchronous input, use, for example:
"sf::Keyboard::IsKeyPressed(sf::Keyboard::A)"

As for the event keypresses, it detects when a key is pressed, I imagine it sets an internal flag saying the key is pressed, then will not trigger again until the key is released, the flag reset, and key pressed again.  So you really don't need to do any input management yourself.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Key Released?
« Reply #6 on: March 15, 2012, 11:12:13 pm »
There's also the online doc, the official examples, the forum, etc...

But anyway, here is the code:
Code: [Select]
case sf::Event::KeyReleased: // need to figure out a way of knowing which key it was so I don't stop both paddles
                if (Event.key.code == sf::Keyboard::W || Event.key.code == sf::Keyboard::S)
                    IsMoving1 = false;

                if (Event.key.code == sf::Keyboard::Up || Event.key.code == sf::Keyboard::Down)
                    IsMoving2 = false;
                break;
Laurent Gomila - SFML developer

Zephilinox

  • Newbie
  • *
  • Posts: 43
    • View Profile
Key Released?
« Reply #7 on: March 16, 2012, 04:44:51 pm »
Quote from: "Laurent"
There's also the online doc, the official examples, the forum, etc...

But anyway, here is the code:
Code: [Select]
case sf::Event::KeyReleased: // need to figure out a way of knowing which key it was so I don't stop both paddles
                if (Event.key.code == sf::Keyboard::W || Event.key.code == sf::Keyboard::S)
                    IsMoving1 = false;

                if (Event.key.code == sf::Keyboard::Up || Event.key.code == sf::Keyboard::Down)
                    IsMoving2 = false;
                break;


thanks, that works perfectly.