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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - km.21

Pages: [1]
1
General / 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,

Pages: [1]