home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / lang / pascal / 6436 < prev    next >
Encoding:
Internet Message Format  |  1992-11-09  |  3.3 KB

  1. Path: sparky!uunet!psgrain!hippo!catpe!f61.n7101.z5.fidonet.org!Shane.Greyvenstein
  2. From: Shane.Greyvenstein@f61.n7101.z5.fidonet.org (Shane Greyvenstein)
  3. Sender: ufgate@catpe.alt.za (newsout1.26)
  4. Newsgroups: comp.lang.pascal
  5. Subject: inputting non-character keys
  6. Message-ID: <2792.2AFD5B8E@catpe.alt.za>
  7. Date: Sun, 08 Nov 92 12:01:05 GMT
  8. Organization: FidoNet node 5:7101/61 - Pyramid, Pretoria
  9. Lines: 130
  10.  
  11. Hello Jack!
  12.  
  13. In a msg of <06 Nov 92>, Jack Hammond writes to All:
  14.  
  15.  -> How does Turbo Pascal (I have 5.0) read such non-character keys
  16.  -> as the function keys, insert, home, pgup etc., and alt-keys?
  17.  
  18. Ok.  Well in posing this question, you're actually asking for two different parts of code.  The ALT/CTRL/LSHIFT/RSHIFT keys, etc. all generate SCAN codes and cannot be read via the ch:=readkey, etc. commands.  To find the scan code of a key, the following code can be used:
  19.  
  20.  
  21. uses CRT,DOS;
  22.  
  23.  
  24.  
  25. var oldvec                 : pointer; { save old int 9 vec here }
  26.     scode                  : byte;
  27.  
  28.  
  29.  
  30.  
  31.  
  32. procedure INT9HANDLER;  interrupt;
  33.  
  34. begin
  35.   scode:=port[$60];                    { get the scan code }
  36.  
  37.   if scode<>0 then
  38.   writeln(scode);                      { if not 0 then print }
  39.  
  40.   port[$20]:=$20;                      { tell PIC were done }
  41. end;
  42.  
  43.  
  44.  
  45.  
  46.  
  47. begin
  48.   writeln('Press ESC to quit ...');
  49.   getintvec($09,oldvec);                   { hook in }
  50.   setintvec($09,@int9handler);
  51.  
  52.   scode:=0;
  53.  
  54.   repeat                                { go until esc pressed }
  55.   until scode=1;
  56.  
  57.   setintvec($09,oldvec);                { uninstall }
  58. end.
  59.  
  60.  
  61.  
  62. As you can see, it's easily modifyable to process the keys.  Just change the interrupt handler to check for a certain scan code . and process it accordingly.  There is a FASTER method to do this too, without the use of interrupts.  All ALT/CTRL/SHIFT/etc. have their status bitmapped at adress $0000:$0417.  By declaring a byte absolute to this position, you can check the status of the keys using simple AND/OR routines.  The bitmap is as follows:
  63.  
  64.       bits           meaning
  65. 7 6 5 4 3 2 1 0
  66. ---------------------------------------
  67. . . . . . . . 1     Right Shift
  68. . . . . . . 1 .     Left Shift
  69. . . . . . 1 . .     Ctrl
  70. . . . . 1 . . .     Alt
  71. . . . 1 . . . .     Scroll Lock
  72. . . 1 . . . . .     Num Lock
  73. . 1 . . . . . .     Caps Lock
  74. 1 . . . . . . .     Insert Lock
  75. ---------------------------------------
  76.  
  77. As for standard keys like F1, ENTER, etc, use the following:
  78.  
  79. uses CRT;
  80.  
  81.  
  82.  
  83.  
  84.  
  85. procedure PRINT_KEY;
  86.  
  87.  
  88. var ch : char;
  89.  
  90.  
  91. begin
  92.   ch:=readkey;
  93.  
  94.   if ch=#0 then      { Key is extended.  (Function keys, INS, etc)
  95.   begin
  96.     write('Extended + #');
  97.     ch:=readkey;     { Read second key in extended sequence }
  98.  
  99.     writeln(ord(ch));
  100.   end
  101.  
  102.   else
  103.  
  104.   writeln(ord(ch);
  105. end;
  106.  
  107.  
  108.  
  109. The above will print out the character codes for the keys desired. To process them, a simple case statement as follows is all that is needed:
  110.  
  111.  
  112.  
  113. begin
  114.   ch:=readkey;
  115.  
  116.   case ch of
  117.  
  118.   #0  : begin
  119.           ch:=readkey;
  120.  
  121.           case ch of
  122.           #80 : writeln('UpArrow');
  123.           end;
  124.         end;
  125.  
  126.   #27 : writeln('ESC');
  127.   end;
  128. end;
  129.  
  130.  
  131.  
  132. Well, I hope that this answers all your questions.  It may be a bit unclear, but if so, don't hesitate to holler.
  133.  
  134. Kindest regards,
  135. Shane
  136.  
  137. --  
  138. INTERNET: Shane.Greyvenstein@f61.n7101.z5.fidonet.org
  139. via:  THE CATALYST BBS in Port Elizabeth, South Africa.
  140.        (catpe.alt.za)   +27-41-34-1122 HST or +27-41-34-2859, V32bis & HST.
  141.