Max
Max

Reputation: 887

Sleep(x) causes computer to sleep instead

This is probably one of the strangest things I've encountered so far in C++:

while(counter != stop_value)
{
    //Part A starting
    ip.ki.wVk = VK_RETURN;
    ip.ki.dwFlags = 0;
    SendInput(1, &ip, sizeof(INPUT));
    ip.ki.wVk = VK_RETURN;
    ip.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &ip, sizeof(INPUT));
    //Part A ending

    if (s == 2) counter++;
    else counter--;
    cout << counter; //for debugging
    Sleep(i);
}

Whenever I comment the "Part A"-part away, it works properly; the program sleeps for i seconds, depending on user input. However, as soon as I add the easy way of triggering the return-key again, it makes my whole computer sleep instead (AKA, the screen goes entirely black for i seconds). I've tried googling, but I can't find anything about this.

And, this is only a small part of the code. I have added the INPUT class and everything further up. s is also a user-defined variable, that (in this case) can vary between 2 and 3. If equal to two, the counter's value will be set to 0, and it will increase until it has reached its max value (which is user-defined, and set to stop-value). While using this method, the sleep function works properly, and only the program "pauses" for i seconds. HOWEVER, as soon as a user set s to 3, it will start decreasing from the max value (user-defined) until it has reached its stop-value, which always is equal to zero. When this happens, the screen gets a blackout instead.

I've tried debugging for ages now, and I just can't seem to find the solution... Since the code is way more complex than this, and I'd need to use way more space (which I don't want to), I'll give you some inputs for the variables you can use instead:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646310(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx

Upvotes: 4

Views: 405

Answers (1)

Moo-Juice
Moo-Juice

Reputation: 38825

May as well stick it as an answer :)

Are you setting the type of ip to INPUT_KEYBOARD? and are you using ZeroMemory on the structure prior to use?

Upvotes: 1

Related Questions