简体   繁体   中英

C++ clearing cin buffer when using GetAsyncKeyState()

Hey guys i have a question which i have been looking for the answer to for quite a while, i have seen many questions alike but none solved my problem.

So i have a function that grabs the players name using get line that goes like this.

std::cout << "Enter your name"<< std::endl;
std::string input;

std::getline(std::cin, input);

and afterwards I have a function that has a menu which tells the user he can press a series of keys to move on to other parts of the menu.

while(true)
{
        //menu one
        if(GetAsyncKeyState(VK_CONTROL))
        {
            Menu_3();
        }
        //here is the problem
        if(GetAsyncKeyState(VK_SHIFT) && GetAsyncKeyState(0x53)) //0X53 = S
        {
            Menu_4();
        }
}

My problem is that if i use a name with a capital letter(created with SHIFT) and an 's' also Menu_4() gets called automatically after i enter the name, example name "Shawn". if i only have a name like this "shawn"(without using shift) then that "s" seems to get stored in the cin buffer and all that is required to move on is press "shift" to get into Menu_4();

Basically the input is getting carried over from the getline name request and its screwing up my GetAsyncKeyState() calls.

Thanks for your help.

I think you misunderstood the behavior of GetAsyncKeyState

see GetAsyncKeyState

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState

...

Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the pre-emptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application. The behavior of the least significant bit of the return value is retained strictly for compatibility with 16-bit Windows applications (which are non-preemptive) and should not be relied upon.

Probably you want to check the current state not if the key was pressed from last call.

if(GetAsyncKeyState(VK_CONTROL) xor 0x1)
....

You are just taking the return value of GetAsyncKeyState(), so effectively doing if(GetAsyncKeyState() != 0) . The docs say

If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState.

So you currently check if the key is pressed now or if the key was pressed since the last call to GetAsyncKeyState. If you only want to check if the key is pressed now, then you must check is the most significant bit of the return value is set.

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.

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