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

Author Topic: Having editable text on screen, and pressing the "i" key breaks it  (Read 6414 times)

0 Members and 1 Guest are viewing this topic.

DraGun

  • Newbie
  • *
  • Posts: 21
    • View Profile
So I'm trying to design this form type of thing where you enter data in fields, and I've gotten the user input text figured out, but when I type in the '"i" key it starts a cascading delete that breaks the code when the string is empty. Here's the function I'm using:

(click to show/hide)
« Last Edit: May 22, 2015, 05:58:07 pm by DraGun »

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #1 on: May 22, 2015, 06:20:02 pm »
Use > Keyboard::[key what you need] instead '\b'

DraGun

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #2 on: May 22, 2015, 06:21:59 pm »
I've tried that, but it doesn't delete it just adds a weird square character.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #3 on: May 22, 2015, 06:49:29 pm »
When the event type is sf::Event::TextEntered, you should be using event.text.unicode, not event.key.code.
See the tutorial.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DraGun

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #4 on: May 22, 2015, 07:03:26 pm »
Well, I implemented what you said, and it does delete, but it also starts that cascading delete, meaning it continually deletes until the string is empty and then it crashes. And that's immediately after it puts that "not a character" box/square.

Redee

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #5 on: May 22, 2015, 07:04:14 pm »
If your text only english its no problem because every symbol = 1byte, and all operations you can do directly with this bytes.
F.e.
string s = "1234";
// here if You pressed 2 times backspace
s[3] = 0;
s[2] = 0;
s.substr(0, 2);
s.resize(s.size() - 2);
cout << s << endl;

// add check keypressed (text entered) event logic
const char* txtToAdd = "777";
s.append(txtToAdd);
cout << s << endl;

To prevent multiple delete you must add variable to check previous delete and Clock time elapsed, to block next crop from backspace or delete key.

Clock starts when detect first delete and check alltime Clock while Keyboard::isKeyPressed(Keyboard::Backspace/Delete).
Time accumulates every frame.
Restart time when difference is >= what you need and in this moment do delete.
« Last Edit: May 22, 2015, 07:21:20 pm by Redee »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #6 on: May 22, 2015, 07:18:40 pm »
Did you also change the event.key.code to event.text.unicode on the erase condition?
Also, you're not checking the event type before the second if statement.
« Last Edit: May 22, 2015, 07:20:40 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DraGun

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #7 on: May 22, 2015, 08:12:00 pm »
Quote
To prevent multiple delete you must add variable to check previous delete and Clock time elapsed, to block next crop from backspace or delete key.

Clock starts when detect first delete and check alltime Clock while Keyboard::isKeyPressed(Keyboard::Backspace/Delete).
Time accumulates every frame.
Restart time when difference is >= what you need and in this moment do delete.

You mention a clock, but I don't see it anywhere in your code. And I'm not sure what your code is even supposed to do.

Quote
Did you also change the event.key.code to event.text.unicode on the erase condition?
Also, you're not checking the event type before the second if statement.

I did change it on the erase side. I put in the check on the erase side, and that stopped the cascading delete, even with the "i" key, but it has now stopped deleting :/

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #8 on: May 22, 2015, 08:26:54 pm »
I put in the check on the erase side, and that stopped the cascading delete, even with the "i" key, but it has now stopped deleting :/
Can you show your latest code?
You should use the tags [code=cpp] and [/code] around the code to use c++ syntax highlighting.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

DraGun

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #9 on: May 22, 2015, 08:47:15 pm »
Sure thing. Oh ok.

void type(sf::Clock & clock, sf::String & string, sf::Event & event)
{

        if (event.type == sf::Event::TextEntered && clock.getElapsedTime() >= sf::milliseconds(130) && event.text.unicode != sf::Keyboard::BackSpace)
        {
                clock.restart();
                string.insert(string.getSize(), event.text.unicode);
        }

        if (event.type == sf::Event::KeyPressed && event.text.unicode == sf::Keyboard::BackSpace && clock.getElapsedTime() >= sf::milliseconds(130))
        {
                clock.restart();
                string.erase(string.getSize() - 1);
        }

}

EDIT: Oh, I'm using Visual Studio 13, in case that matters at all.

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #10 on: May 22, 2015, 08:53:21 pm »
You're using sf::Event::KeyPressed with event.text.unicode. As far as I know, it's always event.text.unicode with sf::Event::TextEntered and event.key.code with sf::Event::KeyPressed.

DraGun

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #11 on: May 22, 2015, 09:03:40 pm »
Yeah, I already changed that (see last comment with code)

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #12 on: May 22, 2015, 09:04:49 pm »
I did, in the second if, you're using KeyPressed and unicode.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #13 on: May 22, 2015, 09:06:00 pm »
Not important, but the lack of "switch" (or at least "else if") for 'event.type' bothers me ;)

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Having editable text on screen, and pressing the "i" key breaks it
« Reply #14 on: May 22, 2015, 09:08:36 pm »
As Shadowmouse pointed out, you're using sf::Event::Keypressed for the erase condition. I would expect it to be the sf::Event::TextEntered condition - the same as the other test.

Just to be clear, the unicode for backspace is not sf::Keyboard::Backspace; it is indeed '\b'.

Can you not nest the conditions so that they are a bit clearer, like this:
void type(sf::Clock & clock, sf::String & string, sf::Event & event)
{

        if (event.type == sf::Event::TextEntered && clock.getElapsedTime() >= sf::milliseconds(130))
        {
                clock.restart();
                if (event.text.unicode == '\b')
                        string.erase(string.getSize() - 1);
                else
                        string.insert(string.getSize(), event.text.unicode);
        }

}

p.s. Jesper, there's only one event type here so there's nothing to switch  ;)
« Last Edit: May 22, 2015, 09:11:05 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything