GetAsyncKeyState: Wrong Returns??

So, I recently emplemented some code that used some other buttons on the keyboard, I used GetAsyncKeyState() to do it.

The function I used goes like this:

1
2
3
4
5
6
7
8
9
10
11
12
bool enter_key()
{
    bool p = false;
    while(GetAsyncKeyState(VK_ENTER))
    {
        if(!p)
        {
            p = true;
        }
    }
    return p;
}


Quite simple really. It performs the while loop while the key is pressed, so the user has to let go for it to do somthing, which garuntees a single key press.

The problem is, for some reason, I will press the enter key, or somthing, and then if I press any other key it returns true! I can not figure this out for the life of me, and I'm starting to question whether it may be a problem with Windows 8, or my library/compiler... This has never happened before.
If you want something to happen after the enter key is pressed, intercept WM_KEYUP and check for VK_RETURN?
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646281%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx

Anyway, p is set to true upon entering this function as long as the enter key is down, and isn't un-set, flipped, or set to false anywhere.

So... yes, if you were to press other keys it wouldn't really care because as soon as it detects the enter key being pressed it will set it to true.
I think you missed the part where p is initialized as false.

So, no, coming into the function it would not return true no matter what.
Last edited on
Topic archived. No new replies allowed.