13.6 – Extended keyboard characters – Turbo C

by subbu on January 12, 2014

Extended keyboard codes:

We know that, every character on the keyboard has its ASCII value. I have already given a list of characters and their ASCII values under the session 3.1 Computer memory and data representation but have not given ASCII values for some of the special keys like arrow keys, function keys, tab, back space and escape key etc. All these non-printable characters are called extended keys. In this session we will see the usage of some extended keys.

Character ASCII Character ASCII
Back space 8 Up Arrow 72
Tab 9 Down Arrow 80
Esc 27 Left Arrow 75
Spacebar 32 Right Arrow 77
Enter 13 F1 to F10 59 to 68

Hello
Issue in accepting extended key from the keyboard:
getche() and getch() are the functions used to accept normal and extended keyboard characters from the keyboard.
getch() accepts the character from the keyboard and returns its ASCII value. It would not echoes (shows while typing) the character on the display.
getche() works similar to getch() but, echoes the character on the display.
There is a problem while accepting extended keyboard characters from the keyboard that we will see through an example

#include <stdio.h>
int main()
{
int ch;
printf("Enter any character:");
ch=getch();   /* Accepting character */
printf("\nThe ASCII value of given character is %d",ch);
return 0;
}

Execution:
Enter any character: [A] The ASCII value of given character is 65

Execution:
Enter any character: [Up Arrow] The ASCII value of given character is 0

Execution:
Enter any character: [Insert] The ASCII value of given character is 0

Note: Here character in [] is the input character but, doesn’t display (echoes)

It is demonstrating that, the ASCII numbers of “Insert” and “Top Arrow” keys are 0. Then how does the compiler find the difference between these extended keys? It is because, the actual ASCII value of character is still stored in the buffer while returning 0, the same way as the line feed character is remains in the buffer when we press the enter key after giving the data.

Date pipe

Using getch() for the second time will fetch the ASCII value of extended key

temp=getch();
ch=getch();

This change in the program helps to get the ASCII values of extended keys

#include <stdio.h>
int main()
{
int ch,temp;
printf("Enter any character:");
temp=getch();                           /* storing 0 */
ch=getch();                                /* storing ASCII value */
printf("\nThe ASCII value of given character is %d",ch);
return 0;
}

Execution:
Enter any character: [Up Arrow] The ASCII value of given character is 72

Execution:
Enter any character: [Down Arrow] The ASCII value of given character is 80

Execution:
Enter any character: [Esc] The ASCII value of given character is 27

Working with arrow keys:

Specification: Develop an application so that, a line follows as the arrow keys press. Use Esc key to quit the application

Graphical characters in C

Program:

#include<stdio.h>
#include<conio.h>
int main()
{
 char ch,dh='\0',temp;
 int x=40,y=11;
 clrscr();
 gotoxy(x,y);           /* beginning from the center */
 while(1)
 {
   temp=getch();        /* to store zero */
   ch=getch();          /* to store ASCII of extended key */
   switch(ch)
   {
     case 72:      /* up arrow */
      if(y>1)                /* checking the top corner */
       y--;                  /* moving up by 1 */
      gotoxy(x,y);
      cprintf("%c",179);    /* printing vertical graphical character */
      if(dh==77)             /* taking right to up turn */
      {
        y++;
        gotoxy(x,y);
        cprintf("%c",217);   /* printing right up corner */
      }
      if(dh==75)             /* taking left to up turn */
      {
        y++;
        gotoxy(x,y);
        cprintf("%c",192);   /* printing left up corner */
      }
      break;
     case 80:       /* down arrow */
      if(y<24)               /* checking the down corner */
        y++;                  /* moving down by 1 */
      gotoxy(x,y);
      cprintf("%c",179);    /* printing vertical graphical character */
      if(dh==77)             /* taking right to down turn */
      {
        y--;
        gotoxy(x,y);
        cprintf("%c",191);   /* printing right down character */
      }
      if(dh==75)             /* taking left to down turn */
      {
        y--;
        gotoxy(x,y);
        cprintf("%c",218);   /* printing left down corner */
      }
      break;
    case 75:       /* left arrow */
     if(x>1)               /* checking left corner */
       x--;                /* moving left by 1 */
     gotoxy(x,y);
     cprintf("%c",196);    /* printing horizontal graphical character */
     if(dh==80)            /* taking down to left turn */
     {
       x++;
       gotoxy(x,y);
       cprintf("%c",217);  /* printing down left corner */
    }
    if(dh==72)             /* taking up to left turn */
    {
       x++;
       gotoxy(x,y);
       cprintf("%c",191);  /* printing up left corner */
    }
    break;
  case 77:      /* right arrow */
    if(x<79)              /* checking right corner */
      x++;                 /* moving right by 1 */
    gotoxy(x,y);
    cprintf("%c",196);    /* printing horizontal graphical character */
    if(dh==80)            /* taking down to right turn */
    {
      x--;
      gotoxy(x,y);
      cprintf("%c",192);  /* printing down right corner */
    }
    if(dh==72)            /* taking up to right turn */
    {
      x--;
      gotoxy(x,y);
      cprintf("%c",218);  /* printing up right corner */
    }
    break;
  case 27:               /* checking for esc key */
   goto out;
 }
  dh=ch;                  /* making current character to previous character */
 }
out:
return 0;
}

Output

Using extended keys

Previous post:

Next post: