You are here:

C/ESC key Catching.

Advertisement


Question
Dear 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.

Answer
Hi 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

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise


I'm sure that I can solve any doubts in Turbo C ,Graphics Programing ,Mouse, Hardware Programming ,File System ,Interrupts, BIOS handling , TSR Programming , General Concepts in C Language, handling inline Assembly statements

Experience

Research over 6+ Years

Organizations
CG-VAK Softwares and Exports Limited

Education/Credentials
Masters in Computer Applications

©2012 About.com, a part of The New York Times Company. All rights reserved.