Hi guys, the thing is, i want my program to exit when the user presses the escape button. I've tried using the getkeystate() function but the prog just skips it.
I've made this simple program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
#include <windows.h>
#include <winuser.h>
usingnamespace std;
main()
{
cout<<"Welcome to C++! Press the escape key to exit.\n";
if(GetKeyState(VK_F1)==1)
{
cout<<"Goodbye!\n";
system("pause");
exit(EXIT_SUCCESS);
}
}
but the program just exits (without printing "goobye!") when i press the excape key or any toher key. does anyone know why?
thanks for the advice but i was wondering how to print the word "goodbye" when the user presses the escape key. i.e the user won't see the word "goodbye" until he presses the escape key. but still thanks, it was really helpful.
int main()
{
cout<<"Welcome to C++! Press the escape key to exit.\n";
while (true) {
if (GetAsyncKeyState(VK_ESCAPE))
{
cout<<"Goodbye!\n";
Sleep(1000);
exit(EXIT_SUCCESS);
}
}
return 0;
}
The while (true) allows the key detection conditional to run infinity until something is done to exit it - nullifying the need for system("PAUSE").
I also threw in a Sleep for 1000 milliseconds to give the user time to read "Goodbye!".