Read ESC key at runtime

Asked

Viewed 91 times

0

I’m writing a program that reads an indefinite number of keyboard values and stops only when the user presses Esc.

I tried to solve this using the function GetAsyncKeyState() as follows.

#include <Windows.h>
if (GetAsyncKeyState(VK_ESCAPE)){
        for(int i=0;i<=n;i++){
                cout<<v[i]<<endl;
        }
        return 0;
}

But even after squeezing Esc the program only goes to the for after I enter some other number. How does the program jump to the for right after I squeeze Esc?

  • 1

    We’re missing a } there.

  • I believe your program is stopping at another loop that only comes out when you press a number. The call GetAsyncKeyState(VK_ESCAPE) at all times will return non-zero if ESC is pressed. I suggest isolating the failed code or posting your entire code here in the OS.

1 answer

1

The Getasynckeystate function returns 0 if the Esc key is not pressed and returns -32767{I think that’s this value) when the Esc key is pressed ' so I think if you put it like this it should work and I also think that you should put that inside a loop' type o while so that it is monitoring when the key is pressed or not.

 if (GetAsyncKeyState(VK_ESCAPE) == -32768)
   {
       for(int i=0;i<=n;i++)
       {
           cout<<v[i]<<endl;
       }
           return 0;
   }

That’s why when you press any other key it enters the loop for, because the Getasynckeystate function is always giving as 0, so you have to tell it the ' can even suddenly put != 0. I think it works too

Browser other questions tagged

You are not signed in. Login or sign up in order to post.