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,