2015-08-14 64 views
0

我已經寫了一個控制檯應用程序,它通過按鍵的形式等待輸入。然而用戶可能想要切換應用程序,因爲'空格鍵'是我應用程序中的重要鍵,所以這會干擾遊戲。控制檯窗口不在焦點時忽略按鍵C++(Windows)

,我擡頭一看這個話題在計算器上,所有的線程之前無論是涉及非基於控制檯的應用程序,非WINDOWS.H圖書館等

目前,我的應用程序getInput()方法本質這個:

while (true) 
{ 
       if (GetAsyncKeyState(0x31)) 
       { 
        return 1; //User pressed 1 key 
       } 
       else if (GetAsyncKeyState(0x32)) 
       { 
        return 2; //User pressed 2 key 
       } 
       else if (GetAsyncKeyState(0x33)) 
       { 
        return 3; //User pressed 3 key 
       } 
       else if (GetAsyncKeyState(0x34)) 
       { 
        return 4; //User pressed 4 key 
       } 
       else if (GetAsyncKeyState(0x35)) 
       { 
        return 5; //User pressed 5 key 
       } 
       else if (GetAsyncKeyState(0x36)) 
       { 
        return 6; //User pressed 6 key 
       } 
} 

如何得到它,以便它將忽略當控制檯窗口不是主窗口時所做的輸入?

謝謝。

回答

2

您可以使用GetConsoleWindow來檢索控制檯輸出窗口的句柄。使用GetForegroundWindow來檢索用戶當前正在使用的窗口的句柄。您可以介紹以下布爾,使用這兩種API調用:

bool isConsoleWindowFocussed = (GetConsoleWindow() == GetForegroundWindow()); 

,並在你的條件與該GetAsyncKeyState連詞使用它。