home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter05 / keytest / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-10  |  1.3 KB  |  48 lines

  1. ////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 5, KeyTest Program
  4. ////////////////////////////////////////////////////
  5.  
  6. #include <stdio.h>
  7. #include <allegro.h>
  8.  
  9. int main(void)
  10. {
  11.     int k, x, y;
  12.     int scancode, ascii;
  13.  
  14.     //initialize program    
  15.     allegro_init();
  16.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
  17.     install_keyboard();
  18.     
  19.     //display title
  20.     textout_ex(screen,font,"KeyTest Program", 0, 0, 15,0);
  21.     textout_ex(screen,font,"Press a key (ESC to quit)...", 0, 20, 15,0);
  22.  
  23.     //set starting position for text
  24.     x = SCREEN_W/2 - 60;
  25.     y = SCREEN_H/2 - 20;
  26.     
  27.     while (!key[KEY_ESC])
  28.     {
  29.         //get and convert scan code
  30.         k = readkey();
  31.         scancode = (k >> 8);
  32.         ascii = scancode_to_ascii(scancode);
  33.         
  34.         //display key values
  35.         textprintf_ex(screen, font, x, y, 15, 0,
  36.             "Key value  = %-6d", k); 
  37.         textprintf_ex(screen, font, x, y+15, 15, 0,
  38.             "Scan code  = %-6d", scancode);
  39.         textprintf_ex(screen, font, x, y+30, 15, 0,
  40.             "ASCII code = %-6d", ascii);
  41.         textprintf_ex(screen, font, x, y+45, 15, 0,
  42.             "Character  = %-6c", (char)ascii);
  43.     }
  44.     allegro_exit();
  45.     return 0;
  46. }
  47. END_OF_MAIN()
  48.