keyboard3.pp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. {
  2. Keyboard example for the PTCPas library
  3. This source code is in the public domain
  4. }
  5. program KeyboardExample3;
  6. {$MODE objfpc}
  7. uses
  8. ptc;
  9. function KeyCode2String(ACode: Integer): AnsiString;
  10. begin
  11. case ACode of
  12. PTCKEY_UNDEFINED : Result := 'PTCKEY_UNDEFINED';
  13. PTCKEY_CANCEL : Result := 'PTCKEY_CANCEL';
  14. PTCKEY_BACKSPACE : Result := 'PTCKEY_BACKSPACE';
  15. PTCKEY_TAB : Result := 'PTCKEY_TAB';
  16. PTCKEY_ENTER : Result := 'PTCKEY_ENTER';
  17. PTCKEY_CLEAR : Result := 'PTCKEY_CLEAR';
  18. PTCKEY_SHIFT : Result := 'PTCKEY_SHIFT';
  19. PTCKEY_CONTROL : Result := 'PTCKEY_CONTROL';
  20. PTCKEY_ALT : Result := 'PTCKEY_ALT';
  21. PTCKEY_PAUSE : Result := 'PTCKEY_PAUSE';
  22. PTCKEY_CAPSLOCK : Result := 'PTCKEY_CAPSLOCK';
  23. PTCKEY_KANA : Result := 'PTCKEY_KANA';
  24. PTCKEY_FINAL : Result := 'PTCKEY_FINAL';
  25. PTCKEY_KANJI : Result := 'PTCKEY_KANJI';
  26. PTCKEY_ESCAPE : Result := 'PTCKEY_ESCAPE';
  27. PTCKEY_CONVERT : Result := 'PTCKEY_CONVERT';
  28. PTCKEY_NONCONVERT : Result := 'PTCKEY_NONCONVERT';
  29. PTCKEY_ACCEPT : Result := 'PTCKEY_ACCEPT';
  30. PTCKEY_MODECHANGE : Result := 'PTCKEY_MODECHANGE';
  31. PTCKEY_SPACE : Result := 'PTCKEY_SPACE';
  32. PTCKEY_PAGEUP : Result := 'PTCKEY_PAGEUP';
  33. PTCKEY_PAGEDOWN : Result := 'PTCKEY_PAGEDOWN';
  34. PTCKEY_END : Result := 'PTCKEY_END';
  35. PTCKEY_HOME : Result := 'PTCKEY_HOME';
  36. PTCKEY_LEFT : Result := 'PTCKEY_LEFT';
  37. PTCKEY_UP : Result := 'PTCKEY_UP';
  38. PTCKEY_RIGHT : Result := 'PTCKEY_RIGHT';
  39. PTCKEY_DOWN : Result := 'PTCKEY_DOWN';
  40. PTCKEY_COMMA : Result := 'PTCKEY_COMMA';
  41. PTCKEY_PERIOD : Result := 'PTCKEY_PERIOD';
  42. PTCKEY_SLASH : Result := 'PTCKEY_SLASH';
  43. PTCKEY_ZERO : Result := 'PTCKEY_ZERO';
  44. PTCKEY_ONE : Result := 'PTCKEY_ONE';
  45. PTCKEY_TWO : Result := 'PTCKEY_TWO';
  46. PTCKEY_THREE : Result := 'PTCKEY_THREE';
  47. PTCKEY_FOUR : Result := 'PTCKEY_FOUR';
  48. PTCKEY_FIVE : Result := 'PTCKEY_FIVE';
  49. PTCKEY_SIX : Result := 'PTCKEY_SIX';
  50. PTCKEY_SEVEN : Result := 'PTCKEY_SEVEN';
  51. PTCKEY_EIGHT : Result := 'PTCKEY_EIGHT';
  52. PTCKEY_NINE : Result := 'PTCKEY_NINE';
  53. PTCKEY_SEMICOLON : Result := 'PTCKEY_SEMICOLON';
  54. PTCKEY_EQUALS : Result := 'PTCKEY_EQUALS';
  55. PTCKEY_A : Result := 'PTCKEY_A';
  56. PTCKEY_B : Result := 'PTCKEY_B';
  57. PTCKEY_C : Result := 'PTCKEY_C';
  58. PTCKEY_D : Result := 'PTCKEY_D';
  59. PTCKEY_E : Result := 'PTCKEY_E';
  60. PTCKEY_F : Result := 'PTCKEY_F';
  61. PTCKEY_G : Result := 'PTCKEY_G';
  62. PTCKEY_H : Result := 'PTCKEY_H';
  63. PTCKEY_I : Result := 'PTCKEY_I';
  64. PTCKEY_J : Result := 'PTCKEY_J';
  65. PTCKEY_K : Result := 'PTCKEY_K';
  66. PTCKEY_L : Result := 'PTCKEY_L';
  67. PTCKEY_M : Result := 'PTCKEY_M';
  68. PTCKEY_N : Result := 'PTCKEY_N';
  69. PTCKEY_O : Result := 'PTCKEY_O';
  70. PTCKEY_P : Result := 'PTCKEY_P';
  71. PTCKEY_Q : Result := 'PTCKEY_Q';
  72. PTCKEY_R : Result := 'PTCKEY_R';
  73. PTCKEY_S : Result := 'PTCKEY_S';
  74. PTCKEY_T : Result := 'PTCKEY_T';
  75. PTCKEY_U : Result := 'PTCKEY_U';
  76. PTCKEY_V : Result := 'PTCKEY_V';
  77. PTCKEY_W : Result := 'PTCKEY_W';
  78. PTCKEY_X : Result := 'PTCKEY_X';
  79. PTCKEY_Y : Result := 'PTCKEY_Y';
  80. PTCKEY_Z : Result := 'PTCKEY_Z';
  81. PTCKEY_OPENBRACKET : Result := 'PTCKEY_OPENBRACKET';
  82. PTCKEY_BACKSLASH : Result := 'PTCKEY_BACKSLASH';
  83. PTCKEY_CLOSEBRACKET : Result := 'PTCKEY_CLOSEBRACKET';
  84. PTCKEY_NUMPAD0 : Result := 'PTCKEY_NUMPAD0';
  85. PTCKEY_NUMPAD1 : Result := 'PTCKEY_NUMPAD1';
  86. PTCKEY_NUMPAD2 : Result := 'PTCKEY_NUMPAD2';
  87. PTCKEY_NUMPAD3 : Result := 'PTCKEY_NUMPAD3';
  88. PTCKEY_NUMPAD4 : Result := 'PTCKEY_NUMPAD4';
  89. PTCKEY_NUMPAD5 : Result := 'PTCKEY_NUMPAD5';
  90. PTCKEY_NUMPAD6 : Result := 'PTCKEY_NUMPAD6';
  91. PTCKEY_NUMPAD7 : Result := 'PTCKEY_NUMPAD7';
  92. PTCKEY_NUMPAD8 : Result := 'PTCKEY_NUMPAD8';
  93. PTCKEY_NUMPAD9 : Result := 'PTCKEY_NUMPAD9';
  94. PTCKEY_MULTIPLY : Result := 'PTCKEY_MULTIPLY';
  95. PTCKEY_ADD : Result := 'PTCKEY_ADD';
  96. PTCKEY_SEPARATOR : Result := 'PTCKEY_SEPARATOR';
  97. PTCKEY_SUBTRACT : Result := 'PTCKEY_SUBTRACT';
  98. PTCKEY_DECIMAL : Result := 'PTCKEY_DECIMAL';
  99. PTCKEY_DIVIDE : Result := 'PTCKEY_DIVIDE';
  100. PTCKEY_F1 : Result := 'PTCKEY_F1';
  101. PTCKEY_F2 : Result := 'PTCKEY_F2';
  102. PTCKEY_F3 : Result := 'PTCKEY_F3';
  103. PTCKEY_F4 : Result := 'PTCKEY_F4';
  104. PTCKEY_F5 : Result := 'PTCKEY_F5';
  105. PTCKEY_F6 : Result := 'PTCKEY_F6';
  106. PTCKEY_F7 : Result := 'PTCKEY_F7';
  107. PTCKEY_F8 : Result := 'PTCKEY_F8';
  108. PTCKEY_F9 : Result := 'PTCKEY_F9';
  109. PTCKEY_F10 : Result := 'PTCKEY_F10';
  110. PTCKEY_F11 : Result := 'PTCKEY_F11';
  111. PTCKEY_F12 : Result := 'PTCKEY_F12';
  112. PTCKEY_F13 : Result := 'PTCKEY_F13';
  113. PTCKEY_F14 : Result := 'PTCKEY_F14';
  114. PTCKEY_F15 : Result := 'PTCKEY_F15';
  115. PTCKEY_F16 : Result := 'PTCKEY_F16';
  116. PTCKEY_F17 : Result := 'PTCKEY_F17';
  117. PTCKEY_F18 : Result := 'PTCKEY_F18';
  118. PTCKEY_F19 : Result := 'PTCKEY_F19';
  119. PTCKEY_F20 : Result := 'PTCKEY_F20';
  120. PTCKEY_F21 : Result := 'PTCKEY_F21';
  121. PTCKEY_F22 : Result := 'PTCKEY_F22';
  122. PTCKEY_F23 : Result := 'PTCKEY_F23';
  123. PTCKEY_F24 : Result := 'PTCKEY_F24';
  124. PTCKEY_F25 : Result := 'PTCKEY_F25';
  125. PTCKEY_F26 : Result := 'PTCKEY_F26';
  126. PTCKEY_F27 : Result := 'PTCKEY_F27';
  127. PTCKEY_F28 : Result := 'PTCKEY_F28';
  128. PTCKEY_F29 : Result := 'PTCKEY_F29';
  129. PTCKEY_F30 : Result := 'PTCKEY_F30';
  130. PTCKEY_F31 : Result := 'PTCKEY_F31';
  131. PTCKEY_F32 : Result := 'PTCKEY_F32';
  132. PTCKEY_F33 : Result := 'PTCKEY_F33';
  133. PTCKEY_F34 : Result := 'PTCKEY_F34';
  134. PTCKEY_F35 : Result := 'PTCKEY_F35';
  135. PTCKEY_DELETE : Result := 'PTCKEY_DELETE';
  136. PTCKEY_NUMLOCK : Result := 'PTCKEY_NUMLOCK';
  137. PTCKEY_SCROLLLOCK : Result := 'PTCKEY_SCROLLLOCK';
  138. PTCKEY_PRINTSCREEN : Result := 'PTCKEY_PRINTSCREEN';
  139. PTCKEY_INSERT : Result := 'PTCKEY_INSERT';
  140. PTCKEY_HELP : Result := 'PTCKEY_HELP';
  141. PTCKEY_META : Result := 'PTCKEY_META';
  142. PTCKEY_MINUS : Result := 'PTCKEY_MINUS';
  143. PTCKEY_BACKQUOTE : Result := 'PTCKEY_BACKQUOTE';
  144. PTCKEY_QUOTE : Result := 'PTCKEY_QUOTE';
  145. PTCKEY_LESS : Result := 'PTCKEY_LESS';
  146. PTCKEY_COMMAND : Result := 'PTCKEY_COMMAND';
  147. PTCKEY_FUNCTION : Result := 'PTCKEY_FUNCTION';
  148. else
  149. Result := '';
  150. end;
  151. end;
  152. procedure DumpKey(AKey: IPTCKeyEvent);
  153. var
  154. mk: TPTCModifierKey;
  155. first: Boolean;
  156. begin
  157. Write('Code=', AKey.Code:3, ' (', KeyCode2String(AKey.Code):19,
  158. '), Unicode=$', HexStr(AKey.Unicode, 4), ', Press=', AKey.Press:5,
  159. ', Shift=', AKey.Shift:5, ', Alt=', AKey.Alt:5, ', Control=',
  160. AKey.Control:5, ', ModifierKeys=[');
  161. first := True;
  162. for mk in TPTCModifierKey do
  163. if mk in AKey.ModifierKeys then
  164. begin
  165. if not first then
  166. Write(',');
  167. first := False;
  168. Write(mk);
  169. end;
  170. Writeln(']');
  171. end;
  172. var
  173. console: IPTCConsole;
  174. format: IPTCFormat;
  175. key: IPTCKeyEvent;
  176. Done: Boolean;
  177. begin
  178. try
  179. try
  180. { create console }
  181. console := TPTCConsoleFactory.CreateNew;
  182. { enable key release events }
  183. console.KeyReleaseEnabled := True;
  184. { create format }
  185. format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  186. { open the console }
  187. console.open('Keyboard example 3', format);
  188. { main loop }
  189. Done := False;
  190. repeat
  191. { check for key press/release }
  192. while console.KeyPressed do
  193. begin
  194. console.ReadKey(key);
  195. case key.code of
  196. PTCKEY_ESCAPE:
  197. begin
  198. Done := True;
  199. Break;
  200. end;
  201. else
  202. DumpKey(key);
  203. end;
  204. end;
  205. { update console }
  206. console.update;
  207. until Done;
  208. finally
  209. if Assigned(console) then
  210. console.close;
  211. end;
  212. except
  213. on error: TPTCError do
  214. { report error }
  215. error.report;
  216. end;
  217. end.