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

Author Topic: Reading key presses into a String  (Read 11682 times)

0 Members and 1 Guest are viewing this topic.

acrin1

  • Newbie
  • *
  • Posts: 45
    • View Profile
    • http://www.crpgdev.com
Reading key presses into a String
« on: November 25, 2007, 04:09:59 pm »
Hi,

I'm wanting to read in single key presses, do some error / special key checking and add the valid key presses to a c++ string. The string would be a mixture of upper and lower case letters and other symbols. Is there a simple way to do this rather than me having to check explicitly for every unique key like so?

if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::L)) myString = myString+"L"

I've looked in the documentation but it wasn't obvious to me how to do this.

In SDL I've been able to do the following in the past:

if ( event.type == SDL_KEYDOWN )
{
 if (( event.key.keysym.unicode < 0x80 ) &&
( event.key.keysym.unicode > 0 )) // it's printable
{
 keyString = (char)event.key.keysym.unicode, 1;
   
I also noticed that only there only seem to be lower case letters defined currently. Does this mean I can't differentiate between upper and lower case input?

Thanks.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Reading key presses into a String
« Reply #1 on: November 26, 2007, 02:50:54 am »
You can use a code equivalent to the SDL one you're showing (which is the right way of doing it), with the TextEntered event.

Code: [Select]
if ( event.Type == sf::Event::TextEntered )
{
    if ( event.Text.Unicode < 0x80 ) // it's printable
    {
        keyString = (char)event.Text.Unicode;
    }
}
Laurent Gomila - SFML developer

acrin1

  • Newbie
  • *
  • Posts: 45
    • View Profile
    • http://www.crpgdev.com
Reading key presses into a String
« Reply #2 on: November 26, 2007, 02:21:50 pm »
Quote from: "Laurent"
You can use a code equivalent to the SDL one you're showing (which is the right way of doing it), with the TextEntered event.

Code: [Select]
if ( event.Type == sf::Event::TextEntered )
{
    if ( event.Text.Unicode < 0x80 ) // it's printable
    {
        keyString = (char)event.Text.Unicode;
    }
}


That's great - I'll give it a try.

Thanks.

acrin1

  • Newbie
  • *
  • Posts: 45
    • View Profile
    • http://www.crpgdev.com
Reading key presses into a String
« Reply #3 on: November 28, 2007, 02:03:36 pm »
I've been trying to read a single key to add it to a string editing routine but don't seem to be able to get this to work. Am I taking the wrong approach to this? Should events only be checked as part of the main loop? (where this seems to work fine).

Thanks.

--

std::string readAlphaKey()
{  
      sf::Event keyEvent;
      std::string keyString;
      bool done = false;

      while (!done)
      {
         while (App.GetEvent(keyEvent))
         {
               if ((keyEvent.Type == sf::Event::KeyPressed) && (keyEvent.Key.Code == sf::Key::Return)) { keyString="RETURN"; done = true; }
            if ((keyEvent.Type == sf::Event::KeyPressed) && (keyEvent.Key.Code == sf::Key::Back)) { keyString="BACKSPACE"; done = true; }
         }
         
         if ( keyEvent.Type == sf::Event::TextEntered )
         {
            if (( keyEvent.Text.Unicode < 0x80 ) && (!done)) // it's printable
            {
               keyString = (char)keyEvent.Text.Unicode;
               done = true;   
            }
         }         
      }
      return(keyString);  
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Reading key presses into a String
« Reply #4 on: November 29, 2007, 03:08:12 am »
Quote
Should events only be checked as part of the main loop?

No, but if you already have an event loop in your main loop, then the event queue will be empty when you run your second event loop in readAlphaKey.
Laurent Gomila - SFML developer

 

anything