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

Author Topic: Mac Getting Keyboard Inputs  (Read 939 times)

0 Members and 1 Guest are viewing this topic.

km.21

  • Newbie
  • *
  • Posts: 1
    • View Profile
Mac Getting Keyboard Inputs
« on: November 26, 2020, 09:09:39 pm »
I am working on a project and used the following code to accept keyboard inputs that appends to a std::string.
***********************************
while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            switch (event.type) {
                case sf::Event::KeyPressed:
                    if (event.key.code == sf::Keyboard::Escape) {
                        window.close();
                        print("Window is closed.");
                    }
                    if (event.key.code == sf::Keyboard::Enter) {
                        print("Enter key pressed.");
                        currentState = MENU;
                        return;
                    }
                    if (event.key.code == sf::Keyboard::Backspace) {
                        playerStats.username.pop_back();
                    }
                    else if (event.text.unicode < 128) {
                        playerStats.username += static_cast<char>(event.text.unicode);
                    }
                    userInput.setString(playerStats.username);
                    break;
                case sf::Event::KeyReleased:
                    break;
                default:
                    break;
            }
        }
*******************************

Most of the character inputs from the keyboard result in a box being displaying to the sf::Text userInput.

'SPACE' results in '9'
']'         results in '/'
'\'         results in '5'

Am I using the event.text.unicode correctly or is there a better way to grab inputs from the keyboard?
Thank you for your time,

 

anything