SFML community forums

Help => General => Topic started by: Rhubarb on May 28, 2024, 02:47:30 pm

Title: Keyboard Press Once [SOLVED]
Post by: Rhubarb on May 28, 2024, 02:47:30 pm
Hello, this is my first post here, so I don't know if it's the right place to ask, but I have an issue with SFML.
The "sf::Keyboard::" works, but only with the F1 to F12 and with Num1 to Num0, it doesn't works with the letters or anything else, I don't know why.

Thanks if you can help, you can answer in french.

edit:
I work with VSCode on Debian 12 and I build with g++
Title: Re: Keyboard big issue
Post by: eXpl0it3r on May 28, 2024, 03:16:25 pm
Can you share some minimal and compiable code that reproduces the issue?
Title: Re: Keyboard big issue
Post by: Rhubarb on May 28, 2024, 03:30:40 pm
Yes, I have a function thats takes input once, and not every frames here it is:
int isPressed(sf::Event event,sf::Keyboard::Key key){
    if(event.type == (sf::Event::KeyPressed)){
        if (event.key.code == key&&function_done==0){
            function_done=1;
            return 0;
        };};
        if(event.type == (sf::Event::KeyReleased)){
            if (event.key.code == key&&function_done==1)function_done=0;
    };
    return 1;
};

So it works with all keys I mentionned above but not with letters.
Title: Re: Keyboard big issue
Post by: eXpl0it3r on May 28, 2024, 03:57:55 pm
That's some interesting code formatting :D
(Btw. you don't need a semicolon after an if-body)

When you say for "letters" are you passing in as key for example sf::Keyboard::A?
Title: Re: Keyboard big issue
Post by: Rhubarb on May 28, 2024, 04:11:52 pm
Yes, that's exactly that I tried O,P,C,A,V none worked, so I assume none letter is working.
And I just love semicolons lol
Title: Re: Keyboard big issue
Post by: eXpl0it3r on May 28, 2024, 04:29:00 pm
Are you using a special keyboard layout?

So the following example application doesn't work?

#include <SFML/Graphics.hpp>

int main()
{
    auto window = sf::RenderWindow{ { 1920u, 1080u }, "Color Switcher" };
    window.setFramerateLimit(144);

    auto backgroundColor = sf::Color::Black;

    while (window.isOpen())
    {
        for (auto event = sf::Event{}; window.pollEvent(event);)
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
            else if (event.type == sf::Event::KeyPressed && event.code == sf::Keyboard::R)
            {
                backgroundColor = sf::Color::Red;
            }
            else if (event.type == sf::Event::KeyRelease && event.code == sf::Keyboard::G)
            {
                backgroundColor = sf::Color::Green;
            }
        }

        window.clear(backgroundColor);
        window.display();
    }
}

If you use SFML 2.6, can you try scancodes instead?
Title: Re: Keyboard big issue
Post by: Rhubarb on May 28, 2024, 04:35:34 pm
I installed SFML with apt-get so I have the 2.6 (I guess?) and I don't know why but the event.code you gave me doesn't work
Title: Re: Keyboard big issue
Post by: eXpl0it3r on May 28, 2024, 04:55:17 pm
Set a break point inside the key pressed event body, run the application through a debugger and check what the value of the key code is.

I have a feeling that you either have a different layout configured that you're expecting, so hitting the specific key doesn't actually trigger something. Or you have something installed on Debian that remaps keyboard values.

It's unlikely an SFML issue, since we've been using basically the same code for a long time now.
Title: Re: Keyboard big issue
Post by: Rhubarb on May 28, 2024, 10:24:16 pm
I did this, and no letters are detected.
if(event.type == (sf::Event::KeyPressed)){
    std::cout << rand()%255 << std::endl;
};
 
Only functions keys, top keyboard numbers and some keys like "inser" "end" "page up" "page down" etc
The problem is also maybe because I have an french Azerty keyboard.
Title: Re: Keyboard big issue
Post by: eXpl0it3r on May 29, 2024, 02:09:49 am
What version of SFML are you using?
Title: Re: Keyboard big issue
Post by: Rhubarb on May 29, 2024, 09:34:39 am
I'm using SFML 2.6
Title: Re: Keyboard big issue
Post by: eXpl0it3r on May 29, 2024, 01:01:52 pm
What window manager are you using? Is this running wayland or xlib?
Title: Re: Keyboard big issue
Post by: Rhubarb on May 30, 2024, 08:56:48 am
I'm using Xlib, x11 to be more precise
Title: Re: Keyboard big issue
Post by: Rhubarb on May 30, 2024, 03:47:37 pm
UPDATE:

I fixed the problem by modifying my function:

int isPressed(sf::Event event,sf::Keyboard::Key key){
    if (sf::Keyboard::isKeyPressed(key)&&function_done==0){
        function_done=1;
        return 0;
    };
    if(event.type == (sf::Event::KeyReleased)){
        if (event.key.code == key&&function_done==1){
            function_done=0;
        }else
            if (!(sf::Keyboard::isKeyPressed(key))){
                function_done=0;
            };
    };
    return 1;
};

So it fixed two problems:

now letters works
and now it works instantly because before you needed to input the key at least once before it works because the function was calling the keyrealsed event :)

thanks for the help also :)
Title: Re: Keyboard Press Once [SOLVED]
Post by: eXpl0it3r on May 31, 2024, 08:35:31 am
While sf::Keyboard::isKeyPressed may "work", it's not the intended usage to mix it with events.

Unfortunately, I don't know what's causing the events to not have the right value.