Thread: Issue: GetAsyncKeyState with left mouse button returns true until first click

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    165

    Issue: GetAsyncKeyState with left mouse button returns true until first click

    Hi, I'm using this code to determine if the left mouse button is currently pressed:

    Code:
    bool leftMouseDown = GetAsyncKeyState(VK_LBUTTON) & 0x8000 ? true : false;
    ..but it returns true until the first time I click the left button even if the left button is not pressed. After that it works as expected. Why could this be?

    I'm guessing it has something to do with the fact that I left double-click the .exe file to start the program and that the mouse is because of that set in some unrefreshed state.

    I tried to simulate an extra mouse click at startup using:

    Code:
    PostMessage (d3d.window, WM_LBUTTONDOWN, 0, 0);
    PostMessage (d3d.window, WM_LBUTTONUP, 0, 0);
    ...but that made no difference.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    With the following program, if I use my usual setting of single-clicking to start programs I don't see the problem at all. If I change back to double-click mode (in Folder Options), I see it on the first iteration, but not on the following iterations (i.e., it prints 1 but not 2, 3, 4, 5) :
    Code:
    #include <stdio.h>
    #include <windows.h>
    int main() {
        int i;
        for (i = 1; i <= 5; i++) {
            if (GetAsyncKeyState(VK_LBUTTON) < 0)
                printf("%d\n", i);
            Sleep(1000);
        }
        printf("Done\n");
        getchar();
        return 0;
    }
    What does the above program do for you?

    I think the hack you're looking for is not to post messages to the window but something at a lower level. Maybe SendInput

    You also might want to add a little delay before the first GetAsyncKeyState. For example, adding Sleep(100) before the loop above gets rid of the 1 for me.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    165
    Quote Originally Posted by algorism View Post
    With the following program, if I use my usual setting of single-clicking to start programs I don't see the problem at all. If I change back to double-click mode (in Folder Options), I see it on the first iteration, but not on the following iterations (i.e., it prints 1 but not 2, 3, 4, 5) :
    Code:
    #include <stdio.h>
    #include <windows.h>
    int main() {
        int i;
        for (i = 1; i <= 5; i++) {
            if (GetAsyncKeyState(VK_LBUTTON) < 0)
                printf("%d\n", i);
            Sleep(1000);
        }
        printf("Done\n");
        getchar();
        return 0;
    }
    What does the above program do for you?

    I think the hack you're looking for is not to post messages to the window but something at a lower level. Maybe SendInput

    You also might want to add a little delay before the first GetAsyncKeyState. For example, adding Sleep(100) before the loop above gets rid of the 1 for me.
    Got the same behavior as you.

    SendInput solved the issue so I think I'll go with that. Thanks a lot!

    For reference, I now do this at startup after window creation:
    Code:
    INPUT input={0};
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    input.mi.dx = 0;
    input.mi.dy = 0;
    SendInput(1,&input,sizeof(INPUT));
    input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    SendInput(1,&input,sizeof(INPUT));
    ...and then disregard the first click in the input related code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with SDL, MOUSE CLICK ON BUTTON.. c++
    By Kova in forum C Programming
    Replies: 0
    Last Post: 02-21-2015, 06:17 PM
  2. Replies: 2
    Last Post: 08-11-2010, 08:00 AM
  3. Left and Right Click
    By EdBier in forum C++ Programming
    Replies: 1
    Last Post: 05-13-2009, 10:54 PM
  4. Simulate Left Mouse Button (VK_LBUTTON)
    By guitarist809 in forum Windows Programming
    Replies: 3
    Last Post: 04-19-2008, 06:05 PM
  5. Mouse event in Graphics Mode (can't catch Left Click)
    By quangnd in forum C++ Programming
    Replies: 5
    Last Post: 12-04-2007, 05:51 PM

Tags for this Thread