kbd.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. {
  2. Free Pascal port of the OpenPTC C++ library.
  3. Copyright (C) 2001-2003 Nikolay Nikolov ([email protected])
  4. Original C++ version by Glenn Fiedler ([email protected])
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. }
  17. Constructor TWin32Keyboard.Create(window : HWND; thread : DWord; multithreaded : Boolean; EventQueue : TEventQueue);
  18. Begin
  19. m_monitor := Nil;
  20. m_event := Nil;
  21. Inherited Create(window, thread);
  22. m_monitor := TWin32Monitor.Create;
  23. m_event := TWin32Event.Create;
  24. { setup defaults }
  25. m_alt := False;
  26. m_shift := False;
  27. m_control := False;
  28. { setup data }
  29. FEventQueue := EventQueue;
  30. m_multithreaded := multithreaded;
  31. { enable buffering }
  32. m_enabled := True;
  33. End;
  34. Destructor TWin32Keyboard.Destroy;
  35. Begin
  36. m_event.Free;
  37. m_monitor.Free;
  38. Inherited Destroy;
  39. End;
  40. (*Function TWin32Keyboard.internal_PeekKey(window : TWin32Window; k : TPTCKeyEvent) : Boolean;
  41. Begin
  42. { check enabled flag }
  43. If Not m_enabled Then
  44. Begin
  45. Result := False;
  46. Exit;
  47. End;
  48. { enter monitor if multithreaded }
  49. If m_multithreaded Then
  50. m_monitor.enter;
  51. { update window }
  52. window.update;
  53. { is a key ready? }
  54. Result := ready;
  55. If Result = True Then
  56. k.Assign(m_buffer[m_tail]);
  57. { leave monitor if multithreaded }
  58. If m_multithreaded Then
  59. m_monitor.leave;
  60. End;
  61. Procedure TWin32Keyboard.internal_ReadKey(window : TWin32Window; k : TPTCKeyEvent);
  62. Var
  63. read : TPTCKeyEvent;
  64. Begin
  65. read := Nil;
  66. Try
  67. { check enabled flag }
  68. If Not m_enabled Then
  69. Begin
  70. read := TPTCKeyEvent.Create;
  71. Exit;
  72. End;
  73. { check if multithreaded }
  74. If m_multithreaded Then
  75. Begin
  76. { check if ready }
  77. If Not ready Then
  78. Begin
  79. { wait for key event }
  80. m_event.wait;
  81. { reset event }
  82. m_event.reset;
  83. End;
  84. { enter monitor }
  85. m_monitor.enter;
  86. { remove key }
  87. read := remove;
  88. { leave monitor }
  89. m_monitor.leave;
  90. End
  91. Else
  92. Begin
  93. { update until ready }
  94. While Not ready Do
  95. { update window }
  96. window.update;
  97. { remove key }
  98. read := remove;
  99. End;
  100. Finally
  101. If Assigned(read) Then
  102. k.Assign(read);
  103. read.Free;
  104. End;
  105. End;*)
  106. Procedure TWin32Keyboard.enable;
  107. Begin
  108. { enable buffering }
  109. m_enabled := True;
  110. End;
  111. Procedure TWin32Keyboard.disable;
  112. Begin
  113. { disable buffering }
  114. m_enabled := False;
  115. End;
  116. Function TWin32Keyboard.WndProc(hWnd : HWND; message : DWord; wParam : WPARAM; lParam : LPARAM) : LRESULT;
  117. Var
  118. i : Integer;
  119. scancode : Integer;
  120. KeyStateArray : Array[0..255] Of Byte;
  121. AsciiBuf : Word;
  122. press : Boolean;
  123. uni : Integer;
  124. tmp : Integer;
  125. Begin
  126. WndProc := 0;
  127. { check enabled flag }
  128. If Not m_enabled Then
  129. Exit;
  130. { process key message }
  131. If (message = WM_KEYDOWN) Or (message = WM_KEYUP) {Or ((message = WM_SYSKEYDOWN) And ((lParam And (1 Shl 29)) <> 0))} Then
  132. Begin
  133. If message = WM_KEYUP Then
  134. press := False
  135. Else
  136. press := True;
  137. { update modifiers }
  138. If wParam = VK_MENU Then
  139. { alt }
  140. m_alt := press
  141. Else
  142. If wParam = VK_SHIFT Then
  143. { shift }
  144. m_shift := press
  145. Else
  146. If wParam = VK_CONTROL Then
  147. { control }
  148. m_control := press;
  149. { enter monitor if multithreaded }
  150. If m_multithreaded Then
  151. m_monitor.enter;
  152. uni := -1;
  153. If GetKeyboardState(@KeyStateArray) Then
  154. Begin
  155. scancode := (lParam Shr 16) And $FF;
  156. {todo: ToUnicode (Windows NT)}
  157. tmp := ToAscii(wParam, scancode, @KeyStateArray, @AsciiBuf, 0);
  158. If (tmp = 1) Or (tmp = 2) Then
  159. Begin
  160. If tmp = 2 Then
  161. Begin
  162. // Writeln('[', AsciiBuf, ']'); {???? todo: dead keys ????}
  163. End
  164. Else
  165. Begin
  166. // Write(Chr(AsciiBuf));
  167. {todo: codepage -> unicode}
  168. If AsciiBuf <= 126 Then
  169. uni := AsciiBuf;
  170. End;
  171. End;
  172. End;
  173. { handle key repeat count }
  174. For i := 1 To lParam And $FFFF Do
  175. { create and insert key object }
  176. FEventQueue.AddEvent(TPTCKeyEvent.Create(wParam, uni, m_alt, m_shift, m_control, press));
  177. { check multithreaded flag }
  178. If m_multithreaded Then
  179. Begin
  180. { set event }
  181. m_event._set;
  182. { leave monitor }
  183. m_monitor.leave;
  184. End;
  185. End;
  186. (* Else
  187. If message = WM_KEYUP Then
  188. { update modifiers }
  189. If wParam = VK_MENU Then
  190. { alt up }
  191. m_alt := False
  192. Else
  193. If wParam = VK_SHIFT Then
  194. { shift up }
  195. m_shift := False
  196. Else
  197. If wParam = VK_CONTROL Then
  198. { control up }
  199. m_control := False;*)
  200. End;
  201. (*Procedure TWin32Keyboard.insert(_key : TPTCKeyEvent);
  202. Begin
  203. { check for overflow }
  204. If (m_head <> (m_tail - 1)) And
  205. ((m_tail <> 0) Or (m_head <> High(m_buffer))) Then
  206. Begin
  207. { insert key at head }
  208. m_buffer[m_head] := _key;
  209. { increase head }
  210. Inc(m_head);
  211. { wrap head from end to start }
  212. If m_head > High(m_buffer) Then
  213. m_head := Low(m_buffer);
  214. End;
  215. End;
  216. Function TWin32Keyboard.remove : TPTCKeyEvent;
  217. Begin
  218. { return key data from tail }
  219. remove := m_buffer[m_tail];
  220. { increase tail }
  221. Inc(m_tail);
  222. { wrap tail from end to start }
  223. If m_tail > High(m_buffer) Then
  224. m_tail := Low(m_buffer);
  225. End;
  226. Function TWin32Keyboard.ready : Boolean;
  227. Begin
  228. ready := m_head <> m_tail;
  229. End;
  230. *)