I'm just messing around with a program right now. I want to set up the program in a while loop so it will continually run, but I want it to stop when I press a certain key. How do I set up an interrupt to do this?
Printable View
I'm just messing around with a program right now. I want to set up the program in a while loop so it will continually run, but I want it to stop when I press a certain key. How do I set up an interrupt to do this?
State your OS and Compiler.
See the FAQ entry on reading key presses without waiting for a newline.
http://faq.cprogramming.com/cgi-bin/...&id=1043284392
In XP, you don'tQuote:
How do I set up an interrupt to do this?
In DOS, you hook the keyboard interrupt, but this would work just fine.
I don't know if it works in console b/c frankly I've never coded console programs. I went from DOS to DX to MFC.Code:while (!kbhit())
{
..run your loop
}
kbhit() works in my somewhat old version of Dev-C++; it's in <conio.h>.
[edit] If you want the loop to end only if a certain key is pressed (and not just any key), you might try something like this:
[/edit]Code:while(!kbhit() || getche() != ' ') {
}
I checked MSVC 2005 and kbhit() is in there. Originally I thought it was only in dos.h but I was wrong.
you could look into signals maybe in linux. They might be useful.
Signals for keyboard? Ok....