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

Author Topic: sf::keyboard::key seems to be missing  (Read 1108 times)

0 Members and 1 Guest are viewing this topic.

Moonglum

  • Newbie
  • *
  • Posts: 10
    • View Profile
sf::keyboard::key seems to be missing
« on: March 04, 2023, 09:03:55 am »
Trying to use sf::keyboard:key to read keyboard state. This doesn't seem to work for me. sf::keyboard is there but sf::keyboard::key seems to be missing and the compiler complains if I tried to just type this in. without the ::key I can compile ok, but the keyboard state does not work.  I assume I have missed something. Any help would be appreciated.

Thanks

Moon.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: sf::keyboard::key seems to be missing
« Reply #1 on: March 04, 2023, 11:27:43 am »
As you haven't really described what you're trying to do or shown some code, it's hard to say what the issue is.
The key enum is certainly there, just a question of how you're trying to use it.

I suggest to read the official tutorials on event handling and keyboard handling.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Moonglum

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: sf::keyboard::key seems to be missing
« Reply #2 on: March 04, 2023, 12:13:35 pm »
Thanks for coming back to me.  I am following a tutorial. I have created a render window, defined a rectangle shape which I called "player" and shaded red. Set up a while loop whilst the render window is open and poll for a windows close or resize. I want to check if a key is pressed and change player position accordingly.

The tutorial is SFML 2.4 For Beginners - 5: Keyboard Input.
by Hilze Vonck on YouTube.


He shows sf::keyboard:key in his tutorial but that doesn't work for me.  I have tried to enter left and right as show below but the object does not move with any keys.

Code is as follows:

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

int main()
{
   
        sf::RenderWindow window(sf::VideoMode(512, 512), "SFML Tutorial", sf::Style::Close | sf::Style::Resize);
        sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
        player.setFillColor(sf::Color::Red);


        while (window.isOpen())
        {
                sf::Event evnt;

                while (window.pollEvent(evnt))
                {
                       
                        switch (evnt.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::Resized:
                                printf ("Width: %i, Height: %i\n", evnt.size.width, evnt.size.height);
                                break;
                        case sf::Event::TextEntered:
                                if (evnt.text.unicode < 128)
                                {
                                        printf("%c", evnt.text.unicode);
                                }
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                player.move(-0.1f, 0.0f);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                player.move(0.1f, 0.0f);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        {
                                player.move(0.0f, -0.1f);
                        }

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        {
                                player.move(0.0f, 0.1f);
                        }



                        window.clear();

                        window.draw(player);
                        window.display();



                }
        }


        return 0;

}

Many thanks for your time.

kojack

  • Sr. Member
  • ****
  • Posts: 314
  • C++/C# game dev teacher.
    • View Profile
Re: sf::keyboard::key seems to be missing
« Reply #3 on: March 04, 2023, 02:12:09 pm »
The isKeyPressed parts and the window clear, draw and display should be outside of the event loop (the while loop that does pollEvent), but inside of the outer while loop.
Try this: (Basically I moved the closing brace of the while loop up to above the isKeyPressed bit)

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

int main()
{
        sf::RenderWindow window(sf::VideoMode(512, 512), "SFML Tutorial", sf::Style::Close | sf::Style::Resize);
        sf::RectangleShape player(sf::Vector2f(100.0f, 100.0f));
        player.setFillColor(sf::Color::Red);

        while (window.isOpen())
        {
                sf::Event evnt;

                while (window.pollEvent(evnt))
                {
                        switch (evnt.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::Resized:
                                printf ("Width: %i, Height: %i\n", evnt.size.width, evnt.size.height);
                                break;
                        case sf::Event::TextEntered:
                                if (evnt.text.unicode < 128)
                                {
                                        printf("%c", evnt.text.unicode);
                                }
                        }
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        player.move(-0.1f, 0.0f);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        player.move(0.1f, 0.0f);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                {
                        player.move(0.0f, -0.1f);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                {
                        player.move(0.0f, 0.1f);
                }

                window.clear();
                window.draw(player);
                window.display();
        }

        return 0;
}

Moonglum

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: sf::keyboard::key seems to be missing
« Reply #4 on: March 04, 2023, 06:09:12 pm »
Hi Kojack

Good spot, that's done the trick. I think there is also a few minor syntax differences with older versions but this works fine now.  In the original code, it would only look at the keyboard when the window was being resized or closed. Moving the brace resolved this.

Many thanks for your help.  Much appreciated.

Moon.

 

anything