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

Author Topic: Get KeyBoard input  (Read 6276 times)

0 Members and 1 Guest are viewing this topic.

Mikea

  • Newbie
  • *
  • Posts: 28
    • View Profile
Get KeyBoard input
« on: July 23, 2009, 12:10:08 am »
Hello, I'm making a highscore system, and to make that, I need the name of the player, and it would be really lame If i asked the player to input the name in the console. So. I'd like some help.

So far, I can only assume I need a std::string and a way to get input. right?
Check out my sfml games here

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Get KeyBoard input
« Reply #1 on: July 23, 2009, 12:34:43 am »
You need the sf::Event::TextEntered event to catch text input, and a sf::String to display it.
Laurent Gomila - SFML developer

Mikea

  • Newbie
  • *
  • Posts: 28
    • View Profile
Get KeyBoard input
« Reply #2 on: July 23, 2009, 03:49:45 am »
sorry to ask.. I looked in the Doc for it, but I can't understand how should I get the text I enter and store it in sf::string.

I have my basic event handler.

Code: [Select]
switch(Game->mAppEvent.Type)
{
case sf::Event::Closed:
//Game->PopState();
//Game->Quit();
break;
case sf::Event::TextEntered:
                          // I suppose i do the work here. right?
                        break;
case sf::Event::KeyPressed:
switch(Game->mAppEvent.Key.Code)
{
case sf::Key::P:

break;
case sf::Key::O:

break;
case sf::Key::I:

break;
case sf::Key::Return:
loopit = false;
break;
default: break;
}
break;
default: break;
}


how do I work with those two?
Check out my sfml games here

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Get KeyBoard input
« Reply #3 on: July 23, 2009, 07:43:53 am »
Code: [Select]
std::string str;
sf::String text;

while (window.IsOpened())
{
    sf::Event event;
    while (window.GetEvent(event))
    {
        if (event.Type == sf::Event::TextEntered)
        {
            // Only handle ASCII -- it's up to you if you want to handle other encodings
            if (event.Text.Unicode < 128)
            {
                str += static_cast<char>(event.Text.Unicode);
                text.SetText(str);
            }
        }
    }

    window.Draw(text);
    ...
}
Laurent Gomila - SFML developer

Mikea

  • Newbie
  • *
  • Posts: 28
    • View Profile
Get KeyBoard input
« Reply #4 on: July 23, 2009, 02:29:21 pm »
I googled some help yesterday night and found this also.

Code: [Select]
if ( event.Type == sf::Event::TextEntered )
    {
        int textSize = m_completeText.size();
        unsigned short unicode = event.Text.Unicode;

        if (unicode == 8) //If backspace
            if (textSize > 0)
                m_completeText.erase(textSize - 1, 1);
        else if (unicode >= 32 && unicode <= 126)
            m_completeText += (char)unicode;
        else if (unicode >= 192 && unicode <= 255)
            m_completeText += (char)unicode;
    }

If anyone is interested.

Thanks a lot Laurent :D
Check out my sfml games here