Reputation: 1
I have the following code:
for (int i = 0; i < 1;) {
if (GetKeyState(VK_SHIFT) & 0x8000)
{
cout << "op";
}
}
This prints out op
if the Shift key is held down, actually it spams the output if you hold down the key.
This is what I want instead:
The program prints out something ONCE when you press/hold down a key instead of spamming it, and the only way to print it again is to let go of the key and press it again. No matter how long you hold down the key, the program will only execute the following code ONCE, unless you press it again.
How do I make my code do this?
Upvotes: 0
Views: 6599
Reputation: 31447
You could simply set a bool
to true
when you've detected a "key down" event and then only set it back to false
once you detect a "key up" event. That way you can know if the key was already down and only report that it was pressed if it was not already in that state.
Upvotes: 3