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

Author Topic: [SOLVED] Keyboard::isKeyPressed not working even though it worked before  (Read 4031 times)

0 Members and 1 Guest are viewing this topic.

BananaBro_Chaos

  • Newbie
  • *
  • Posts: 10
    • View Profile
As the title states, Keyboard::isKeyPressed is not working. To demonstrate this issue, I created a simple project to show that it does not work:

#include <iostream>
#include "SFML/Graphics.hpp"
#include "SFML/Window.hpp"
using namespace std;
using namespace sf;

RenderWindow window(VideoMode(1400, 840), "Game Window");

int main(){
    while(window.isOpen()){
        Event event;
       
        if(Keyboard::isKeyPressed(Keyboard::A)){
            cout << &#39;e&#39; << endl;
        }
        while (window.pollEvent(event)){
            if (event.type == Event::Closed){
                window.close();
            }
        }
    }
}

Even if I pressed A, nothing gets outputted.

Some additional information, I code in XCode on Mac; however, I can't run SFML directly from XCode. Instead, I downloaded SFML through homebrew and ran the code through Terminal. I don't know if this affects the code (it didn't affect it previously) but I figured I might add it.
« Last Edit: May 28, 2024, 05:14:58 pm by BananaBroSHSID »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11039
    • View Profile
    • development blog
    • Email
On macOS in order to use sf::Keyboard::isKeyPressed you'll need to give monitoring permission to the application.
Alternatively, you can use the KeyPressed event that doesn't need any additional permission.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BananaBro_Chaos

  • Newbie
  • *
  • Posts: 10
    • View Profile
I did give permission to both XCode and Terminal; however, it still doesn't work. What's even weirder is that I didn't change the permission settings ever. Keyboard just worked one day and didn't the next.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11039
    • View Profile
    • development blog
    • Email
The application needs the permission, not the terminal or XCode as far as I know.
If you recompile or change the application name, the permission might be removed, but I'm not sure how macOS hands this out.

Either way, I still highly recommend to track keyboard states via events.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BananaBro_Chaos

  • Newbie
  • *
  • Posts: 10
    • View Profile
nevermind it fully works now apparently I needed to restart the computer and that's all

 

anything