home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / learn / getch.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-28  |  1001 b   |  41 lines

  1. /* GETCH.C illustrates how to process ASCII or extended keys.
  2.  * Functions illustrated include:
  3.  *      getch           getche
  4.  */
  5.  
  6. #include <conio.h>
  7. #include <ctype.h>
  8. #include <stdio.h>
  9.  
  10. main()
  11. {
  12.     int key;
  13.  
  14.     /* Read and display keys until ESC is pressed. */
  15.     while( 1 )
  16.     {
  17.         /* If first key is 0, then get second extended. */
  18.         if( !(key = getch()) )
  19.         {
  20.             key = getch();
  21.             printf( "ASCII: no\tChar: NA\t" );
  22.         }
  23.  
  24.         /* Otherwise there's only one key. */
  25.         else
  26.             printf( "ASCII: yes\tChar: %c \t", isgraph( key ) ? key : ' ' );
  27.  
  28.         printf( "Decimal: %d\tHex: %X\n", key, key );
  29.  
  30.         /* Echo character response to prompt. */
  31.         if( key == 27)
  32.         {
  33.             printf( "Do you really want to quit? (Y/n) " );
  34.             key = getche();
  35.             printf( "\n" );
  36.             if( (toupper( key ) == 'Y') || (key == 13) )
  37.                 break;
  38.         }
  39.     }
  40. }
  41.