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

Author Topic: KeyPressed won't repeat  (Read 3871 times)

0 Members and 1 Guest are viewing this topic.

yoni0505

  • Newbie
  • *
  • Posts: 9
    • View Profile
KeyPressed won't repeat
« on: December 25, 2010, 09:32:42 pm »
I'm making a text input field and I want to delete the last char in a string when the user press BACKSPACE.

For some reason it isn't working right:
When I press BACKSPACE it will only delete one char then it will not delete anything untill I enter a new char to the string.

Im using this function to delete the last char:

Code: [Select]
void CheckKeyPressed(CField* Field, sf::Event::KeyEvent* Key)
{
if(Key->Code == sf::Key::Back)
{
Field->str = Field->str.substr(0, Field->str.length() - 1);
}
}


I know that the EnableKeyRepeat is true by default.

Why it doesn't work right?  :?
"There is no knowledge that is not power."

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
KeyPressed won't repeat
« Reply #1 on: December 25, 2010, 09:39:14 pm »
Why dont you test it further? I think that most of the people should debug deeper and smarter for a time before posting a question..

Check if other keys are repeating, for example. Check if you function is called as it should, etc.

yoni0505

  • Newbie
  • *
  • Posts: 9
    • View Profile
KeyPressed won't repeat
« Reply #2 on: December 25, 2010, 10:06:13 pm »
Quote from: "panithadrum"
Why dont you test it further? I think that most of the people should debug deeper and smarter for a time before posting a question..

Check if other keys are repeating, for example. Check if you function is called as it should, etc.

Of course I tested it and debugged it as best as I could, googled for answer but didn't find any.
I can't find a reason why this thing happening, after all it works after I enter a new char to the string.

I wouldn't ask in the forums unless I reach a dead end cause it will probably will be much faster to debug then waiting for an answer that may never come.

EDIT:
I found out that the size of the string won't decrease when I delete(?) the last char.
I guess it means that it always will delete the last char in the string that will be null after it was deleted once..?

EDIT:
if I delete 2chars, at the first time it will once delete 2 chars, then it will delete 1 char.
"There is no knowledge that is not power."

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
KeyPressed won't repeat
« Reply #3 on: December 26, 2010, 02:11:03 am »
Maybe this should be:
Code: [Select]
Field->str.substr(0, Field->str.length() - 2);
?

Let's get first that function working well!

yoni0505

  • Newbie
  • *
  • Posts: 9
    • View Profile
KeyPressed won't repeat
« Reply #4 on: December 26, 2010, 07:09:40 am »
Quote from: "panithadrum"
Maybe this should be:
Code: [Select]
Field->str.substr(0, Field->str.length() - 2);
?

Let's get first that function working well!

Ok, it's working, but on the first delete after each time new char was entered, as I said, it would delete the first two chars.
How do I fix the first delete? There must be a reason for this, and if we can find it we would be able to find reasonable solution.

EDIT:
Could it be a null character problem?
When I add sf::Text::Unicode to the string, does it include null character in the end of the string?
And does the null character counted by the str.length() ?
Perhaps this is the source of the problem?
Is there a way to make sure the string is NULL terminated?
"There is no knowledge that is not power."

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
KeyPressed won't repeat
« Reply #5 on: December 26, 2010, 12:39:23 pm »
Backspace is a valid unicode character, so it gets added to your string if you don't filter it out. So I guess that's what happened, and you always ended up adding/deleting the backspace char.
Laurent Gomila - SFML developer

yoni0505

  • Newbie
  • *
  • Posts: 9
    • View Profile
KeyPressed won't repeat
« Reply #6 on: December 26, 2010, 05:42:04 pm »
Quote from: "Laurent"
Backspace is a valid unicode character, so it gets added to your string if you don't filter it out. So I guess that's what happened, and you always ended up adding/deleting the backspace char.

This is heavy man... (  :lol: )

I checked if "Enter" (Return(?)) is also unicode char, I found a list on wikipedia and found "Carriage return", is it like the key.code.return?

if so I can use else if for the Text event  :)

EDIT:
Thank you guys, problem fixed.
"There is no knowledge that is not power."

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
KeyPressed won't repeat
« Reply #7 on: December 26, 2010, 05:58:41 pm »
Quote
I checked if "Enter" (Return(?)) is also unicode char, I found a list on wikipedia and found "Carriage return", is it like the key.code.return?

Probably. You should test to be sure.
Laurent Gomila - SFML developer

 

anything