2016-02-12 49 views
-3

儘管關於此主題的相同問題已經被問到噸的時間,我找不到任何工作解決方案。C++ GetAsyncKeyState關鍵狀態問題

我的簡單程序有兩個循環。當我輸入憑證並按下回車鍵。該程序必須進入第一個循環。這樣我就可以打印菜單並等待用戶輸入菜單選擇。基於菜單選擇,另一個循環必須激活並等待用戶輸入。

但這裏的問題雖然我輸入憑據,然後按回車,它打印菜單,而不用等待用戶輸入它進入第一個菜單並顯示項目內的第一個菜單。由於WinAPI的鍵狀態問題發生此問題

if (GetAsyncKeyState(VR_RETURN) != 0)將在兩種情況下激活。如果按鍵。或按鍵之前已被按下。

如何克服這個問題?

以下是我的客人登錄功能

void guestLogin() 
{ 
    setColor(BASECOLOR); 
    clear(); 

    string screen_lable = "GUEST LOGIN"; 

    gotoxy(getCenterCoordinatesForText(screen_lable), 2); 

    cout << screen_lable << endl; 

    gotoxy(getCenterCoordinatesForText(screen_lable), 3); 

    for (int i = 0; i < screen_lable.length(); i++) 
    { 
     cout << "="; 
    } 

    screen_lable = "PLEASE ENTER YOUR NAME: "; 

    gotoxy(getCenterCoordinatesForText(screen_lable), 5); 

    gotoxy(getCenterCoordinatesForText(screen_lable), 7); 

    getline(cin, username); 

    //When user presses enter this will take username and proceed to show menus for the user and it should not go ahead and and select 1st menu and show the content. 
    if (username.length() != 0) 
    { 
     OptionsList(); 
    } 
} 

//菜單爲

void OptionsList() 
{ 
    menuIndex = 0; 

    while (true) 
    { 
     setColor(BASECOLOR); 
     clear(); 

     string appName = "BEST FOOD INFORMATION SYSTEM"; 
     string menuBtn = "MENUS"; 
     string catering = "CATERING"; 
     string services = "SERVICES"; 
     string helpBtn = "HELP"; 
     string logoutBtn = "LOGOUT"; 

     gotoxy(getCenterCoordinatesForText(appName), 2); 
     cout << appName << endl; 

     /*if (UserRole::Customer) 
     { 
      gotoxy(getCenterCoordinatesForText(menuBtn + " || " + catering + " || " + helpBtn + " || " + logoutBtn), 5); 
     }*/ 
     //else if (UserRole::Administrator){ 
      gotoxy(getCenterCoordinatesForText(menuBtn + " || " + catering +" || " + services + " || " + helpBtn + " || " + logoutBtn), 5); 
     //} 

     //Updating Screen Text State Based On Key Selection 
     if (menuIndex == 0) 
     { 
      setColor(INVERTED_BASECOLOR); 
      cout << menuBtn; 
      setColor(BASECOLOR); 
      cout << " || "; 
      cout << catering; 
      cout << " || "; 
      cout << services; 
      cout << " || "; 
      cout << helpBtn; 
      cout << " || "; 
      cout<< logoutBtn; 
     } 
     else if (menuIndex == 1) 
     { 
      setColor(BASECOLOR); 
      cout << menuBtn; 
      cout << " || "; 
      setColor(INVERTED_BASECOLOR); 
      cout << catering; 
      setColor(BASECOLOR); 
      cout << " || "; 
      cout << services; 
      cout << " || "; 
      cout << helpBtn; 
      cout << " || "; 
      cout << logoutBtn; 
     } 
     else if (menuIndex == 2) 
     { 
      setColor(BASECOLOR); 
      cout << menuBtn; 
      cout << " || "; 
      cout << catering; 
      cout << " || "; 
      setColor(INVERTED_BASECOLOR); 
      cout << services; 
      setColor(BASECOLOR); 
      cout << " || "; 
      cout << helpBtn; 
      cout << " || "; 
      cout << logoutBtn; 
     } 
     else if (menuIndex == 3) 
     { 
      setColor(BASECOLOR); 
      cout << menuBtn; 
      cout << " || "; 
      cout << catering; 
      cout << " || "; 
      cout << services; 
      cout << " || "; 
      setColor(INVERTED_BASECOLOR); 
      cout << helpBtn; 
      setColor(BASECOLOR); 
      cout << " || "; 
      cout << logoutBtn; 
     } 
     else if (menuIndex == 4) 
     { 
      setColor(BASECOLOR); 
      cout << menuBtn; 
      cout << " || "; 
      cout << catering; 
      cout << " || "; 
      cout << services; 
      cout << " || "; 
      cout << helpBtn; 
      cout << " || "; 
      setColor(INVERTED_BASECOLOR); 
      cout << logoutBtn; 
     } 

     //Getting Key Events 
     while (true) 
     { 

      if (GetAsyncKeyState(VK_RIGHT) != 0) 
      { 
       menuIndex += 1; 
       if (menuIndex == 5) 
       { 
        menuIndex = 4; 
       } 
       break; 
      } 
      else if (GetAsyncKeyState(VK_LEFT) != 0) 
      { 

       menuIndex -= 1; 
       if (menuIndex == -1) 
       { 
        menuIndex = 0; 
       } 
       break; 
      } 
      // Here is the issue occours 
      else if (GetAsyncKeyState(VK_RETURN) != 0) 
      { 

       switch (menuIndex) 
       { 
        case 0: 
        { 
         //Previously pressed ENTER Key Stroke evokes this function as the GetAsyncKeyState() returns the pressed state. 
         FoodsNBeverages(); 
        } break; 
        case 1: 
        { 
         Catering(); 
        } break; 
        case 2: 
        { 
         Services(); 
        } break; 
        case 3: 
        { 
         Help(); 
        } break; 
        case 4: 
        { 
         Logoff(); 
        } break; 
       } 
      } 
     } 

     Sleep(200); 

    } 
} 

許多變化已經試過(GetAsyncKeyState(VK_MENU)&0x8000)但他們沒有正在使用的用戶。

+1

取代

GetAsyncKeyState(VK_RETURN) != 0 

請提供[MCVE](http://stackoverflow.com/help/mcve)。包含無關代碼的問題對未來的訪問者來說既沒有用處,也不可能得到答案。 – IInspectable

回答

-2

你必須不斷地使用GetAsyncKeyState()函數來檢查鍵是否被按下。然後,即使按鍵之前未按下鍵,該功能將返回0。

+0

[文檔](https://msdn.microsoft.com/en-us/library/windows/desktop/ms646293.aspx)似乎不同意你的看法。 – IInspectable

1

要測試密鑰是否已關閉,您需要測試返回值是否設置了MSB。這在documentation中有解釋。

如果設置了最高有效位,則關鍵是關閉。

因此,與

GetAsyncKeyState(VK_RETURN) < 0