• Advertisement
Sign in to follow this  

Using sfml2.0 to input a name

This topic is 1903 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello.
Currently i am trying to find a easy way to make a input name in SFML 2.0

What i am trying to do is something like this

std::string Name;
case sf::Event::KeyPressed: //When key is pressed
Name.push_back(EventList.key.code); //Push back the key code
break;


The problem in this is that the EventList.key.code is SFML enum that is not "ascii" i think.
So what do i do?

This is the code i am using to add to namestring when a key is pressed, and it works flawlessly
But i want something more efficient!

//objApp.objHeroCreator.NameString is a std::string
case sf::Event::KeyPressed:
switch(EventList.key.code)
{
case sf::Keyboard::Num1:
if(Shift)
objApp.objHeroCreator.NameString.push_back('!');
else
objApp.objHeroCreator.NameString.push_back('1');
break;
case sf::Keyboard::Num2:
if(Shift)
objApp.objHeroCreator.NameString.push_back('@');
else
objApp.objHeroCreator.NameString.push_back('2');
break;
case sf::Keyboard::Num3:
if(Shift)
objApp.objHeroCreator.NameString.push_back('#');
else
objApp.objHeroCreator.NameString.push_back('3');
break;
case sf::Keyboard::Num4:
if(Shift)
objApp.objHeroCreator.NameString.push_back('$');
else
objApp.objHeroCreator.NameString.push_back('4');
break;
case sf::Keyboard::Num5:
if(Shift)
objApp.objHeroCreator.NameString.push_back('%');
else
objApp.objHeroCreator.NameString.push_back('5');
break;
case sf::Keyboard::Num6:
if(Shift)
objApp.objHeroCreator.NameString.push_back('^');
else
objApp.objHeroCreator.NameString.push_back('6');
break;
case sf::Keyboard::Num7:
if(Shift)
objApp.objHeroCreator.NameString.push_back('&');
else
objApp.objHeroCreator.NameString.push_back('7');
break;
case sf::Keyboard::Num8:
if(Shift)
objApp.objHeroCreator.NameString.push_back('*');
else
objApp.objHeroCreator.NameString.push_back('8');
break;
case sf::Keyboard::Num9:
if(Shift)
objApp.objHeroCreator.NameString.push_back('(');
else
objApp.objHeroCreator.NameString.push_back('9');
break;
case sf::Keyboard::Num0:
if(Shift)
objApp.objHeroCreator.NameString.push_back(')');
else
objApp.objHeroCreator.NameString.push_back('0');
break;
case sf::Keyboard::Return:
sf::Mouse::setPosition(sf::Vector2i(objApp.Screen.getPosition().x + SCREEN_WIDTH /2,
objApp.Screen.getPosition().y + SCREEN_HEIGHT /2));
break;
case sf::Keyboard::Delete:
if(objApp.objHeroCreator.NameString.size() > 0)
{
objApp.objHeroCreator.NameString.erase(objApp.objHeroCreator.NameString.size() - 1);
}
break;
case sf::Keyboard::Back:
if(objApp.objHeroCreator.NameString.size() > 0)
{
objApp.objHeroCreator.NameString.erase(objApp.objHeroCreator.NameString.size() - 1);
}
break;
case sf::Keyboard::Space:
if(objApp.objHeroCreator.NameString.size() > 0)
objApp.objHeroCreator.NameString.push_back(' ');
break;
case sf::Keyboard::Q:
if(Shift)
objApp.objHeroCreator.NameString.push_back('Q');
else
objApp.objHeroCreator.NameString.push_back('q');
break;
case sf::Keyboard::W:
if(Shift)
objApp.objHeroCreator.NameString.push_back('W');
else
objApp.objHeroCreator.NameString.push_back('w');
break;
case sf::Keyboard::E:
if(Shift)
objApp.objHeroCreator.NameString.push_back('E');
else
objApp.objHeroCreator.NameString.push_back('e');
break;
case sf::Keyboard::R:
if(Shift)
objApp.objHeroCreator.NameString.push_back('R');
else
objApp.objHeroCreator.NameString.push_back('r');
break;
case sf::Keyboard::T:
if(Shift)
objApp.objHeroCreator.NameString.push_back('T');
else
objApp.objHeroCreator.NameString.push_back('t');
break;
case sf::Keyboard::Y:
if(Shift)
objApp.objHeroCreator.NameString.push_back('Y');
else
objApp.objHeroCreator.NameString.push_back('y');
break;
case sf::Keyboard::U:
if(Shift)
objApp.objHeroCreator.NameString.push_back('U');
else
objApp.objHeroCreator.NameString.push_back('u');
break;
case sf::Keyboard::I:
if(Shift)
objApp.objHeroCreator.NameString.push_back('I');
else
objApp.objHeroCreator.NameString.push_back('i');
break;
case sf::Keyboard::o:
if(Shift)
objApp.objHeroCreator.NameString.push_back('O');
else
objApp.objHeroCreator.NameString.push_back('o');
break;
case sf::Keyboard::P:
if(Shift)
objApp.objHeroCreator.NameString.push_back('P');
else
objApp.objHeroCreator.NameString.push_back('p');
break;
case sf::Keyboard::A:
if(Shift)
objApp.objHeroCreator.NameString.push_back('A');
else
objApp.objHeroCreator.NameString.push_back('a');
break;
case sf::Keyboard::S:
if(Shift)
objApp.objHeroCreator.NameString.push_back('S');
else
objApp.objHeroCreator.NameString.push_back('s');
break;
case sf::Keyboard::D:
if(Shift)
objApp.objHeroCreator.NameString.push_back('D');
else
objApp.objHeroCreator.NameString.push_back('d');
break;
case sf::Keyboard::F:
if(Shift)
objApp.objHeroCreator.NameString.push_back('F');
else
objApp.objHeroCreator.NameString.push_back('f');
break;
case sf::Keyboard::G:
if(Shift)
objApp.objHeroCreator.NameString.push_back('G');
else
objApp.objHeroCreator.NameString.push_back('g');
break;
case sf::Keyboard::H:
if(Shift)
objApp.objHeroCreator.NameString.push_back('H');
else
objApp.objHeroCreator.NameString.push_back('h');
break;
case sf::Keyboard::J:
if(Shift)
objApp.objHeroCreator.NameString.push_back('J');
else
objApp.objHeroCreator.NameString.push_back('j');
break;
case sf::Keyboard::K:
if(Shift)
objApp.objHeroCreator.NameString.push_back('K');
else
objApp.objHeroCreator.NameString.push_back('k');
break;
case sf::Keyboard::L:
if(Shift)
objApp.objHeroCreator.NameString.push_back('L');
else
objApp.objHeroCreator.NameString.push_back('l');
break;
case sf::Keyboard::Z:
if(Shift)
objApp.objHeroCreator.NameString.push_back('Z');
else
objApp.objHeroCreator.NameString.push_back('z');
break;
case sf::Keyboard::X:
if(Shift)
objApp.objHeroCreator.NameString.push_back('X');
else
objApp.objHeroCreator.NameString.push_back('x');
break;
case sf::Keyboard::C:
if(Shift)
objApp.objHeroCreator.NameString.push_back('C');
else
objApp.objHeroCreator.NameString.push_back('c');
break;
case sf::Keyboard::V:
if(Shift)
objApp.objHeroCreator.NameString.push_back('V');
else
objApp.objHeroCreator.NameString.push_back('V');
break;
case sf::Keyboard::B:
if(Shift)
objApp.objHeroCreator.NameString.push_back('B');
else
objApp.objHeroCreator.NameString.push_back('B');
break;
case sf::Keyboard::N:
if(Shift)
objApp.objHeroCreator.NameString.push_back('N');
else
objApp.objHeroCreator.NameString.push_back('n');
break;
case sf::Keyboard::M:
if(Shift)
objApp.objHeroCreator.NameString.push_back('M');
else
objApp.objHeroCreator.NameString.push_back('m');
break;
default:
break;
}
Edited by BaneTrapper

Share this post


Link to post
Share on other sites
Advertisement
For text input, you're supposed to use sf::Event::TextEvent instead of keypresses - it wraps this for you.

TextEvent gives each character, when entered, as a UTF-32 character in a Uint32. I haven't messed around with Unicode before, but I think SFML also provides a way to convert it - check out this thread on the SFML forums.

Share this post


Link to post
Share on other sites
I was actually just working on getting a user's name today. Here is a snippet of the code I used:


while( window.pollEvent(event) )
{
switch( event.type )
{
case sf::Event::TextEntered:
if( event.text.unicode < 128 )
{
std::string name = playerName.getString();
if( event.text.unicode == 13 ) // return key
{
// finished entering name
} else if( event.text.unicode == 8 ) { // backspace
if( name.size() > 0 ) name.resize( name.size() - 1 );
} else {
name += static_cast<char>(event.text.unicode);
}
playerName.setString( name );
}
break;
// ... more cases
}
}
Edited by kidman171

Share this post


Link to post
Share on other sites

For text input, you're supposed to use sf::Event::TextEvent instead of keypresses - it wraps this for you.

TextEvent gives each character, when entered, as a UTF-32 character in a Uint32. I haven't messed around with Unicode before, but I think SFML also provides a way to convert it - check out this thread on the SFML forums.


Thanks on info


I was actually just working on getting a user's name today. Here is a snippet of the code I used:

while( window.pollEvent(event) )
{
switch( event.type )
{
case sf::Event::TextEntered:
if( event.text.unicode < 128 )
{
std::string name = playerName.getString();
if( event.text.unicode == 13 ) // return key
{
// finished entering name
} else if( event.text.unicode == 8 ) { // backspace
if( name.size() > 0 ) name.resize( name.size() - 1 );
} else {
name += static_cast<char>(event.text.unicode);
}
playerName.setString( name );
}
break;
// ... more cases
}
}



Thanks for code, good to have example shown.
That be all i need help with. Edited by BaneTrapper

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement