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

Author Topic: C++ problem with sf::Event::TextEntered  (Read 7172 times)

0 Members and 1 Guest are viewing this topic.

pauliuc

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • http://www.dustychest.co.nr
C++ problem with sf::Event::TextEntered
« on: September 30, 2010, 05:43:05 pm »
Hello all,

i am making a text box, and a small problem occured.

Code: [Select]
if( event.Type == sf::Event::KeyPressed )
{
       if( event.Key.Code == sf::Key::Back )
       {
            text.erase( text.size( ) - 1 );
       }
       else if( event.Key.Code == sf::Key::Return )
       {
            text += "\n";
       }
}
else if( event.Type == sf::Event::TextEntered )
{
      text += ( char )event.Text.Unicode;
}


The problem is that sf::Key::Back is ignored, when i type text. I figured out that its the problem with sf::Event::TextEntered because it takes backspace as text entered( ? ). How can i avoid that?

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
C++ problem with sf::Event::TextEntered
« Reply #1 on: September 30, 2010, 06:17:55 pm »
Since BackSpace is a character you can sort it out. It has the value 8 if I remember correctly.

Code: [Select]
if (event.Text.Unicode != 8)
    text += ( char )event.Text.Unicode;

pauliuc

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • http://www.dustychest.co.nr
C++ problem with sf::Event::TextEntered
« Reply #2 on: September 30, 2010, 07:07:47 pm »
thanks i will try that  :wink:

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
C++ problem with sf::Event::TextEntered
« Reply #3 on: September 30, 2010, 07:33:20 pm »
You may want to check for only valid input, like [a-z] and [0-9] ranges + some other ranges (because there are much more key which doesn't have a printable value like backspace).

Refer to an UTF charset list to know the ranges (or search this forum).
SFML / OS X developer

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
C++ problem with sf::Event::TextEntered
« Reply #4 on: October 01, 2010, 11:08:08 am »
If you look closely at the documentation, you can see this :
Code: [Select]
class Event
00205 {
00206 public :
00207
00212     struct KeyEvent
00213     {
00214         Key::Code Code;    
00215         bool      Alt;    
00216         bool      Control;
00217         bool      Shift;  
00218     };
00219
00224     struct TextEvent
00225     {
00226         Uint32 Unicode;
00227     };
Meaning that Event.Key.Code is to use only when a KeyEvent occurs, not a TextEvent !
Mindiell
----

pauliuc

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • http://www.dustychest.co.nr
C++ problem with sf::Event::TextEntered
« Reply #5 on: October 02, 2010, 03:31:26 pm »
It works with Lupinus suggested way  :)
Quote


Meaning that Event.Key.Code is to use only when a KeyEvent occurs, not a TextEvent !


im not using it when text event occurs... im using it when KeyPressed occurs.

 

anything