Advertisement
Mygod

easycrt Pascal Module

Jan 4th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 6.02 KB | None | 0 0
  1. unit easycrt;
  2.  
  3. interface
  4.  
  5. uses dos,windows;
  6.  
  7. var SpecialKey,DoingNumChars: Boolean; DoingNumCode: Byte; ScanCode:char;
  8. function ReadKey:char;
  9.  
  10. implementation
  11.  
  12. Function RemapScanCode (ScanCode: byte; CtrlKeyState: byte): byte;
  13. var AltKey, CtrlKey, ShiftKey: boolean;
  14. const CtrlKeypadKeys: array[$47..$53] of byte =
  15.    ($77, $8D, $84, $8E, $73, $8F, $74, $4E, $75, $91, $76, $92, $93);
  16. begin
  17.  AltKey := ((CtrlKeyState AND
  18.            (RIGHT_ALT_PRESSED OR LEFT_ALT_PRESSED)) > 0);
  19.  CtrlKey := ((CtrlKeyState AND
  20.            (RIGHT_CTRL_PRESSED OR LEFT_CTRL_PRESSED)) > 0);
  21.  ShiftKey := ((CtrlKeyState AND SHIFT_PRESSED) > 0);
  22.  
  23.  if AltKey then
  24.   begin
  25.    case ScanCode of
  26.    $02..$0D: inc(ScanCode, $76);
  27.    // Function keys
  28.    $3B..$44: inc(Scancode, $2D);
  29.    $57..$58: inc(Scancode, $34);
  30.    // Extended cursor block keys
  31.    $47..$49, $4B, $4D, $4F..$53:
  32.              inc(Scancode, $50);
  33.    // Other keys
  34.    $1C:      Scancode := $A6;   // Enter
  35.    $35:      Scancode := $A4;   // / (keypad and normal!)
  36.    end
  37.   end
  38.  else if CtrlKey then
  39.    case Scancode of
  40.    // Tab key
  41.    $0F:      Scancode := $94;
  42.    // Function keys
  43.    $3B..$44: inc(Scancode, $23);
  44.    $57..$58: inc(Scancode, $32);
  45.    // Keypad keys
  46.    $35:      Scancode := $95;   // \
  47.    $37:      Scancode := $96;   // *
  48.    $47..$53: Scancode := CtrlKeypadKeys[Scancode];
  49.    //Enter on Numpad
  50.    $1C:
  51.    begin
  52.      Scancode := $0A;
  53.      SpecialKey := False;
  54.    end;
  55.    end
  56.  else if ShiftKey then
  57.    case Scancode of
  58.    // Function keys
  59.    $3B..$44: inc(Scancode, $19);
  60.    $57..$58: inc(Scancode, $30);
  61.    //Enter on Numpad
  62.    $1C:
  63.    begin
  64.      Scancode := $0D;
  65.      SpecialKey := False;
  66.    end;
  67.    end
  68.  else
  69.    case Scancode of
  70.      // Function keys
  71.      $57..$58: inc(Scancode, $2E); // F11 and F12
  72.      //Enter on NumPad
  73.      $1C:
  74.        begin
  75.          Scancode := $0D;
  76.          SpecialKey := False;
  77.        end;
  78.  end;
  79.  RemapScanCode := ScanCode;
  80. end;
  81. function KeyPressed : boolean;
  82. var
  83.   nevents,nread : dword;
  84.   buf : TINPUTRECORD;
  85.   AltKey: Boolean;
  86.   c : longint;
  87. begin
  88.   KeyPressed := FALSE;
  89.   if ScanCode <> #0 then
  90.     KeyPressed := TRUE
  91.   else
  92.    begin
  93.      GetNumberOfConsoleInputEvents(TextRec(input).Handle,nevents);
  94.      while nevents>0 do
  95.        begin
  96.           ReadConsoleInputA(TextRec(input).Handle,buf,1,nread);
  97.           if buf.EventType = KEY_EVENT then
  98.             if buf.Event.KeyEvent.bKeyDown then
  99.               begin
  100.                  { Alt key is VK_MENU }
  101.                  { Capslock key is VK_CAPITAL }
  102.  
  103.                  AltKey := ((Buf.Event.KeyEvent.dwControlKeyState AND
  104.                             (RIGHT_ALT_PRESSED OR LEFT_ALT_PRESSED)) > 0);
  105.                  if not(Buf.Event.KeyEvent.wVirtualKeyCode in [VK_SHIFT, VK_MENU, VK_CONTROL,
  106.                                                       VK_CAPITAL, VK_NUMLOCK,
  107.                                                       VK_SCROLL]) then
  108.                    begin
  109.                       keypressed:=true;
  110.  
  111.                       if (ord(buf.Event.KeyEvent.AsciiChar) = 0) or
  112.                          (buf.Event.KeyEvent.dwControlKeyState and (LEFT_ALT_PRESSED or ENHANCED_KEY) > 0) then
  113.                         begin
  114.                            SpecialKey := TRUE;
  115.                            ScanCode := Chr(RemapScanCode(Buf.Event.KeyEvent.wVirtualScanCode, Buf.Event.KeyEvent.dwControlKeyState));
  116.                         end
  117.                       else
  118.                         begin
  119.                            { Map shift-tab }
  120.                            if (buf.Event.KeyEvent.AsciiChar=#9) and
  121.                               (buf.Event.KeyEvent.dwControlKeyState and SHIFT_PRESSED > 0) then
  122.                             begin
  123.                               SpecialKey := TRUE;
  124.                               ScanCode := #15;
  125.                             end
  126.                            else
  127.                             begin
  128.                               SpecialKey := FALSE;
  129.                               ScanCode := Chr(Ord(buf.Event.KeyEvent.AsciiChar));
  130.                             end;
  131.                         end;
  132.  
  133.                       if AltKey then
  134.                         begin
  135.                            case Buf.Event.KeyEvent.wVirtualScanCode of
  136.                              71 : c:=7;
  137.                              72 : c:=8;
  138.                              73 : c:=9;
  139.                              75 : c:=4;
  140.                              76 : c:=5;
  141.                              77 : c:=6;
  142.                              79 : c:=1;
  143.                              80 : c:=2;
  144.                              81 : c:=3;
  145.                              82 : c:=0;
  146.                            else
  147.                              break;
  148.                            end;
  149.                            DoingNumChars := true;
  150.                            DoingNumCode := Byte((DoingNumCode * 10) + c);
  151.                            Keypressed := false;
  152.                            Specialkey := false;
  153.                            ScanCode := #0;
  154.                         end
  155.                       else
  156.                         break;
  157.                    end;
  158.               end
  159.              else
  160.               begin
  161.                 if (Buf.Event.KeyEvent.wVirtualKeyCode in [VK_MENU]) then
  162.                if DoingNumChars then
  163.                  if DoingNumCode > 0 then
  164.                    begin
  165.                       ScanCode := Chr(DoingNumCode);
  166.                       Keypressed := true;
  167.  
  168.                       DoingNumChars := false;
  169.                       DoingNumCode := 0;
  170.                       break
  171.                    end; { if }
  172.               end;
  173.           { if we got a key then we can exit }
  174.           if keypressed then
  175.             exit;
  176.           GetNumberOfConsoleInputEvents(TextRec(input).Handle,nevents);
  177.        end;
  178.    end;
  179. end;
  180. function ReadKey: char;
  181. begin
  182.   while (not KeyPressed) do
  183.     Sleep(1);
  184.   if SpecialKey then begin
  185.     ReadKey := #0;
  186.     SpecialKey := FALSE;
  187.   end else begin
  188.     ReadKey := ScanCode;
  189.     ScanCode := #0;
  190.   end;
  191. end;
  192.  
  193.  
  194. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement