keyboard3.pp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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): string;
  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_DELETE : Result := 'PTCKEY_DELETE';
  113. PTCKEY_NUMLOCK : Result := 'PTCKEY_NUMLOCK';
  114. PTCKEY_SCROLLLOCK : Result := 'PTCKEY_SCROLLLOCK';
  115. PTCKEY_PRINTSCREEN : Result := 'PTCKEY_PRINTSCREEN';
  116. PTCKEY_INSERT : Result := 'PTCKEY_INSERT';
  117. PTCKEY_HELP : Result := 'PTCKEY_HELP';
  118. PTCKEY_META : Result := 'PTCKEY_META';
  119. PTCKEY_MINUS : Result := 'PTCKEY_MINUS';
  120. PTCKEY_BACKQUOTE : Result := 'PTCKEY_BACKQUOTE';
  121. PTCKEY_QUOTE : Result := 'PTCKEY_QUOTE';
  122. else
  123. Result := '';
  124. end;
  125. end;
  126. procedure DumpKey(AKey: IPTCKeyEvent);
  127. var
  128. mk: TPTCModifierKey;
  129. first: Boolean;
  130. begin
  131. Write('Code=', AKey.Code:3, ' (', KeyCode2String(AKey.Code):19,
  132. '), Unicode=$', HexStr(AKey.Unicode, 4), ', Press=', AKey.Press:5,
  133. ', Shift=', AKey.Shift:5, ', Alt=', AKey.Alt:5, ', Control=',
  134. AKey.Control:5, ', ModifierKeys=[');
  135. first := True;
  136. for mk in TPTCModifierKey do
  137. if mk in AKey.ModifierKeys then
  138. begin
  139. if not first then
  140. Write(',');
  141. first := False;
  142. Write(mk);
  143. end;
  144. Writeln(']');
  145. end;
  146. var
  147. console: IPTCConsole;
  148. format: IPTCFormat;
  149. key: IPTCKeyEvent;
  150. Done: Boolean;
  151. begin
  152. try
  153. try
  154. { create console }
  155. console := TPTCConsoleFactory.CreateNew;
  156. { enable key release events }
  157. console.KeyReleaseEnabled := True;
  158. { create format }
  159. format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  160. { open the console }
  161. console.open('Keyboard example 3', format);
  162. { main loop }
  163. Done := False;
  164. repeat
  165. { check for key press/release }
  166. while console.KeyPressed do
  167. begin
  168. console.ReadKey(key);
  169. case key.code of
  170. PTCKEY_ESCAPE:
  171. begin
  172. Done := True;
  173. Break;
  174. end;
  175. else
  176. DumpKey(key);
  177. end;
  178. end;
  179. { update console }
  180. console.update;
  181. until Done;
  182. finally
  183. if Assigned(console) then
  184. console.close;
  185. end;
  186. except
  187. on error: TPTCError do
  188. { report error }
  189. error.report;
  190. end;
  191. end.