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

Author Topic: [SOLEVD] Keyboard input check always returns true, even if not supposed to  (Read 1175 times)

0 Members and 2 Guests are viewing this topic.

ToneXum

  • Newbie
  • *
  • Posts: 6
    • View Profile
Hello,

I've just recently tried to check for specific keypress on my keyboard.

Problem is that the statement returns true even if the key, the method supposed to check is not pressed.

In the code bellow you can see I've tried different methods (wich I have commented out) and I have always gotten the same result.

And just for context, I have tried moving a rectangle here.

This is the entire Code.

(click to show/hide)

Code was Edited 1 time

The Result: The rectangle doesnt move

The output even states that with every event that happenes "accelRight" becomes true, even if the event is not pressing the F key.

Any help is much appreciated,

Thanks
« Last Edit: December 03, 2022, 10:41:50 pm by ToneXum »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Keyboard input check always returns true, even if not supposed to
« Reply #1 on: December 03, 2022, 08:15:27 pm »
Only use the .key.code value of your event when it is relevant: after you have checked that your event type is KeyPressed or KeyReleased, like in the tutorial.

If you don't use curly brackets around your if, only the first instruction following the if will be dependent of the result of your condition.
if (event.key.code == 100)
     accelRight = true;
     std::cout << "accelRight is now true";
This thing you wrote is equivalent to:
if (event.key.code == 100) {
     accelRight = true;
}
std::cout << "accelRight is now true";
Your std::cout will always be called, whether the if is true or false...


if (sf::Event::KeyReleased)
This doesn't mean anything  ??? It's literally like if (6) or whatever the value of the KeyReleased enum is.

ToneXum

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Keyboard input check always returns true, even if not supposed to
« Reply #2 on: December 03, 2022, 08:29:43 pm »
That with the cout is really my bad, defenetly could have seen that one ::)

I have also changed the "if" so that it uses "isKeyPressed" (just like in the tutorial)

Lastly, I let it print out whenever the if statemant for the "KeyReleased" fires

Problem is, whenever anything happenes (any event is called, like moving your mouse), it suddently says that all the if statements are true.

So the problem remains.

By the way, thanks for your quick response :)
« Last Edit: December 03, 2022, 08:46:40 pm by ToneXum »

ToneXum

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: [SOLEVD] Keyboard input check always returns true, even if not supposed to
« Reply #3 on: December 03, 2022, 10:44:39 pm »
I have found out that the problem is not the false firing of the if statement wich controlles the acutall input but rather the false firing of the resetting.

In the code you can see that whenever a key is released it sets the movement to false wich ultimatly ruined my code.

Thanks to G. anyways for responding.

 

anything