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

Author Topic: sf::Keyboard question  (Read 2209 times)

0 Members and 2 Guests are viewing this topic.

OrientatedCookie

  • Newbie
  • *
  • Posts: 18
    • View Profile
sf::Keyboard question
« on: April 23, 2015, 10:13:09 pm »
I am making an app that need keyboard input, and I need it so when you press a key it 'permantley' makes a sprite appear, not just while the key is pressed. Not sure how to do this. And yes I have read the tutorials and looked for other questions like this, but it says sf::keyboard has only one function which isKeyPressed. Here is the bit of code
if (sf::Keyboard:IsKeyPressed(sf::Keyboard::S))
{
//something
}
//need the sprite to draw here if the key has been pressed once
                                               

blojayble

  • Newbie
  • *
  • Posts: 19
  • whoa
    • View Profile
Re: sf::Keyboard question
« Reply #1 on: April 23, 2015, 10:17:21 pm »
bool pressed = false;
//before loop

if (sf::Keyboard:IsKeyPressed(sf::Keyboard::S))
{
      pressed = true;
}
//need the sprite to draw here if the key has been pressed once
if(pressed) drawSprite();

Something like this?             
« Last Edit: April 23, 2015, 10:19:13 pm by blojayble »
0 bottles of beer on the wall,
0 bottles of beer!
Take one down, pass it around,
4,294,967,295 bottles of beer on the wall!

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::Keyboard question
« Reply #2 on: April 23, 2015, 10:20:17 pm »
Simple.
1. Detect key press
2. Remember that key was pressed.
3. Display sprite if key was ever pressed (see 2).

Your program can save state beyond a single frame you know.

OrientatedCookie

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: sf::Keyboard question
« Reply #3 on: April 23, 2015, 10:21:31 pm »
Thanks for quick response :) I think I got it now

 

anything