繁体   English   中英

鼠标单击SFML后获取输入

[英]Get input after a mouse click SFML

我正在使用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;


        }

我假设您正在制作一个文本框。

按下该框时,应切换一个布尔值,以显示是否选中了该框。 然后,在另一个事件中,应检查是否已输入任何文本( 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
            }
        }
}

这应该让您捕获在选择文本框时键入的字符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM