C/ESC key Catching.
Expert: Prince M. Premnath - 12/11/2007
QuestionDear Prince M. Premnath,
My problem is that, i am scaning the integer values through loop into array. I want to exit from loop as soon as user press ESC key. How can i do it. Please help me.
Thanks in advance.
AnswerHi dear Rajju !
Here im presenting you a simple program what i have developed in order to illustrate the concept wit certain limitation !
The following program will meet your needs , make sure you shouldn't enter 0 as an input to the array since i have used 0 as a delimiter for this program !
#include<stdio.h>
#include<string.h>
#include<dos.h>
#include<stdlib.h>
union REGS i , o;
void getkey(char* key , char* scancode)
{
char a , b;
i.h.ah = 0;
int86(0x16 , &i , &o);
a = o.h.al;
b = o.h.ah;
*key = a;
*scancode = b;
}
int getnumber()
{
char arr[6];
int no;
int i;
char ch , code;
i = 0;
do{
getkey(&ch , &code);
if( ch == 27 )
return 0;
else
arr[i++] = ch;
printf("%c" ,ch);
}while( ch != 13);
printf("\n");
no = atoi(arr);
return no;
}
void main()
{
int ch , i = 0;
int arr[10] , no;
char a , code;
int count = 0;
clrscr();
for( i = 0 ; i < 10 ; i++)
{
no = getnumber();
if( no == 0)
break;
else
arr[i] = no;
++count;
}
for( i = 0 ; i < count ; i++)
printf("%d" , arr[i]);
}
Note : This program is just an idea , you are free to modify or you can develop on your own !
If you found any difficulties in understandng this program feel free to ask me a follow up
Thanks and Regards !
Prince M. Premnath