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. For testing purpose, I am using cout to print what I enter. Here's the code snippet. event is an object of sf::Event, rect1 is the rectangle. in the if statement, i have specified the area where I click. 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.
...
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). 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.