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

Author Topic: [Solved] getting input and put them into a string  (Read 8513 times)

0 Members and 1 Guest are viewing this topic.

seal

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved] getting input and put them into a string
« on: February 06, 2008, 01:20:59 am »
Hello.

I want to make a textfield where I type text like any other textfield. So when a keyPress event is received I could just put the unicode character for the key in a string?
Well, I though so but that dosen't work. When I press a key that aren't a character it bugs.
So.. then I found some code from a SDL tutorial and he checked if it was a valid character, and that's what Im using.

Here's the code:
Code: [Select]

in my event loop...
                if(event.Key.Code == sf::Key::Back)     //om man tryckt på backsteg..
                {
                    if(inputField.length() > 0)
                        inputField.erase( inputField.length() - 1 );
                }
                else
                {
                    //en ifsats för att kolla att det är en bokstav eller siffra..
                    // sorry, swedish comments :P
                    if(
                    (event.Text.Unicode >= (char)'0' && event.Text.Unicode <= (char)'9')
                    ||
                    (event.Text.Unicode >= (char)'a' && event.Text.Unicode <= (char)'z')
                    ||
                    (event.Text.Unicode >= (char)'A' && event.Text.Unicode <= (char)'Z')
                    ||
                    event.Text.Unicode == (char)' '
                    )
                    {
                        inputField += (char)event.Text.Unicode;
                    }
                }
                if(event.Key.Code == sf::Key::Space)
                    inputField += (char)' ';


...
draw the text to the window.




Works really good, except when it comes to the rest of the characters I want to use, I want the user to be able to write any character..
And holding SHIFT and typing a character won't put any character in the string, why?

How would you do this?

PS, I've tried to use wxwidgets all day, but I just can't get it to work. And I just want to try to make a simple chat so I don't think I need a GUI yet.

Thanks!

Edit;
I just saw this topic:
http://sfml.sourceforge.net/forum/viewtopic.php?t=148

But it dosen't solve my problem as I want it to be able to write all characters (i.e. !"#¤%& etc), including the non-internationel ones (like å ä ö). And it dosen't work with upper case characters.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] getting input and put them into a string
« Reply #1 on: February 06, 2008, 01:53:59 am »
The Event.Text field is only valid when a Event::TextEntered has been received, which is the event you must catch (forget Event::KeyPressed for this kind of input).
Laurent Gomila - SFML developer

seal

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved] getting input and put them into a string
« Reply #2 on: February 06, 2008, 02:55:54 am »
Quote from: "Laurent"
The Event.Text field is only valid when a Event::TextEntered has been received, which is the event you must catch (forget Event::KeyPressed for this kind of input).


Works perfect, apart from when I press backspace or return. If I do that, I can't type. Also å ä ö dosen't work.

Thanks!

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
[Solved] getting input and put them into a string
« Reply #3 on: February 06, 2008, 10:11:47 pm »
I would also know how to do this, I wouldn't mind a tutorial for dummies ;)

theanzelm

  • Newbie
  • *
  • Posts: 7
    • View Profile
[Solved] getting input and put them into a string
« Reply #4 on: June 14, 2008, 05:32:52 pm »
I think non-international characters are not working because you cast to char instead of wchar_t or something similar.

Code: [Select]
inputField += (char)event.Text.Unicode

Also you should check if event.Text.Unicode returns a printable character:

Code: [Select]
if(event.Text.Unicode < 0x80){
    ...do something with char...
}


The backspace/return issue should be solved then.
(Check for them manually and remove the last character or add a "\n" to your string if you want the backspace/newline functionality)

I would be interested in the casting part too, what do I have to do with the Uint16 event.Text.Unicode gives me, to be able to append it to a string?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[Solved] getting input and put them into a string
« Reply #5 on: June 15, 2008, 06:19:21 am »
Just use a wstring, and cast the character to wchar_t.
Laurent Gomila - SFML developer

 

anything