keyboard.pp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2000 by Florian Klaempfl
  4. member of the Free Pascal development team
  5. Keyboard unit for OS/2
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit Keyboard;
  13. interface
  14. {$i keybrdh.inc}
  15. implementation
  16. uses
  17. KbdCalls, DosCalls;
  18. {$i keyboard.inc}
  19. const
  20. DefaultKeyboard = 0;
  21. Handle: word = DefaultKeyboard;
  22. procedure SysInitKeyboard;
  23. var
  24. K: TKbdInfo;
  25. begin
  26. if KbdGetFocus (IO_Wait, DefaultKeyboard) = No_Error then
  27. begin
  28. if KbdOpen (Handle) <> No_Error then
  29. Handle := DefaultKeyboard;
  30. KbdFlushBuffer (Handle);
  31. KbdFreeFocus (DefaultKeyboard);
  32. KbdGetFocus (IO_Wait, Handle);
  33. K.cb := SizeOf (K);
  34. KbdGetStatus (K, Handle);
  35. K.fsMask := $14;
  36. KbdSetStatus (K, Handle);
  37. end;
  38. end;
  39. procedure SysDoneKeyboard;
  40. begin
  41. KbdFreeFocus (Handle);
  42. if KbdGetFocus (IO_Wait, DefaultKeyboard) = No_Error then
  43. begin
  44. KbdClose (Handle);
  45. Handle := DefaultKeyboard;
  46. KbdFreeFocus (DefaultKeyboard);
  47. end;
  48. end;
  49. function SysGetKeyEvent: TKeyEvent;
  50. var
  51. K: TKbdKeyInfo;
  52. begin
  53. KbdGetFocus (IO_Wait, Handle);
  54. while (KbdCharIn (K, IO_NoWait, Handle) <> No_Error)
  55. or (K.fbStatus and $41 <> $40) do
  56. DosSleep (5);
  57. with K do
  58. begin
  59. if (byte (chChar) = $E0) and (fbStatus and 2 <> 0) then chChar := #0;
  60. SysGetKeyEvent := cardinal ($0300 or fsState and $F) shl 16 or
  61. cardinal (byte (chScan)) shl 8 or byte (chChar);
  62. end;
  63. end;
  64. function SysPollKeyEvent: TKeyEvent;
  65. var
  66. K: TKbdKeyInfo;
  67. Key : TKeyEvent;
  68. begin
  69. Key:=0;
  70. KbdGetFocus (IO_NoWait, Handle);
  71. if (KbdPeek (K, Handle) <> No_Error) or
  72. (K.fbStatus and $40 = 0) then
  73. FillChar (K, SizeOf (K), 0)
  74. else
  75. with K do
  76. begin
  77. if (byte (chChar) = $E0) and (fbStatus and 2 <> 0) then
  78. chChar := #0;
  79. Key:= cardinal ($0300 or fsState and $F) shl 16 or
  80. cardinal (byte (chScan)) shl 8 or byte (chChar);
  81. end;
  82. if (Key and $FFFF)=0 then
  83. Key := 0;
  84. SysPollKeyEvent:=Key;
  85. end;
  86. function SysGetShiftState: Byte;
  87. var
  88. K: TKbdInfo;
  89. begin
  90. KbdGetFocus (IO_NoWait, Handle);
  91. K.cb := SizeOf (K);
  92. if KbdGetStatus (K, Handle) = No_Error then
  93. SysGetShiftState := (K.fsState and $F)
  94. else
  95. SysGetShiftState := 0;
  96. end;
  97. Const
  98. SysKeyboardDriver : TKeyboardDriver = (
  99. InitDriver : @SysInitKeyBoard;
  100. DoneDriver : @SysDoneKeyBoard;
  101. GetKeyevent : @SysGetKeyEvent;
  102. PollKeyEvent : @SysPollKeyEvent;
  103. GetShiftState : @SysGetShiftState;
  104. TranslateKeyEvent : Nil;
  105. TranslateKeyEventUnicode : Nil;
  106. );
  107. begin
  108. SetKeyBoardDriver(SysKeyBoardDriver);
  109. SetKbdCtrlBreakHandler;
  110. end.