所以我在这里使用 SFML,我基本上想用输入的字母制作一个字符串。SFML 有一个内置的东西来检查是否在窗口内按下了键,它还有一个可以检测它是否是特定的东西,比如退格,所以我想把它们结合起来,这样你就可以输入和退格一个字符串(因为没有退格检测,所以如果你按下它就不会做任何事情)。
这是我的代码:
#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;
}
基本上,它创建一个窗口,然后检查它是否已关闭,然后检查是否按下了退格键,然后检查是否没有按下退格键,但按下了不同的键。
因此,这一切在我的 IDE(Visual Studio 2017 社区)上运行良好,但是,当我多次按退格键(第一次工作)时,它不会删除字符。
我的假设是这是由于事件未清除,但这没有意义,因为您仍然可以执行诸如按退格键后关闭窗口之类的操作。为什么if
function
即使多次按下它也只会触发一次退格?