home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Game Programming in 21 Days / TYGAMES_R.ISO / source / day_07 / keytst.c < prev    next >
Encoding:
Text File  |  1994-07-25  |  6.0 KB  |  224 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. // D E F I N E S  ////////////////////////////////////////////////////////////
  12.  
  13. // bitmasks for control keys/shift status
  14.  
  15. #define SHIFT_R             0x0001
  16. #define SHIFT_L             0x0002
  17. #define CTRL                0x0004
  18. #define ALT                 0x0008
  19. #define SCROLL_LOCK_ON      0x0010
  20. #define NUM_LOCK_ON         0x0020
  21. #define CAPS_LOCK_ON        0x0040
  22. #define INSERT_MODE         0x0080
  23. #define CTRL_L              0x0100
  24. #define ALT_L               0x0200
  25. #define CTRL_R              0x0400
  26. #define ALT_R               0x0800
  27. #define SCROLL_LOCK_DWN     0x1000
  28. #define NUM_LOCK_DWN        0x2000
  29. #define CAPS_LOCK_DWN       0x4000
  30. #define SYS_REQ_DWN         0x8000
  31.  
  32. // scan code values, note keys with two symbols on them are the same so I will
  33. // consistantly try to use the lower symbol for example. the 1 key also has a
  34. // ! above it, but we would just call it the SCAN_1 key.
  35.  
  36. #define SCAN_ESC          1
  37. #define SCAN_1            2
  38. #define SCAN_2            3
  39. #define SCAN_3            4
  40. #define SCAN_4            5
  41. #define SCAN_5            6
  42. #define SCAN_6            7
  43. #define SCAN_7            8
  44. #define SCAN_8            9
  45. #define SCAN_9            10
  46. #define SCAN_0            11
  47. #define SCAN_MINUS        12
  48. #define SCAN_EQUALS       13
  49. #define SCAN_BKSP         14
  50. #define SCAN_TAB          15
  51. #define SCAN_Q            16
  52. #define SCAN_W            17
  53. #define SCAN_E            18
  54. #define SCAN_R            19
  55. #define SCAN_T            20
  56. #define SCAN_Y            21
  57. #define SCAN_U            22
  58. #define SCAN_I            23
  59. #define SCAN_O            24
  60. #define SCAN_P            25
  61. #define SCAN_LFT_BRACKET  26
  62. #define SCAN_RGT_BRACKET  27
  63. #define SCAN_ENTER        28
  64. #define SCAN_CTRL         29
  65.  
  66. #define SCAN_A            30
  67. #define SCAN_S            31
  68. #define SCAN_D            32
  69. #define SCAN_F            33
  70. #define SCAN_G            34
  71. #define SCAN_H            35
  72. #define SCAN_J            36
  73. #define SCAN_K            37
  74. #define SCAN_L            38
  75.  
  76. #define SCAN_SEMI         39
  77. #define SCAN_APOS         40
  78. #define SCAN_TILDE        41
  79.  
  80. #define SCAN_LEFT_SHIFT   42
  81. #define SCAN_BACK_SLASH   43
  82. #define SCAN_Z            44
  83. #define SCAN_X            45
  84. #define SCAN_C            46
  85. #define SCAN_V            47
  86. #define SCAN_B            48
  87. #define SCAN_N            49
  88. #define SCAN_M            50
  89. #define SCAN_COMMA        51
  90.  
  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. //  M A I N //////////////////////////////////////////////////////////////////
  182.  
  183. void main(void) // keyboard demo
  184. {
  185. unsigned char key;
  186. int done=0;
  187.  
  188. _clearscreen(_GCLEARSCREEN);
  189.  
  190. // show some info
  191.  
  192. printf("Press a key and look at the scan code it generates.");
  193. printf("\nTo exit the program press the 'q' key.");
  194.  
  195. // wait till user hits q
  196.  
  197. while(!done)
  198.      {
  199.  
  200.      _settextposition(4,0);
  201.  
  202.      // has the user presseda key
  203.  
  204.      if ( (key = Get_Scan_Code()) )
  205.         {
  206.         printf("Scan Code = %d  ",key);
  207.         printf("\nScan code interpretted as ASCII = %c",key);
  208.         } // end if key pressed
  209.  
  210.      // test for ctrl and alt keys
  211.  
  212.      if (Get_Control_Keys(CTRL))
  213.         printf("\ncontrol key pressed                      ");
  214.  
  215.      if (Get_Control_Keys(ALT))
  216.         printf("\nalt key pressed                          ");
  217.  
  218.      if (key==16) done=1; // 16 is the scan code for 'q'
  219.  
  220.      } // end main
  221.  
  222. } // end main
  223.  
  224.