[英]Get input after a mouse click SFML
I am making a form using SFML and I'm stuck in a place where after I click on a rectangle and THEN I get input. 我正在使用SFML制作表单,并且被卡在一个地方,在此之后,单击矩形,然后获得输入。 For testing purpose, I am using cout to print what I enter.
出于测试目的,我正在使用cout打印输入的内容。 Here's the code snippet.
这是代码片段。 event is an object of sf::Event, rect1 is the rectangle.
event是sf :: Event的对象,rect1是矩形。 in the if statement, i have specified the area where I click.
在if语句中,我指定了单击的区域。 Now I want to print out what I type after I click on the rectangle.
现在,我想在单击矩形后打印出我键入的内容。 Please Help on this because I haven't cracked it for over 6 hrs.
请帮忙解决这个问题,因为我已经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;
}
I assume you're making a text box. 我假设您正在制作一个文本框。
When the box is pressed you should toggle a boolean which shows whether the box is selected or not. 按下该框时,应切换一个布尔值,以显示是否选中了该框。 Then, in another event, you should check if any text has been entered ( TextEntered event).
然后,在另一个事件中,应检查是否已输入任何文本( TextEntered事件)。 If they have, you should check if the text box is selected, and if it is, insert the characters.
如果有,则应检查是否选中了文本框,如果已选中,则插入字符。
Here is an example: 这是一个例子:
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
}
}
}
This should let you capture the characters that are typed when the text box is selected. 这应该让您捕获在选择文本框时键入的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.