home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / keyboard.swg / 0010_Keyboard SCAN Keys.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-05-28  |  3.4 KB  |  124 lines

  1. {
  2. > I need help on reading the keyboard in a specific way, I need to read it
  3. >as a whole not a key at a time. I need to do this For the games I make, Iha
  4. >to ba able to hold down one key to perform a Function and then hold down
  5. >another key and scan both keys at the same time but to perform 2 different
  6. >Functions. For instance, if I hold down the left arrow key to make aCharact
  7. >run I should be able to hold down the space bar to make him fire agun at th
  8. >same time.
  9.  
  10.  by Sean Palmer, 1993, released to public domain
  11. }
  12.  
  13. Unit keyScan;  {for now, ignores extended codes ($E0 prefix)}
  14.  
  15. Interface
  16.  
  17. Type
  18.   scanCode = (
  19.     kNone, kEsc, k1, k2, k3, k4, k5, k6, k7, k8, k9, k0, kMinus, kEqual,
  20.     kBack, kTab, kQ, kW, kE, kR, kT, kY, kU, kI, kO, kP, kLBracket,
  21.     kRBracket, kEnter, kCtrl, kA, kS, kD, kF, kG, kH, kJ, kK, kL, kColon,
  22.     kQuote, kTilde, kLShift, kBackSlash, kZ, kX, kC, kV, kB, kN, kM, kComma,
  23.     kPeriod, kSlash, kRShift, kPadStar, kAlt, kSpace, kCaps, kF1, kF2, kF3,
  24.     kF4, kF5, kF6, kF7, kF8, kF9, kF10, kNum, kScroll, kHome, kUp, kPgUp,
  25.     kPadMinus, kLf, kPad5, kRt, kPadPlus, kend, kDn, kPgDn, kIns, kDel,
  26.     kSysReq, kUnknown55, kUnknown56, kF11, kF12);
  27.  
  28. Const
  29.   kPad7 = kHome;
  30.   kPad8 = kUp;
  31.   kPad9 = kPgUp;
  32.   kPad4 = kLf;
  33.   kPad6 = kRt;
  34.   kPad1 = kend;
  35.   kPad2 = kDn;
  36.   kPad3 = kPgDn;
  37.   letters = [kQ..kP, kA..kL, kZ..kM];
  38.   numbers = [k1..k0, kPad1..kPad3, kPad4..kPad6, kPad7..kPad9];
  39.   FunctionKeys = [kF1..kF10, kF11..kF12];
  40.   keyPad = [kPadStar, kNum..kDel];
  41.  
  42. Var
  43.  keyboard : set of scanCode;
  44.  lastKeyDown : scanCode;
  45.  
  46. Implementation
  47. Uses Dos;
  48.  
  49. Const
  50.   normChar : Array [scanCode] of Char = (
  51.   {00} #0,^[,'1','2','3','4','5','6','7','8','9','0','-','=',^H,^I,
  52.   {10} 'q','w','e','r','t','y','u','i','o','p','[',']',^M,#0,'a','s',
  53.   {20} 'd','f','g','h','j','k','l',';','''','`',#0,'\','z','x','c','v',
  54.   {30} 'b','n','m',',','.','/',#0,'*',#0,' ',#0,#0,#0,#0,#0,#0,
  55.   {40} #0,#0,#0,#0,#0,#0,#0,'7','8','9','-','4','5','6','+','1',
  56.   {50} '2','3','0','.',#0,#0,#0,#0,#0);
  57.   shiftChar : Array [scanCode] of Char = (
  58.   {00} #0,^[,'!','@','#','$','%','^','&','*','(',')','_','+',^H,^I,
  59.   {10} 'Q','W','E','R','T','Y','U','I','O','P','{','}',^M,#0,'A','S',
  60.   {20} 'D','F','G','H','J','K','L',':','"','~',#0,'|','Z','X','C','V',
  61.   {30} 'B','N','M','<','>','?',#0,'*',#0,' ',#0,#0,#0,#0,#0,#0,
  62.   {40} #0,#0,#0,#0,#0,#0,#0,'7','8','9','-','4','5','6','+','1',
  63.   {50} '2','3','0','.',#0,#0,#0,#0,#0);
  64.  
  65. Function ascii(k : scanCode) : Char;
  66. begin
  67.   if [kLShift, kRShift] * keyboard <> [] then
  68.     ascii := shiftChar[k]
  69.   else
  70.     ascii := normChar[k];
  71. end;
  72.  
  73. Var
  74.   oldKeyInt : Pointer;
  75.  
  76. Procedure keyISR; interrupt;
  77. Var
  78.   k : scanCode;
  79.   b : Byte;
  80. begin
  81.   Asm
  82.    in al, $60;
  83.    mov b, al;
  84.    and al, $7F;
  85.    mov k, al;
  86.    pushF;
  87.    call [oldKeyInt];      {allow BIOS to process also}
  88.   end;
  89.   memW[$40 : $1A] := memW[$40 : $1C];  {clear BIOS keyboard buffer}
  90.   if shortint(b) >= 0 then
  91.   begin
  92.     keyboard := keyboard + [k];
  93.     lastKeyDown := k;
  94.   end
  95.   else
  96.   if b <> $E0 then
  97.     keyboard := keyboard - [k]
  98.   else ;
  99. end;
  100.  
  101. Procedure keybegin;
  102. begin
  103.   keyboard := [];
  104.   lastKeyDown := kNone;
  105.   getIntVec(9, oldKeyInt);
  106.   setIntVec(9, @KeyISR);
  107. end;
  108.  
  109. Var
  110.   ExitSave:Pointer;
  111.  
  112. Procedure keyend;
  113. begin
  114.   setIntVec(9, oldKeyInt);
  115.   ExitProc := ExitSave;
  116. end;
  117.  
  118.  
  119. begin
  120.   keybegin;
  121.   ExitSave := ExitProc;
  122.   ExitProc := @keyend;
  123. end.
  124.