keyboard.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. {
  2. System independent keyboard interface for os2
  3. $Id$
  4. }
  5. uses
  6. {$IFDEF PPC_FPC}
  7. KbdCalls, DosCalls;
  8. {$ELSE}
  9. {$IFDEF PPC_VIRTUAL}
  10. OS2Base;
  11. {$ELSE}
  12. {$IFDEF PPC_BPOS2}
  13. Os2Subs, DosProcs;
  14. {$ELSE}
  15. {$IFDEF PPC_SPEED}
  16. BseSub, BseDos;
  17. {$ENDIF}
  18. {$ENDIF}
  19. {$ENDIF}
  20. {$ENDIF}
  21. {$IFDEF PPC_VIRTUAL}
  22. type
  23. TKbdKeyInfo = KbdKeyInfo;
  24. TKbdInfo = KbdInfo;
  25. {$ELSE}
  26. {$IFDEF PPC_SPEED}
  27. type
  28. TKbdKeyInfo = KbdKeyInfo;
  29. TKbdInfo = KbdInfo;
  30. {$ENDIF}
  31. {$ENDIF}
  32. {$IFNDEF PPC_FPC}
  33. type
  34. cardinal = longint;
  35. {$ENDIF}
  36. const
  37. DefaultKeyboard = 0;
  38. Handle: word = DefaultKeyboard;
  39. procedure InitKeyboard;
  40. var
  41. K: TKbdInfo;
  42. begin
  43. if KbdGetFocus (IO_Wait, DefaultKeyboard) = No_Error then
  44. begin
  45. if KbdOpen (Handle) <> No_Error then Handle := DefaultKeyboard;
  46. KbdFlushBuffer (Handle);
  47. KbdFreeFocus (DefaultKeyboard);
  48. KbdGetFocus (IO_Wait, Handle);
  49. K.cb := 10;
  50. KbdGetStatus (K, Handle);
  51. K.fsMask := $14;
  52. KbdSetStatus (K, Handle);
  53. end;
  54. end;
  55. procedure DoneKeyboard;
  56. begin
  57. KbdFreeFocus (Handle);
  58. if KbdGetFocus (IO_Wait, DefaultKeyboard) = No_Error then
  59. begin
  60. KbdClose (Handle);
  61. Handle := DefaultKeyboard;
  62. KbdFreeFocus (DefaultKeyboard);
  63. end;
  64. end;
  65. function GetKeyEvent: TKeyEvent;
  66. var
  67. K: TKbdKeyInfo;
  68. begin
  69. if PendingKeyEvent <> 0 then
  70. begin
  71. GetKeyEvent := PendingKeyEvent;
  72. PendingKeyEvent := 0;
  73. end else
  74. begin
  75. KbdGetFocus (IO_Wait, Handle);
  76. while (KbdCharIn (K, IO_Wait, Handle) <> No_Error)
  77. or (K.fbStatus and $40 = 0) do DosSleep (5);
  78. with K do
  79. begin
  80. if (byte (chChar) = $E0) and (fbStatus and 2 <> 0) then chChar := #0;
  81. GetKeyEvent := cardinal ($0300 or fsState and $F) shl 16 or
  82. cardinal (byte (chScan)) shl 8 or byte (chChar);
  83. end;
  84. end;
  85. end;
  86. function PollKeyEvent: TKeyEvent;
  87. var
  88. K: TKbdKeyInfo;
  89. begin
  90. if PendingKeyEvent = 0 then
  91. begin
  92. KbdGetFocus (IO_NoWait, Handle);
  93. if (KbdCharIn (K, IO_NoWait, Handle) <> No_Error) or
  94. (K.fbStatus and $40 = 0) then FillChar (K, SizeOf (K), 0) else
  95. with K do
  96. begin
  97. if (byte (chChar) = $E0) and (fbStatus and 2 <> 0) then chChar := #0;
  98. PendingKeyEvent := cardinal ($0300 or fsState and $F) shl 16 or
  99. cardinal (byte (chScan)) shl 8 or byte (chChar);
  100. end;
  101. end;
  102. PollKeyEvent := PendingKeyEvent;
  103. if PendingKeyEvent and $FFFF = 0 then PendingKeyEvent := 0;
  104. end;
  105. function PollShiftStateEvent: TKeyEvent;
  106. var
  107. K: TKbdInfo;
  108. begin
  109. KbdGetFocus (IO_NoWait, Handle);
  110. KbdGetStatus (K, Handle);
  111. PollShiftStateEvent := cardinal (K.fsState and $F) shl 16;
  112. end;
  113. type
  114. {$IFDEF PPC_FPC}
  115. TTranslationEntry = packed record
  116. {$ELSE}
  117. TTranslationEntry = record
  118. {$ENDIF}
  119. Min, Max: byte;
  120. Offset: word;
  121. end;
  122. const
  123. TranslationTableEntries = 12;
  124. TranslationTable: array [1..TranslationTableEntries] of TTranslationEntry =
  125. ((Min: $3B; Max: $44; Offset: kbdF1), { function keys F1-F10 }
  126. (Min: $54; Max: $5D; Offset: kbdF1), { Shift fn keys F1-F10 }
  127. (Min: $5E; Max: $67; Offset: kbdF1), { Ctrl fn keys F1-F10 }
  128. (Min: $68; Max: $71; Offset: kbdF1), { Alt fn keys F1-F10 }
  129. (Min: $85; Max: $86; Offset: kbdF11), { function keys F11-F12 }
  130. (Min: $87; Max: $88; Offset: kbdF11), { Shift+function keys F11-F12 }
  131. (Min: $89; Max: $8A; Offset: kbdF11), { Ctrl+function keys F11-F12 }
  132. (Min: $8B; Max: $8C; Offset: kbdF11), { Alt+function keys F11-F12 }
  133. (Min: 71; Max: 73; Offset: kbdHome), { Keypad keys kbdHome-kbdPgUp }
  134. (Min: 75; Max: 77; Offset: kbdLeft), { Keypad keys kbdLeft-kbdRight }
  135. (Min: 79; Max: 81; Offset: kbdEnd), { Keypad keys kbdEnd-kbdPgDn }
  136. (Min: $52; Max: $53; Offset: kbdInsert));
  137. function TranslateKeyEvent (KeyEvent: TKeyEvent): TKeyEvent;
  138. var
  139. I: integer;
  140. ScanCode: byte;
  141. begin
  142. if KeyEvent and $03000000 = $03000000 then
  143. begin
  144. if (KeyEvent and $000000FF <> 0) and (KeyEvent and $000000FF <> $E0) then
  145. TranslateKeyEvent := KeyEvent and $00FFFFFF else
  146. begin
  147. { This is a function key }
  148. ScanCode := (KeyEvent and $0000FF00) shr 8;
  149. I := 1;
  150. while (I <= TranslationTableEntries) and
  151. ((TranslationTable [I].Min > ScanCode) or
  152. (ScanCode > TranslationTable [I].Max)) do Inc (I);
  153. if I > TranslationTableEntries then TranslateKeyEvent := KeyEvent else
  154. TranslateKeyEvent := $02000000 + (KeyEvent and $00FF0000) +
  155. (ScanCode - TranslationTable[I].Min) + TranslationTable[I].Offset;
  156. end;
  157. end else TranslateKeyEvent := KeyEvent;
  158. end;
  159. function TranslateKeyEventUniCode (KeyEvent: TKeyEvent): TKeyEvent;
  160. begin
  161. TranslateKeyEventUniCode := KeyEvent;
  162. ErrorHandler (errKbdNotImplemented, nil);
  163. end;
  164. {
  165. $Log$
  166. Revision 1.3 2000-10-08 18:41:58 hajny
  167. * $E0 for ext. keys correctly discarded now
  168. Revision 1.2 2000/07/13 11:32:26 michael
  169. + removed logs
  170. }