home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20128 < prev    next >
Encoding:
Text File  |  1993-01-24  |  6.4 KB  |  246 lines

  1. Xref: sparky comp.lang.c:20128 comp.os.msdos.programmer:12383 alt.sources:3079
  2. Newsgroups: comp.lang.c,comp.os.msdos.programmer,alt.sources
  3. Path: sparky!uunet!utcsri!geac!lethe!druid!darcy
  4. From: darcy@druid.uucp (D'Arcy J.M. Cain)
  5. Subject: Re: Reading the arrow keys... how?
  6. Message-ID: <1993Jan23.193041.19858@druid.uucp>
  7. Date: Sat, 23 Jan 1993 19:30:41 GMT
  8. References: <1jmeupINN4g9@savoy.cc.williams.edu> <1993Jan22.010607.15208@cbfsb.cb.att.com>
  9. Organization: D'Arcy Cain Consulting
  10. Followup-To: comp.os.msdos.programmer
  11. Lines: 233
  12.  
  13. **NOTE:  I am crossposting to alt.sources because of the header file
  14. included but am redirecting followups to comp.os.msdos.programmer.
  15.  
  16. rnichols@cbnewsg.cb.att.com (robert.k.nichols) writes:
  17. >In article <1jmeupINN4g9@savoy.cc.williams.edu> 93gke@williams.edu (Just Ask The Axis) writes:
  18. >>I am trying to read the arrow keys from a PC keyboard in C. So far
  19. >You can use getch() to read the arrow keys.  The extended keys all
  20. >return a 2-byte sequence, with the first byte being either 0 or 0xE0.
  21.  
  22. Where did you get that 0xE0?  It returns a zero byte followed by the
  23. scan code.
  24.  
  25. Here is a header file I wrote which I find very useful.  The comments
  26. explain its usage.  Enjoy.
  27.  
  28. /*
  29. ascii.h
  30. Written by D'Arcy J.M. Cain
  31. D'Arcy Cain Consulting
  32. darcy@druid.com
  33.  
  34. ASCII control codes (including extended IBM codes)
  35.  
  36. This file is mainly a handy place to keep the standard names of ASCII
  37. control characters as well as a simple macro to return a control character.
  38. Further, if used on an MSDOS system it creates defines for the extended
  39. characters on the IBM keyboard.  The number defined is the scan code of
  40. the key shifted left 8 bits.  To use this create a simple function
  41. something like this:
  42.  
  43. int
  44. getkey(void)
  45. {
  46.     int c;
  47.  
  48.     if (!(c = getch()))        /* don't forget to include conio.h */
  49.         c = getch() << 8
  50.  
  51.     return(c);
  52. }
  53.  
  54. Now you can do the following:
  55.  
  56.     switch (c = getkey())
  57.     {
  58.         case UP_ARROW:
  59.             /* up arrow stuff */
  60.             break;
  61.  
  62.         case DOWN_ARROW:
  63.             /* down arrow stuff */
  64.             break;
  65.  
  66.         case FUNC1:
  67.         case ALT_H:
  68.             /* present help */
  69.             break;
  70.  
  71.         case CR:
  72.             /* do carriage return stuff */
  73.             break;
  74.  
  75.         /* etc ... */
  76.  
  77.         default:
  78.             if (isprint(c))
  79.                 /* put character into string or whatever */
  80.             break;
  81.     }
  82.  
  83. */
  84.  
  85. #ifndef        _ASCII_H
  86. #define        _ASCII_H
  87.  
  88. #ifndef        CTRL
  89. #define     CTRL(x) ((x) & 0x1f)
  90. #endif
  91.  
  92. #define     NUL     0x00
  93. #define     SOH     0x01
  94. #define     STX     0x02
  95. #define     ETX     0x03
  96. #define     EOT     0x04
  97. #define     ENQ     0x05
  98. #define     ACK     0x06
  99. #define     BEL     0x07
  100. #define     BS      0x08
  101. #define     HT      0x09
  102. #define     LF      0x0a
  103. #define     VT      0x0b
  104. #define     FF      0x0c
  105. #define     CR      0x0d
  106. #define     SO      0x0e
  107. #define     SI      0x0f
  108. #define     DLE     0x10
  109. #define     DC1     0x11
  110. #define     XON     0x11    /* alternate name for DC1 */
  111. #define     DC2     0x12
  112. #define     DC3     0x13
  113. #define     XOFF    0x13    /* alternate name for DC3 */
  114. #define     DC4     0x14
  115. #define     NAK     0x15
  116. #define     SYN     0x16
  117. #define     ETB     0x17
  118. #define     CAN     0x18
  119. #define     EM      0x19
  120. #define     SUB     0x1a
  121. #define     ESC     0x1b
  122. #define     FS      0x1c
  123. #define     GS      0x1d
  124. #define     RS      0x1e
  125. #define     US      0x1f
  126. #define     RUBOUT  0x7f
  127.  
  128. #ifndef        EOF
  129. #define        EOF        -1
  130. #endif
  131.  
  132. #ifdef        __MSDOS__
  133. #define     FUNC1      0x3b00
  134. #define     FUNC2      0x3c00
  135. #define     FUNC3      0x3d00
  136. #define     FUNC4      0x3e00
  137. #define     FUNC5      0x3f00
  138. #define     FUNC6      0x4000
  139. #define     FUNC7      0x4100
  140. #define     FUNC8      0x4200
  141. #define     FUNC9      0x4300
  142. #define     FUNC10     0x4400
  143.  
  144. #define     SHIFT_F1      0x5400
  145. #define     SHIFT_F2      0x5500
  146. #define     SHIFT_F3      0x5600
  147. #define     SHIFT_F4      0x5700
  148. #define     SHIFT_F5      0x5800
  149. #define     SHIFT_F6      0x5900
  150. #define     SHIFT_F7      0x5a00
  151. #define     SHIFT_F8      0x5b00
  152. #define     SHIFT_F9      0x5c00
  153. #define     SHIFT_F10     0x5d00
  154.  
  155. #define     CTL_F1      0x5e00
  156. #define     CTL_F2      0x5f00
  157. #define     CTL_F3      0x6000
  158. #define     CTL_F4      0x6100
  159. #define     CTL_F5      0x6200
  160. #define     CTL_F6      0x6300
  161. #define     CTL_F7      0x6400
  162. #define     CTL_F8      0x6500
  163. #define     CTL_F9      0x6600
  164. #define     CTL_F10     0x6700
  165.  
  166. #define     ALT_F1      0x6800
  167. #define     ALT_F2      0x6900
  168. #define     ALT_F3      0x6a00
  169. #define     ALT_F4      0x6b00
  170. #define     ALT_F5      0x6c00
  171. #define     ALT_F6      0x6d00
  172. #define     ALT_F7      0x6e00
  173. #define     ALT_F8      0x6f00
  174. #define     ALT_F9      0x7000
  175. #define     ALT_F10     0x7100
  176.  
  177. #define     ALT_1       0x7800
  178. #define     ALT_2       0x7900
  179. #define     ALT_3       0x7a00
  180. #define     ALT_4       0x7b00
  181. #define     ALT_5       0x7c00
  182. #define     ALT_6       0x7d00
  183. #define     ALT_7       0x7e00
  184. #define     ALT_8       0x7f00
  185. #define     ALT_9       0x8000
  186. #define     ALT_10      0x8100
  187.  
  188. #define     ALT_A       0x1e00
  189. #define     ALT_B       0x3000
  190. #define     ALT_C       0x2e00
  191. #define     ALT_D       0x2000
  192. #define     ALT_E       0x1200
  193. #define     ALT_F       0x2100
  194. #define     ALT_G       0x2200
  195. #define     ALT_H       0x2300
  196. #define     ALT_I       0x1700
  197. #define     ALT_J       0x2400
  198. #define     ALT_K       0x2500
  199. #define     ALT_L       0x2600
  200. #define     ALT_M       0x3200
  201. #define     ALT_N       0x3100
  202. #define     ALT_O       0x1800
  203. #define     ALT_P       0x1900
  204. #define     ALT_Q       0x1000
  205. #define     ALT_R       0x1300
  206. #define     ALT_S       0x1f00
  207. #define     ALT_T       0x1400
  208. #define     ALT_U       0x1600
  209. #define     ALT_V       0x2f00
  210. #define     ALT_W       0x1100
  211. #define     ALT_X       0x2d00
  212. #define     ALT_Y       0x1500
  213. #define     ALT_Z       0x2c00
  214.  
  215. #define     ALT_HYPHEN  0x8200
  216. #define     ALT_EQUAL   0x8300
  217.  
  218. #define     SHIFT_TAB   0x0f00
  219. #define     HOME        0x4700
  220. #define     UP_ARROW    0x4800
  221. #define     PG_UP       0x4900
  222. #define     LEFT_ARROW  0x4b00
  223. #define     RIGHT_ARROW 0x4d00
  224. #define     END         0x4f00
  225. #define     DOWN_ARROW  0x5000
  226. #define     PG_DOWN     0x5100
  227. #define     INSERT      0x5200
  228. #define        DELETE        0x5300
  229.  
  230. #define     CTL_PR_SCR      0x7200
  231. #define     CTL_LEFT_ARROW  0x7300
  232. #define     CTL_RIGHT_ARROW 0x7400
  233. #define     CTL_END         0x7500
  234. #define     CTL_PG_DOWN     0x7600
  235. #define     CTL_HOME        0x7700
  236. #define     CTL_PG_UP       0x8400
  237.  
  238. #endif        /* __MSDOS__ */
  239. #endif        /* _ASCII_H  */
  240.  
  241. -- 
  242. D'Arcy J.M. Cain (darcy@druid.com)  |
  243. D'Arcy Cain Consulting              |   There's no government
  244. Toronto, Ontario, Canada            |   like no government!
  245. +1 416 424 2871          DoD#0082   |
  246.