我想做的是检查是否已输入文本,然后将该文本添加到字符串的末尾。我当前的代码:
#include <SFML/Graphics.hpp>
int main(){
sf::RenderWindow window(sf::VideoMode(800,600),"Window",
sf::Style::Titlebar | sf::Style::Close);
sf::Font arial;
arial.loadFromFile("arial.ttf");
sf::Text t;
t.setFillColor(sf::Color::White);
t.setFont(arial);
std::string s = "This is text that you type: ";
t.setString(s);
while(window.isOpen()){
sf::Event event;
while(window.pollEvent(event)){
if(event.type == sf::Event::Closed){
window.close();
}
if(event.type == sf::Event::TextEntered){
s.append(std::to_string(event.key.code));
}
}
t.setString(s);
window.clear(sf::Color::Black);
window.draw(t);
window.display();
}
}
这很有效,直到我键入键并且所显示的只是一系列数字(键的ASCII值)为止。我如何制作程序,以便将键的char值附加到
s
而不是ASCII值?
希望我能为您提供足够的信息以帮助我。任何答复表示赞赏:)
答案 0 :(得分:0)
Documentation是您的朋友:-)如果发生TextEntered
事件,event.text
“包含输入字符的Unicode值。您可以将其直接放在{ {1}},或在确保其位于ASCII范围(0-127)后将其强制转换为sf::String
。”
没有提及char
事件的event.key
成员,该成员旨在TextEntered
事件。由于联合的关键成员处于非活动状态并被其他数据覆盖,因此您看到的“ ASCII值”可能是垃圾值。另外请注意,std::to_string
仅将数字转换为它们的十进制表示形式。
以下一些代码适合您:
KeyPressed