|
I think I've got the solution...
Going back to a previous post (by gwross), your best method will be to use kbdhit(). It will return 0 if no key has been pressed, and 1 if a key has been pressed. Since I assume you only want it to quit when a certain key is pressed, you can then issue a getch() which will return the key that was pressed. Just compare it, and you should be good to go. Just make sure to include <conio.h>.
Here's the code I tested it with:
while(true){
if(kbdhit()){
cout<<"key pressed:";
myChar=getch();
cout<<myChar<<"\n";
if(myChar=='q')
return 0;
}
else{
cout<<"no key pressed\n";
}
cout.sync_with_stdio()
}
BTW, that cout.sync_with_stdio() is because I was having problems with the output being very sluggish.
|