Reputation: 773
How would could I set a key that would access any function at anytime, like for example I have a simple code here and anytime I am working or in the program I could exit with the ESC key rather just optioning it after all those lines, making it like shortcut keys...
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main(){
int a,b,c,opt;
START:
clrscr();
printf("Enter Num A: ");
scanf("%d",&a);
printf("Enter Num B: ");
scanf("%d",&b);
printf("Enter Num C: ");
scanf("%d",&c);
printf("Do it Again? ESC to Exit");
opt=getch();
if(opt==27) exit(0);
else goto START;
}
EDIT: Is there any technique to command it like
while(inp!=27)
do{
...
}
So that the code would run and for example would exit if I pressed ESC key at anytime?
Upvotes: 0
Views: 120
Reputation: 70523
It depends on the platform you are working on. On windows (as an example) you would need to add a handler for the keyboard event message. Then when that message is sent to your window you would have code to perform the shutdown.
Many development platforms make this easier for you by hiding the messages behind an event model.
Prior to windows you needed to hook into the interrupt stack (using a command called "TSR" for terminate and stay resident) of the keyboard driver. Then when a keyboard event was called your code could run.
Upvotes: 2