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

Author Topic: Event KeyReleased repetes itself.  (Read 1861 times)

0 Members and 1 Guest are viewing this topic.

LorenzoBrunetti

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Event KeyReleased repetes itself.
« on: June 07, 2024, 03:21:39 pm »
Hi, it's the first time i'm trying event handling but the event KeyReleased is not working properly.
The code in question looks like this:
int main() {
        sf::RenderWindow win(sf::VideoMode(800, 800), "Window");
        bool button = false;
        while (win.isOpen()) {
                sf::Event event;
                while (win.pollEvent(event))
                {
                        switch (event.type) {
                        case sf::Event::Closed:
                                win.close();
                                break;
                        case sf::Event::KeyPressed:
                                switch (event.key.code) {
                                case 0:
                                        button = true;
                                        break;
                                }
                        case sf::Event::KeyReleased:
                                switch (event.key.code) {
                                case 0:
                                        button = false;
                                        break;
                                }
                        }
                }
                if (button) {
                        std::cout << "Key pressed" << std::endl;
                }
                else {
                        std::cout << "Key not pressed" << std::endl;
                }

        }
}

I'm trying to manage key imputs as explaned in the event tutorial but as key released keeps repeting the boolean continuously returns false.
Does anyone knows how to fix it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10991
    • View Profile
    • development blog
    • Email
Re: Event KeyReleased repetes itself.
« Reply #1 on: June 07, 2024, 03:57:44 pm »
I recommend to use sf::Keyboard::A instead of integer values for the switch-case statements of the key.code.

In your code the KeyReleased event is triggered once when you let go of the A key. At that point the boolean button is set to false and as such "Key not pressed" is printed until you press the A key again.

What are you trying to achieve exactly?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

LorenzoBrunetti

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Event KeyReleased repetes itself.
« Reply #2 on: June 07, 2024, 05:34:13 pm »
I want to have a boolean that, when the key is pressed is set to true and when it's released it's set to false. The problem was that for some reason as soon as the boolean was set to true, became false.
Anyway, I used  sf::Keyboard::A instead of the integer and now the code works an intended.
Thank you.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10991
    • View Profile
    • development blog
    • Email
Re: Event KeyReleased repetes itself.
« Reply #3 on: June 07, 2024, 06:30:02 pm »
Oh, you're missing some break;

You need a break after the KeyPressed handling (and after the KeyRelease handling).
Otherwise both checks will be executed.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

LorenzoBrunetti

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Event KeyReleased repetes itself.
« Reply #4 on: June 07, 2024, 07:38:25 pm »
oh... im feeling so dumb rn.
Thank you!

 

anything