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

Author Topic: Keyboard Press Once [SOLVED]  (Read 3041 times)

0 Members and 5 Guests are viewing this topic.

Rhubarb

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Keyboard Press Once [SOLVED]
« 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++
« Last Edit: May 30, 2024, 03:48:11 pm by Rhubarb »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Keyboard big issue
« Reply #1 on: May 28, 2024, 03:16:25 pm »
Can you share some minimal and compiable code that reproduces the issue?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Rhubarb

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Keyboard big issue
« Reply #2 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Keyboard big issue
« Reply #3 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Rhubarb

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Keyboard big issue
« Reply #4 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Keyboard big issue
« Reply #5 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Rhubarb

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Keyboard big issue
« Reply #6 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Keyboard big issue
« Reply #7 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Rhubarb

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Keyboard big issue
« Reply #8 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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Keyboard big issue
« Reply #9 on: May 29, 2024, 02:09:49 am »
What version of SFML are you using?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Rhubarb

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Keyboard big issue
« Reply #10 on: May 29, 2024, 09:34:39 am »
I'm using SFML 2.6

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Keyboard big issue
« Reply #11 on: May 29, 2024, 01:01:52 pm »
What window manager are you using? Is this running wayland or xlib?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Rhubarb

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Keyboard big issue
« Reply #12 on: May 30, 2024, 08:56:48 am »
I'm using Xlib, x11 to be more precise
« Last Edit: May 30, 2024, 01:35:14 pm by Rhubarb »

Rhubarb

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
Re: Keyboard big issue
« Reply #13 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 :)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Keyboard Press Once [SOLVED]
« Reply #14 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything