Saturday, November 2, 2013

getch() philosophy ...


Up till now we have used getch() several times to pause program output. But, by using getch() we can perform other important tasks also. getch() gets a character from the keyboard but it does not echo (display) that character on the screen.
Type the following code:
Program 10:
#include <stdio.h>
#include <conio.h>
void main()
{
     int gender; /* An integer variable. */
    clrscr();
    printf(“\nEnter Gender (1-Male, 2-Female):”);
    gender=getch();
    printf(“\nYour entered gender is =%d:”,gender);
    getch();
}

Output:
#----------------------------------------------------------------#
Enter Gender (1-Male, 2-Female):
Your entered gender is=49 (if you entered 1)

Enter Gender (1-Male, 2-Female):
Your entered gender is=50 (if you entered 2)
#----------------------------------------------------------------#

Strange output..!! From where this 49 and 50 comes? Definitely This question is  arises in your mind. Correct! Read the following paragraph carefully.

When you press a key from the keyboard, the keyboard circuit transmits a sequence of one or more 8-bit numbers to the computer. This sequence of 8-bits is called “scan code” and it uniquely identifies the key you pressed. Each key available on the keyboard has a unique scan code. The ROMBIOS routines translate scan code into two byte sequence. The first byte contains the ASCII code of the key you hit, and the second byte contains the scan code of the key you hit. If the key you hit is a special key, such as function key or arrow key, then first byte contains a value 0 (zero), whereas the second byte contains the scan code of the key.

Now, look at the following example.

#include <stdio.h>
#include <conio.h>
void main()
{
     int key;
    clrscr();
    printf(“\nPress any key…”);
    key=getch();
    printf(“\nASCII code of the key you pressed is=%d”,key);
    getch();
}

Run the program and press z. And observe the output.
#----------------------------------------------------------------#
Press any key…
ASCII code of the key you pressed is=122
#----------------------------------------------------------------#

Run the program several times by pressing function keys, arrow keys or numeric keypad keys and every time you will get this same output.

Press any key…
ASCII code of the key you pressed is=0

Now, we are going to write a program which handles both i.e. ASCII values as well as scan code.

#include <stdio.h>
#include <conio.h>
void main()
{
     int ascii,scan;
     clrscr();
     printf(“\nPress any key…”);
     ascii=getch(); /* Receive the first byte. */
     if(ascii==0) /* If special key is hit the first byte contains 0. */
    {
       scan=getch();
       printf(“\nScan code of the key you pressed is=%d”,scan);
    }
    printf(“\nASCII code of the key you pressed is=%d”,ascii);
    getch();
}

Run the program by pressing arrow keys and function keys. Program shows both ASCII and scan code of the keys. If you press F1 then program will display the following output.
Output:
#----------------------------------------------------------------#
Press any key…
Scan code of the key you pressed is=59
ASCII code of the key you pressed is=0

#----------------------------------------------------------------#

No comments:

Post a Comment