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

Author Topic: Limitations of repeated key events  (Read 12939 times)

0 Members and 1 Guest are viewing this topic.

Neroku

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Limitations of repeated key events
« on: August 01, 2020, 03:01:54 pm »
Hello,

I want to rely on the support for event repetition. However, it isn't behaving the way I was expecting. Consider the following program:

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

auto main() -> int {
        sf::RenderWindow win(sf::VideoMode(100, 100), "KeyTest");
        win.setFramerateLimit(8);
        win.setKeyRepeatEnabled(true);
       
        while (win.isOpen()) {

                // consume the events
                for (sf::Event event; win.pollEvent(event);)
                {
                        if (event.type == sf::Event::KeyPressed)
                        {
                                if (event.key.code == sf::Keyboard::A)
                                        std::cout << "A";
                                else if (event.key.code == sf::Keyboard::B)
                                        std::cout << "B";
                        } else if (event.type == sf::Event::Closed)
                                return 0;
                }

                std::cout << std::flush;

        } // while
}
 

With this code, if I press A, then "A" is displayed. If I now press B while still holding A pressed, then "B" is displayed but "A" is no longer displayed (the KeyPressed event for A is no longer generated even though A is still pressed). Finally, if I release B while still holding A pressed, then nothing is printed (no repeated event KeyPressed is generated for A, but is pressed). However, if I released A instead of B, then "B" would have still been printed (the KeyReleased event for A does not affect the repetition of the KeyPress event for B).

My guess is that an event is only repeated if this potential event to be repeated does exactly match the last event that was generated with the same type (i.e., same value in the type data member of the sf::Event).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Limitations of repeated key events
« Reply #1 on: August 01, 2020, 05:49:33 pm »
And your question is?

Note that this behaviour is controlled by the OS, not by SFML. You have the exact same anywhere you can type text.
Laurent Gomila - SFML developer

Neroku

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Limitations of repeated key events
« Reply #2 on: August 01, 2020, 07:35:02 pm »
I would like to know whether or not my assumptions are correct.

So, SFML just maps kind of OS events into their own events.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Limitations of repeated key events
« Reply #3 on: August 01, 2020, 10:26:21 pm »
no, the problem is the line
else if (event.key.code == sf::Keyboard::B)
by using "else if", this code is going to be executed only if A is NOT pressed. if you want both to be checked, just remove the "else"
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Neroku

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Limitations of repeated key events
« Reply #4 on: August 01, 2020, 10:33:17 pm »
no, the problem is the line
else if (event.key.code == sf::Keyboard::B)
by using "else if", this code is going to be executed only if A is NOT pressed. if you want both to be checked, just remove the "else"

I don't think that's the issue. I'm handling a single event per iteration of the for loop, i.e., I'm only pulling one event from the event queue at a time with sf::Window::pollEvent() and there can't be an event whose code member is both A and B at the same time.

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Limitations of repeated key events
« Reply #5 on: August 02, 2020, 02:10:06 pm »
ops, you're right. it's just one event checked at a time.
but as Laurent said, I tested it in a text editor and it gives the exact same results  :(
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Neroku

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Limitations of repeated key events
« Reply #6 on: August 02, 2020, 03:49:21 pm »
I tested it in a text editor and it gives the exact same results  :(

Good suggestion, I opened a text editor and got exactly the same behavior  ???
It looks then that the limitation isn't introduced by SFML, but it is inherited from the OS just as Laurent said. I wasn't pretty sure what was meant by:

You have the exact same anywhere you can type text.

Know I understand  8). Thank you all  :)