Get key press for only when got pressed and get it again when it is released and pressed again

  c++, io, keyboard, windows

I’m trying to get key input for only when it is pressed like not for if we keep it pressed.

I want it to stop the input until it is "released" and "pressed" again, but I am unable to do it.

my code:

while(isRunning)
{
    if(GetKeyState('K') & 0x8000) 
    {
        std::cout << "K is Pressed" << std::endl;
    }
}

output when pressed:

K is Pressed
K is Pressed
K is Pressed
K is Pressed
K is Pressed
K is Pressed
K is Pressed
K is Pressed
K is Pressed
K is Pressed

but what I want my out to be is a

K is Pressed

and I want it to print it again when I "Release" it and "Press" it again.

Source: Windows Questions

LEAVE A COMMENT