我正在使用SFML创建一个表单,并且我被困在一个地方,在我点击一个矩形然后我得到输入。出于测试目的,我使用cout来打印我输入的内容。这是代码片段。 event是sf :: Event的对象,rect1是矩形。在if语句中,我指定了单击的区域。 现在我想在点击矩形后打印出我键入的内容。请帮忙,因为我已经超过6小时了。
...
switch (event.type){
case Event::Closed:
window.close();
break;
case Event::MouseMoved:
//cout << event.mouseMove.x << ", " << event.mouseMove.y << endl;
break;
case Event::MouseButtonReleased:
if (event.key.code==Mouse::Left
&& Mouse::getPosition(window).x >= rect1.getPosition().x
&& Mouse::getPosition(window).x <= rect1.getPosition().x + rect1.getSize().x
&& Mouse::getPosition(window).y >= rect1.getPosition().y
&& Mouse::getPosition(window).y <= rect1.getPosition().y + rect1.getSize().y)
{
//what I want to do is here I guess.
}
break;
}
答案 0 :(得分:1)
我假设你正在制作一个文本框。
按下该框时,您应切换一个布尔值,显示该框是否被选中。然后,在另一个事件中,您应该检查是否输入了任何文本(TextEntered事件)。如果有,则应检查是否选中了文本框,如果是,则插入字符。
以下是一个例子:
switch (event.type){
case Event::Closed:
window.close();
break;
case Event::MouseMoved:
//cout << event.mouseMove.x << ", " << event.mouseMove.y << endl;
break;
case Event::MouseButtonReleased:
if (event.key.code==Mouse::Left
&& Mouse::getPosition(window).x >= rect1.getPosition().x
&& Mouse::getPosition(window).x <= rect1.getPosition().x + rect1.getSize().x
&& Mouse::getPosition(window).y >= rect1.getPosition().y
&& Mouse::getPosition(window).y <= rect1.getPosition().y + rect1.getSize().y)
{
// The box has been selected
// Toggle the boolean
isSelected = !isSelected;
}
break;
case Event::TextEntered:
if ( isSelected )
{
if ( event.Text.Unicode < 0x80 ) // it's printable
{
// Here is the character that was typed
char keyString = (char) event.Text.Unicode;
// Here you should add the character to perhaps a string containing the total text in the text box
}
}
}
这可以让您捕获选中文本框时输入的字符。