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

Author Topic: Problems with keeping checking if button is pressed.  (Read 4134 times)

0 Members and 1 Guest are viewing this topic.

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Problems with keeping checking if button is pressed.
« on: February 15, 2015, 11:30:41 pm »
Hello

I have a problem regarding checking if an user pressed a button.

Code:
#include <iostream>

using namespace std;
int main() {
bool repeat = true;

while (repeat) {
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape) {
                cout << "Are you sure you want to quit?" <<endl;
        } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) {
                cout << "A is pressed, moving forward." <<endl;
        }
}
}

What I need to do, is checking if an user pressed a button, if it does it should print a message.

My problems:
- If I put it in a while loop to repeat checking if a button is pressed, my window gets spammed with "A is pressed, moving forward". The message should just appear once.
- If I don't put a while loop, my program just terminates without me even having the chance to type.

How do I keep repeating that checking for when a button is pressed without my program terminating and without my window spammed with the couts?

Thanks for reading,
Niely

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Problems with keeping checking if button is pressed.
« Reply #1 on: February 16, 2015, 12:42:38 am »
There are at least two ways to achieve this behaviour:

1.) Use events, something like this

//in event loop (pollEvent/waitEvent)
if(event.type == sf::Event::KeyPressed)
{
    switch(event.key.code)
    {
    case sf::Keyboard::A :
        cout << "A is pressed, moving forward." <<endl;
        break;
    }
}
 

2) You can have somewhere a bool to check if the button was pressed last frame.


void somefunc()
{
    static bool stateLastFrame = false; //static to make the example simple... could be a member/local variable outside of the gameloop...
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        if(!stateLastFrame)
            cout << "A is pressed, moving forward." <<endl;
        stateLastFrame = true;
    }
    else
        stateLastFrame = false;
}

 


I hope I understood you correctly and you were asking for a way to get one "message" per key press.

AlexAUT
« Last Edit: February 16, 2015, 12:45:29 am by AlexAUT »

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Problems with keeping checking if button is pressed.
« Reply #2 on: February 16, 2015, 11:25:58 pm »
Hi!
Thanks a lot, your second option works.

However; the only problem now is; that if I typ 'fast', the program ignores some key-presses.
That stateLastFrame isn't switched fast enough to false, or that the code didn't reached the else statement to make it false.

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: Problems with keeping checking if button is pressed.
« Reply #3 on: February 18, 2015, 02:22:09 pm »
If you use bools for this you would need a bool for each key, because if you just use one bool for all, it stays true as long as any key is pressed.

I suggest using events like AlexAUT already showed you. Especially because it seems you are trying to implement a text field to enter something. Take a look at the text entered event.
Failing to succeed does not mean failing to progress!

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Problems with keeping checking if button is pressed.
« Reply #4 on: February 19, 2015, 10:35:04 pm »
I'm trying to do this with events then, because I assume that'd work better then booleans. However; the sample code provided doesn't work.

How exactly would that go with events? I already tried a few things, but those don't work.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Problems with keeping checking if button is pressed.
« Reply #5 on: February 19, 2015, 10:39:38 pm »
Have you read the tutorials? Everything is explained there: http://www.sfml-dev.org/tutorials/2.2/window-events.php

Anyways a minimal example, basically the same as in my first post:

#include <iostream>
#include <SFML/Window.hpp>

int main()
{
  sf::Window window({ 800, 600 }, "test");

  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::KeyPressed)
      {
        switch (event.key.code)
        {
        case sf::Keyboard::A:
          std::cout << "A is pressed, moving forward." << std::endl;
          break;
        }
      }
    }
  }
}
 


AlexAUT

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Problems with keeping checking if button is pressed.
« Reply #6 on: February 21, 2015, 12:02:39 am »
I already got the window to work. However, now I only need this piece to work:

Code:
#include <iostream>
#include <SFML/Window.hpp>

int main()
{


    sf::Event event;

while(true) {
      if (event.type == sf::Event::KeyPressed)
      {
        switch (event.key.code)
        {
        case sf::Keyboard::A :
          std::cout << "A is pressed, moving forward." << std::endl;
          break;
        }
      }
     }
}

This is the compiler G++:
g++ -o test test.cpp -std=c++11 -lsfml-system -lsfml-window

No compile error, however, when I press 'a' or 'A' no cout shows up. I'm using Linux Mint 17.1 by the way.

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Problems with keeping checking if button is pressed.
« Reply #7 on: February 21, 2015, 12:06:41 am »
You should go read the tutorials again. You need a window for events, and to call sf::Window::pollEvent() to get an actual event.
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

Niely

  • Full Member
  • ***
  • Posts: 101
    • View Profile
Re: Problems with keeping checking if button is pressed.
« Reply #8 on: February 21, 2015, 12:20:49 am »
I didn't needed it when I used Booleans... How do I make a 'fake' window then? I want to use CLI, not GUI.
With window by the way, also no cout shows up.
« Last Edit: February 21, 2015, 12:23:40 am by Niely »

 

anything