home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Game Programming Gurus / Tricks_of_the_Game_Programming_Gurus_SAMS_Publishing_1994.iso / source / chap_03 / key.c < prev    next >
Text File  |  1994-05-04  |  6KB  |  213 lines

  1.  
  2. // I N C L U D E S ///////////////////////////////////////////////////////////
  3.  
  4. #include <dos.h>
  5. #include <bios.h>
  6. #include <stdio.h>
  7. #include <math.h>
  8. #include <conio.h>
  9. #include <graph.h>
  10.  
  11.  
  12. // D E F I N E S  ////////////////////////////////////////////////////////////
  13.  
  14. // bitmasks for control keys/shift status
  15.  
  16. #define SHIFT_R             0x0001
  17. #define SHIFT_L             0x0002
  18. #define CTRL                0x0004
  19. #define ALT                 0x0008
  20. #define SCROLL_LOCK_ON      0x0010
  21. #define NUM_LOCK_ON         0x0020
  22. #define CAPS_LOCK_ON        0x0040
  23. #define INSERT_MODE         0x0080
  24. #define CTRL_L              0x0100
  25. #define ALT_L               0x0200
  26. #define CTRL_R              0x0400
  27. #define ALT_R               0x0800
  28. #define SCROLL_LOCK_DWN     0x1000
  29. #define NUM_LOCK_DWN        0x2000
  30. #define CAPS_LOCK_DWN       0x4000
  31. #define SYS_REQ_DWN         0x8000
  32.  
  33. // scan code values, note keys with two symbols on them are the same so I will
  34. // consistantly try to use the lower symbol for example. the 1 key also has a
  35. // ! above it, but we would just call it the SCAN_1 key.
  36.  
  37. #define SCAN_ESC          1
  38. #define SCAN_1            2
  39. #define SCAN_2            3
  40. #define SCAN_3            4
  41. #define SCAN_4            5
  42. #define SCAN_5            6
  43. #define SCAN_6            7
  44. #define SCAN_7            8
  45. #define SCAN_8            9
  46. #define SCAN_9            10
  47. #define SCAN_0            11
  48. #define SCAN_MINUS        12
  49. #define SCAN_EQUALS       13
  50. #define SCAN_BKSP         14
  51. #define SCAN_TAB          15
  52. #define SCAN_Q            16
  53. #define SCAN_W            17
  54. #define SCAN_E            18
  55. #define SCAN_R            19
  56. #define SCAN_T            20
  57. #define SCAN_Y            21
  58. #define SCAN_U            22
  59. #define SCAN_I            23
  60. #define SCAN_O            24
  61. #define SCAN_P            25
  62. #define SCAN_LFT_BRACKET  26
  63. #define SCAN_RGT_BRACKET  27
  64. #define SCAN_ENTER        28
  65. #define SCAN_CTRL         29
  66.  
  67. #define SCAN_A            30
  68. #define SCAN_S            31
  69. #define SCAN_D            32
  70. #define SCAN_F            33
  71. #define SCAN_G            34
  72. #define SCAN_H            35
  73. #define SCAN_J            36
  74. #define SCAN_K            37
  75. #define SCAN_L            38
  76.  
  77. #define SCAN_SEMI         39
  78. #define SCAN_APOS         40
  79. #define SCAN_TILDE        41
  80.  
  81. #define SCAN_LEFT_SHIFT   42
  82. #define SCAN_BACK_SLASH   43
  83. #define SCAN_Z            44
  84. #define SCAN_X            45
  85. #define SCAN_C            46
  86. #define SCAN_V            47
  87. #define SCAN_B            48
  88. #define SCAN_N            49
  89. #define SCAN_M            50
  90. #define SCAN_COMMA        51
  91. #define SCAN_PERIOD       52
  92. #define SCAN_FOWARD_SLASH 53
  93. #define SCAN_RIGHT_SHIFT  54
  94. #define SCAN_PRT_SCRN     55
  95. #define SCAN_ALT          56
  96. #define SCAN_SPACE        57
  97. #define SCAN_CAPS_LOCK    58
  98. #define SCAN_F1           59
  99. #define SCAN_F2           60
  100. #define SCAN_F3           61
  101. #define SCAN_F4           62
  102. #define SCAN_F5           63
  103. #define SCAN_F6           64
  104. #define SCAN_F7           65
  105. #define SCAN_F8           66
  106. #define SCAN_F9           67
  107. #define SCAN_F10          68
  108. #define SCAN_F11          133
  109. #define SCAN_F12          134
  110. #define SCAN_NUM_LOCK     69
  111. #define SCAN_SCROLL_LOCK  70
  112. #define SCAN_HOME         71
  113. #define SCAN_UP           72
  114. #define SCAN_PGUP         73
  115. #define SCAN_NUM_MINUS    74
  116. #define SCAN_LEFT         75
  117. #define SCAN_CENTER       76
  118. #define SCAN_RIGHT        77
  119. #define SCAN_NUM_PLUS     78
  120. #define SCAN_END          79
  121. #define SCAN_DOWN         80
  122. #define SCAN_PGDWN        81
  123. #define SCAN_INS          82
  124. #define SCAN_DEL          83
  125.  
  126. // F U N C T I O N S /////////////////////////////////////////////////////////
  127.  
  128. unsigned char Get_Ascii_Key(void)
  129.  
  130. {
  131.  
  132. // if there is a normal ascii key waiting then return it, else return 0
  133.  
  134. if (_bios_keybrd(_KEYBRD_READY))
  135.  return(_bios_keybrd(_KEYBRD_READ));
  136. else return(0);
  137.  
  138. } // end Get_Ascii_Key
  139.  
  140. //////////////////////////////////////////////////////////////////////////////
  141.  
  142. unsigned int Get_Control_Keys(unsigned int mask)
  143. {
  144. // return the status of all the requested control key
  145.  
  146. return(mask & _bios_keybrd(_KEYBRD_SHIFTSTATUS));
  147.  
  148. } // end Get_Control_Keys
  149.  
  150. //////////////////////////////////////////////////////////////////////////////
  151.  
  152. unsigned char Get_Scan_Code(void)
  153. {
  154. // get the scan code of a key press, since we have to look at status bits
  155. // let's use the inline assembler
  156.  
  157.  
  158. // is a key ready?
  159.  
  160. __asm
  161.     {
  162.     mov ah,01h          ; function 1: is a key ready?
  163.     int 16h             ; call the interrupt
  164.     jz empty            ; there was no key so exit
  165.     mov ah,00h          ; function 0: get the scan code please
  166.     int 16h             ; call the interrupt
  167.     mov al,ah           ; result was in ah so put into al
  168.     xor ah,ah           ; zero out ah
  169.     jmp done            ; data's in ax...let's blaze!
  170.  
  171. empty:
  172.      xor ax,ax          ; clear out ax i.e. 0 means no key
  173. done:
  174.  
  175.     } // end asm
  176.  
  177. // since data is in ax it will be returned properly
  178.  
  179. } // end Get_Scan_Code
  180.  
  181. //////////////////////////////////////////////////////////////////////////////
  182.  
  183. void main(void) // keyboard demo
  184. {
  185. unsigned char key;
  186. int done=0;
  187. unsigned int control;
  188.  
  189. _clearscreen(_GCLEARSCREEN);
  190.  
  191. while(!done)
  192.      {
  193.  
  194.      _settextposition(2,0);
  195.  
  196.      if ( (key = Get_Scan_Code()) )
  197.         printf("%c %d  ",key,key);
  198.  
  199.      // test for ctrl and alt keys
  200.  
  201.      if (Get_Control_Keys(CTRL))
  202.         printf("\ncontrol key pressed");
  203.  
  204.      if (Get_Control_Keys(ALT))
  205.         printf("\nalt key pressed    ");
  206.  
  207.      if (key==16) done=1; // 16 is the scan code for 'q'
  208.  
  209.      } // end main
  210.  
  211. } // end main
  212.  
  213.