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

Author Topic: [SOLVED]sf::Keyboard::isKeyPressed is not taking inputs.  (Read 1818 times)

0 Members and 1 Guest are viewing this topic.

Lauro

  • Newbie
  • *
  • Posts: 10
    • View Profile
[SOLVED]sf::Keyboard::isKeyPressed is not taking inputs.
« on: November 27, 2019, 11:00:46 am »
hello everybody, i am new in sfml so sorry if i may ask something silly.

i have this:
void GameLoop::loop() {

    gameWindow.clear();
    sf::Event event{};

    while(gameWindow.isOpen()){
        while (gameWindow.pollEvent(event)) {
            if (event.type == sf::Event::EventType::Closed)             //close window
                gameWindow.close();

            //future events here

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            playerUp = true;
        if(!(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)))
            playerUp = false;
        std::cout << playerUp << std::endl;


        gameWindow.display();
    }
}
 

I am trying to modify playerUp that is a bool initialised as false in the constructor.
If i am not wrong, i should have understood that the difference between sf::Keyboard::isKeyPressed (as i am doing) and sf::Event::KeyPressed is that the first consider how long you press a key, while the second is only triggered when i press the key so i should handle with more code the fact that it it not released.
Considering that i want to keep the code clean I opted for isKeyPressed out of eventPoll loop.
when i launch the program this is the output:
0
We got a keyboard without any keys (1)
0
0
0
0
//there are only 0s 'till the end even though i press the key
 

I tried to use another key beside Up but with no result.
So, what does that message means? what am i doing wrong?

edit: Forgot to say i am using macOS, maybe this is something that have to be known for you to help me.

side question:
if i got it right sf:Event and sf::Keyboard should be part of Window.hpp, but i am not sure.
By the way in the header I included Graphics.hpp as seen in the tutorial to check if sfml was working and it still find those elements i mentioned, why? Could this be the problem of my question?
Also, where can i check the right "category" (window, graphics, audio ...) of the various elements of sfml? for example if i want to know which is the right category of sf::Event, the guide tell me i need to include Event.hpp, but where can i read if it belongs to window rather then graphics and so on.

sorry for my silly questions :P
« Last Edit: November 27, 2019, 07:42:46 pm by Lauro »

Lauro

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: [SOLVED]sf::Keyboard::isKeyPressed is not taking inputs.
« Reply #1 on: November 27, 2019, 07:48:37 pm »
i found the solution by myself, this is really easy and nothing related to sfml, but if someone is a new apple products user as me maybe he is not really used to think about all the permission the mac os ask (and honestly i think it's something very good).

I'm italian so i don't know how english version of macOS calls the various things, but i will translate by meaning hoping this can help.

just open Preferences via apple logo in the top left corner, security and privacy, inputs and be sure Clion or any other ide you use has the permission.

voilĂ  :D


again, this can be silly for most of people, but many others still need this kind of help.