home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / pascal / 5162 < prev    next >
Encoding:
Text File  |  1992-09-01  |  9.8 KB  |  328 lines

  1. Newsgroups: comp.lang.pascal
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!ux1.cso.uiuc.edu!news.cs.indiana.edu!nstn.ns.ca!dragon.acadiau.ca!890491g
  3. From: 890491g@dragon.acadiau.ca (Don Graves)
  4. Subject: Re: Read any key from the keyboard?
  5. Message-ID: <1992Sep2.032347.12325@dragon.acadiau.ca>
  6. Organization: Acadia University
  7. References: <1992Sep1.123402.160@csghsg5a.bitnet> <1992Sep1.105717.11923@doug.cae.wisc.edu>
  8. Date: Wed, 2 Sep 1992 03:23:47 GMT
  9. Lines: 317
  10.  
  11. chris@castlab.engr.wisc.edu (Christian Rohrmeier) writes:
  12.  
  13. >In article <1992Sep1.123402.160@csghsg5a.bitnet> 89612048s@csghsg5a.bitnet writes:
  14. >>How to read any key from the keyboard?
  15. >>
  16. >>Can someone give me an example how to read any key pressed on the
  17. >>keyboard. That means also the ALT SHIFT CONTROL and combinations
  18. >>of these keys.
  19. >>
  20. >>And also, can you help me about the Num, Scrol and Capslock keys.
  21. >>How to turn them on and off, and how to get there current status?
  22. >>
  23. >>I want to say thank you very much for your help.
  24. >>
  25. >>Yours Markus
  26.  
  27.  
  28.  
  29.     I have a little unit that will answer most of this for you:
  30.  
  31. {---------- start here ------------}
  32.  
  33. {--------------------------------------------------------------------------}
  34. Unit KeyBoard;
  35. {--------------------------------------------------------------------------}
  36. { By Don Graves (Acadia University, Wolfville, N.S., Canada B1A 3N3 }
  37. { don.graves@dragon.acadiau.ca                                      }
  38. {--------------------------------------------------------------------------}
  39. Interface
  40. {--------------------------------------------------------------------------}
  41. Uses Dos;
  42. {--------------------------------------------------------------------------}
  43. Const
  44. { Scan code values }
  45. { list may not be TOTALLY complete and MAYBE there are errors }
  46.     K_F1=59;   { F1 Key }
  47.     K_F2=60;
  48.     K_F3=61;
  49.     K_F4=62;
  50.     K_F5=63;
  51.     K_F6=64;
  52.     K_F7=65;
  53.     K_F8=66;
  54.     K_F9=67;
  55.     K_F10=68;
  56.     K_F11=137;
  57.     K_F12=138;
  58.  
  59.     K_HOME=71;  { HOME key }
  60.     K_END=79;
  61.     K_PGUP=73;
  62.     K_PGDN=81;
  63.     K_INS=82;
  64.     K_DEL=83;
  65.     K_LEFT=75;
  66.     K_RIGHT=77;
  67.     K_UP=72;
  68.     K_DOWN=80;
  69.  
  70.     K_L_ENTER=28;
  71.     K_R_ENTER=224;
  72.     K_TAB=15;
  73.  
  74.     K_ESC=1;
  75.     K_BACKSPACE=14;
  76.     K_SPACE=57;
  77.  
  78.     CTRL_HOME=119;
  79.     CTRL_END=117;
  80.     CTRL_PGUP=132;
  81.     CTRL_PGDN=118;
  82.     CTRL_INS=146;
  83.     CTRL_DEL=147;
  84.     CTRL_LEFT=115;
  85.     CTRL_RIGHT=116;
  86.     CTRL_UP=141;
  87.     CTRL_DOWN=145;
  88.     CTRL_TAB=148;
  89.  
  90.     ALT_INS=162;
  91.     ALT_DEL=163;
  92.     ALT_HOME=151;
  93.     ALT_END=159;
  94.     ALT_PGUP=153;
  95.     ALT_PGDN=161;
  96.     ALT_UP=152;
  97.     ALT_DOWN=160;
  98.     ALT_RIGHT=157;
  99.     ALT_LEFT=155;
  100.  
  101.     K_Q=16;       { scan codes for letter KEYS (not ascii codes) }
  102.     K_W=17;
  103.     K_E=18;
  104.     K_R=19;
  105.     K_T=20;
  106.     K_Y=21;
  107.     K_U=22;
  108.     K_I=23;
  109.     K_O=24;
  110.     K_P=25;
  111.  
  112.     K_A=30;
  113.     K_S=31;
  114.     K_D=32;
  115.     K_F=33;
  116.     K_G=34;
  117.     K_H=35;
  118.     K_J=36;
  119.     K_K=37;
  120.     K_L=38;
  121.  
  122.     K_Z=44;
  123.     K_X=45;
  124.     K_C=46;
  125.     K_V=47;
  126.     K_B=48;
  127.     K_N=49;
  128.     K_M=50;
  129.  
  130. {--------------------------------------------------------------------------}
  131. Function KeyScan: Integer;
  132.     { This procedure waits for a key to be pressed (if a keypress is       }
  133.     { already available in the keyboard buffer, then it returns that press }
  134.     { instead of waiting for one).                                         }
  135. {--------------------------------------------------------------------------}
  136. Function KeyAvailable: Boolean;
  137.     { Returns TRUE if a keypress is waiting in the keyboard buffer and     }
  138.     { FALSE otherwise.                                                     }
  139. {--------------------------------------------------------------------------}
  140. Procedure FlushKeyBuffer;
  141.     { This procedure flushes the keyboard buffer.                          }
  142. {--------------------------------------------------------------------------}
  143. Function NumLock: Boolean;
  144.     { It returns TRUE if the flag is ON, FALSE otherwise.                  }
  145. {--------------------------------------------------------------------------}
  146. Function CapsLock: Boolean;
  147.     { It returns TRUE if the flag is ON, FALSE otherwise.                  }
  148. {--------------------------------------------------------------------------}
  149. Function ScrollLock: Boolean;
  150.     { It returns TRUE if the flag is ON, FALSE otherwise.                  }
  151. {--------------------------------------------------------------------------}
  152. Function InsertMode: Boolean;
  153.     { It returns TRUE if the flag is ON, FALSE otherwise.                  }
  154. {--------------------------------------------------------------------------}
  155. Function CtrlKey: Boolean;
  156.     { It returns TRUE if a CTRL key is pressed and FALSE                   }
  157.     { otherwise.                                                           }
  158. {--------------------------------------------------------------------------}
  159. Function AltKey: Boolean;
  160.     { It returns TRUE if an ALT key is pressed and FALSE                   }
  161.     { otherwise.                                                           }
  162. {--------------------------------------------------------------------------}
  163. Function LeftShift: Boolean;
  164.     { It returns TRUE if the left SHIFT key is pressed                     }
  165.     { and FALSE otherwise.                                                 }
  166. {--------------------------------------------------------------------------}
  167. Function RightShift: Boolean;
  168.     { It returns TRUE if the right SHIFT key is pressed                    }
  169.     { and FALSE otherwise.                                                 }
  170. {--------------------------------------------------------------------------}
  171. Procedure KeyboardRate (delay_value, repeat_rate: Integer);
  172.     { delay value (00h=250ms to 03h=1000ms)                                }
  173.     { repeat rate (00h=30/sec to 0Ch=10/sec [def] to                       }
  174.     { 1Fh=2/sec)                                                           }
  175. {--------------------------------------------------------------------------}
  176. Implementation
  177. {--------------------------------------------------------------------------}
  178. Const
  179.     ins_mode    = $80;
  180.     caps_lock   = $40;
  181.     num_lock    = $20;
  182.     scroll_lock = $10;
  183.     alt_key     = $08;
  184.     ctrl_key    = $04;
  185.     left_shift  = $02;
  186.     right_shift = $01;
  187.     kybd_port   = $60;
  188. {---------------------------------------------------------------------------}
  189. Var r: Registers;
  190. {---------------------------------------------------------------------------}
  191. Procedure KeyboardRate (delay_value, repeat_rate: Integer);
  192. Begin
  193.     r.ah := $03;
  194.     r.al := $05;
  195.     r.bh := delay_value;
  196.     r.bl := repeat_rate;
  197.     Intr ($16, r);
  198. End;
  199. {--------------------------------------------------------------------------}
  200. Procedure FlushKeyBuffer;
  201. Var head : word absolute $40:$1A;
  202.     tail : word absolute $40:$1C;
  203. Begin
  204.     tail := head;
  205. End;
  206. {--------------------------------------------------------------------------}
  207. Function KeyAvailable: Boolean;
  208. Var head : word absolute $40:$1A;
  209.     tail : word absolute $40:$1C;
  210. Begin
  211.     KeyAvailable := (NOT (head = tail));
  212. End;
  213. {--------------------------------------------------------------------------}
  214. Function KeyScan: Integer;
  215. Begin
  216.     r.ah := $10;
  217.     Intr ($16, r);
  218.     KeyScan := r.ah
  219. End;
  220. {--------------------------------------------------------------------------}
  221. Function NumLock: Boolean;
  222. Begin
  223.     r.ah := $12;
  224.     Intr ($16, r);
  225.     r.al := r.al And num_lock;
  226.     If (r.al = num_lock) Then
  227.         NumLock := TRUE
  228.     Else
  229.         NumLock := FALSE;
  230. End;
  231. {--------------------------------------------------------------------------}
  232. Function CapsLock: Boolean;
  233. Begin
  234.     r.ah := $12;
  235.     Intr ($16, r);
  236.     r.al := r.al And caps_lock;
  237.     If (r.al = caps_lock) Then
  238.         CapsLock := TRUE
  239.     Else
  240.         CapsLock := FALSE;
  241. End;
  242. {--------------------------------------------------------------------------}
  243. Function ScrollLock: Boolean;
  244. Begin
  245.     r.ah := $12;
  246.     Intr ($16, r);
  247.     r.al := r.al And scroll_lock;
  248.     If (r.al = scroll_lock) Then
  249.         ScrollLock := TRUE
  250.     Else
  251.         ScrollLock := FALSE;
  252. End;
  253. {--------------------------------------------------------------------------}
  254. Function InsertMode: Boolean;
  255. Begin
  256.     r.ah := $12;
  257.     Intr ($16, r);
  258.     r.al := r.al And ins_mode;
  259.     If (r.al = ins_mode) Then
  260.         InsertMode := TRUE
  261.     Else
  262.         InsertMode := FALSE;
  263. End;
  264. {--------------------------------------------------------------------------}
  265. Function CtrlKey: Boolean;
  266. Begin
  267.     r.ah := $12;
  268.     Intr ($16, r);
  269.     r.al := r.al And ctrl_key;
  270.     If (r.al = ctrl_key) Then
  271.         CtrlKey := TRUE
  272.     Else
  273.         CtrlKey := FALSE;
  274. End;
  275. {--------------------------------------------------------------------------}
  276. Function AltKey: Boolean;
  277. Begin
  278.     r.ah := $12;
  279.     Intr ($16, r);
  280.     r.al := r.al And alt_key;
  281.     If (r.al = alt_key) Then
  282.         AltKey := TRUE
  283.     Else
  284.         AltKey := FALSE;
  285. End;
  286. {--------------------------------------------------------------------------}
  287. Function LeftShift: Boolean;
  288. Begin
  289.     r.ah := $12;
  290.     Intr ($16, r);
  291.     r.al := r.al And left_shift;
  292.     If (r.al = left_shift) Then
  293.         LeftShift := TRUE
  294.     Else
  295.         LeftShift := FALSE;
  296. End;
  297. {--------------------------------------------------------------------------}
  298. Function RightShift: Boolean;
  299. Begin
  300.     r.ah := $12;
  301.     Intr ($16, r);
  302.     r.al := r.al And right_shift;
  303.     If (r.al = right_shift) Then
  304.         RightShift := TRUE
  305.     Else
  306.         RightShift := FALSE;
  307. End;
  308. {--------------------------------------------------------------------------}
  309. End.
  310. {--------------------------------------------------------------------------}
  311.  
  312. {---------- end here --------------}
  313.  
  314. You should be able to use this to solve your problem.  To test for shift/ctrl
  315. states, just use
  316. if RightShift then dowhatever;
  317. etc.
  318. None of these procedures change the states of the keyboard flag bits (I didn't
  319. get around to doing this ... yet), but it wouldn't be hard.
  320. Good luck.
  321.  
  322.  
  323. -- 
  324. --
  325. Don Graves
  326. Acadia University, Wolfville, N.S. Canada
  327. don.graves@dragon.acadiau.ca || 890491g@ace.acadiau.ca
  328.