簡體   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