0

I also tried to use Sleep because I saw in someone's code which was similar to mine but now it doesn't detect clicks I want it to detect first I tested using the space key in my keyboard I thought it was only for space but when I tested using a character and mouse button I had the same issue so now I'm here to get help here's my code:

void waitforspace()
{
    bool strtd = false, stop = true, set = false;
    std::chrono::steady_clock::time_point nw, thn;
    std::thread t1;
    while (true)
    {
        if ((GetKeyState(VK_RBUTTON) & 0x8000))
        {
            // I tried this to fix the repeating issue it worked but now most of the time my clicks doesn't get detected
            if (!(GetKeyState(VK_RBUTTON) & 0x8000))
            {
                if (set)
                {
                    nw = std::chrono::high_resolution_clock::now();
                    std::chrono::duration<double, std::milli> time_span = nw - thn;
                    set = false;
                    if (time_span.count() < 350.0)
                    {
                        std::cout << "Double click Detected!\n";
                        if (!strtd)
                        {
                            // Function I want to run if user clicked twice
                            t1 = std::thread(sendclicks, &stop);
                            strtd = true;
                        }
                        else
                        {
                            stop = false;
                            strtd = false;
                        }
                    }
                }
                else
                {
                    std::cout << "One click Detected!\n";
                    thn = std::chrono::high_resolution_clock::now();
                    set = true;
                }
            }
        }
    }
}
Gerhardh
  • 11,688
  • 4
  • 17
  • 39
  • 1
    GetKeyState is supposed to be used (mailnly) in response to WM_KEYDOWN event. Is it your case? – Giuseppe Guerrini Nov 15 '21 at 09:39
  • 1
    You need `GetAsyncKeyState` in this case, not `GetKeyState`. But, really, you should not be writing code like this at all. You are polling for keyboard input, which is not how window managers are designed to work. Rather, they are *event-driven*, which means that you'll receive a notification message whenever a keyboard event occurred. – Cody Gray - on strike Nov 15 '21 at 09:41
  • @CodyGray that will entail writing a Windows app instead of a console app – user253751 Nov 15 '21 at 09:45
  • @CodyGray I changed the second if to GetAsyncKeyState and now it's working but can you explain to me why? also I know I should make an event for it but I want it to be a simple console application(I'm new to C++ and win32 doesn't look simple to me yet but I'm trying to get better at it) –  Nov 15 '21 at 10:10
  • To handle mouse **events** in a console app, see https://stackoverflow.com/a/6285501/15416. Polling is indeed not nice. – MSalters Nov 15 '21 at 10:11
  • 1
    GetKeyState() returns the *buffered* state of the user input. It is only appropriate in an app that uses the standard message loop (GetMessage + DispatchMessage), the buffered state gets updated after a keyboard or mouse message is handled by that loop. Buffering ensures that the keyboard state can be accurately tested to detect, say, Shift+Click. Necessary because the message might be processed long after the input event, the Shift key might well have been released by then. A console app doesn't have a message loop so you can't reliably use GetKeyState(). Favor ReadConsolelnput(). – Hans Passant Nov 15 '21 at 12:21

0 Answers0