Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Key Released event is either not properly ordered or missing when fps is limited #2191

Closed
Neko-Box-Coder opened this issue Sep 3, 2022 · 3 comments · Fixed by #2242
Closed

Comments

@Neko-Box-Coder
Copy link

Subject of the issue

Key Released event is either not properly ordered or missing when fps is limited, creating a "key stuck" effect.

Your environment

  • Your OS / distro / window manager used:
    • Manajro KDE 5.18.5
  • Your version of SFML (2.5.0, git master, etc):
    • Tested on latest snapshot, but it also existed on snapshots from April. Very likely that it has existed for quite a while.
  • Your compiler and compiler version used:
    • GCC 12
  • Special compiler flags used:
# relevant cmake commands used:
set (CMAKE_CXX_STANDARD 11)
#Find shared libraries in the same location as the executable
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-rpath -Wl,$ORIGIN")
set(CMAKE_CXX_FLAGS "-fpermissive -std=c++0x")  #For surpressing goto warning

Steps to reproduce

  1. Limit the framerate, either using built-in set frame limit method or manually set the thread to sleep.
  2. Hold any key for a few seconds and release, in this case I am using the "a" key.

Tell us how to reproduce this issue. Please provide a minimal, complete and verifiable example, you can use the follow template as a start:

#include <iostream> 
#include "SFML/Graphics.hpp"
#include "SFML/Window.hpp"
#include <thread>

int main()
{
    sf::RenderWindow window(sf::VideoMode(sf::Vector2u(500, 500)), "SFML works!");

    window.setFramerateLimit(30);

    bool pressed = false;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if(event.type == sf::Event::KeyPressed)
            {
                std::cout<<("key pressed\n");

                if(event.key.code == sf::Keyboard::Key::A)
                    pressed = true;

                continue;
            }
            else if(event.type == sf::Event::KeyReleased)
            {
                std::cout<<("key released\n");

                if(event.key.code == sf::Keyboard::Key::A)
                    pressed = false;
                continue;
            }

            if (event.type == sf::Event::Closed)
                window.close();
        }

        if(pressed)
            window.clear(sf::Color(255, 0, 0, 255));
        else
            window.clear(sf::Color(255, 255, 255, 255));

        window.display();
        // std::this_thread::sleep_for(std::chrono::milliseconds(20));
    }

    return 0;
}

Expected behavior

The release key event should always come after the key pressed event.
With the example I have given, the background should always be white unless the "a" key is being held.

Actual behavior

The release key event comes before the last key pressed event.
With the example I have given, the background gets "stuck" with the red background occasionally.

Peek 2022-09-03 18-30

@kimci86
Copy link
Contributor

kimci86 commented Sep 3, 2022

This behavior was introduced by mistake in #1850 (see #1850 (comment)).
There is currently an open PR #1894 trying to solve it, without much success for now.

@Neko-Box-Coder
Copy link
Author

Neko-Box-Coder commented Sep 3, 2022

Thanks for informing me.

I am using fcitx5 instead of ibus, so this is also affecting fcitx5 if it makes any difference.
I have also tried limiting at 60 and 90 fps but although less frequent, the problem still exists.

Given this is probably affecting most use cases with any sort of framerate locking.
Do you know if there's any plan to fix this?

Thanks.

@eXpl0it3r eXpl0it3r added this to Discussion in SFML 2.6.0 via automation Sep 5, 2022
@eXpl0it3r eXpl0it3r added this to the 2.6 milestone Sep 5, 2022
@eXpl0it3r eXpl0it3r moved this from Discussion to Review & Testing in SFML 2.6.0 Oct 24, 2022
@eXpl0it3r eXpl0it3r linked a pull request Oct 24, 2022 that will close this issue
2 tasks
@eXpl0it3r
Copy link
Member

Fixed with #2242

If anyone runs into an issue, please let us know 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
SFML 2.6.0
  
Done
Development

Successfully merging a pull request may close this issue.

3 participants