samedi 24 mars 2018

SFML Backspace Isn't Triggering More Than Once

so I'm working with SFML here, and I basically want to make a string out of entered letters. SFML has a built in thing to check if keys are pressed inside of a window, and it also has one to detect if it is something specific like a backspace, so I wanted to combine these to make it so you could type and backspace a string (since without the backspace detection, it doesn't do anything if you press it).

Here's my code:

#include <iostream>
#include "SFML/Window.hpp"
#include <vector>

using namespace std;
using namespace sf;

int main() {
    // Initializes the class and creates the window
    Window window;
    window.create(VideoMode(800, 600), "Test window", Style::Close | Style::Titlebar | Style::Resize);
    // Run while the window is open
    vector <char> sentence;
    while (window.isOpen()) {
        Event event;
        // Check if an event is triggered
        while (window.pollEvent(event)) {
            // If the event is to close it, close it
            if (event.type == Event::Closed) {
                window.close();
            }
            // If the backspace key is pressed
            else if (event.type == Event::KeyPressed) {
                if (event.key.code == Keyboard::BackSpace) {
                    sentence.pop_back();
                }
            }
            // If text is entered in the window
            else if (event.type == Event::TextEntered) {
                sentence.push_back(static_cast <char> (event.text.unicode));
                cout << "Sentence = ";
                for (int i = 0; i < sentence.size(); i++) {
                    cout << sentence[i];
                }
                cout << endl;
            }
        }
    }
    return 0;
}

Basically, it creates a window, then it checks if it is closed, and then it checks if a backspace was pressed, and then it checks if a backspace wasn't pressed, but a different key was.

And so this all works great on my IDE (Visual Studio 2017 Community), however, when I press backspace more than once (it works the first time), it doesn't remove the character.

My assumption is that this is due to the event not clearing, but this wouldn't make sense since you can still do things like close the window after pressing backspace and such. Why does it only trigger the backspace if function once even though you press it multiple times?

Aucun commentaire:

Enregistrer un commentaire