keyboard.pp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Keyboard example for OpenPTC 1.0 C++ implementation
  6. Copyright (c) Glenn Fiedler ([email protected])
  7. This source code is in the public domain
  8. }
  9. program KeyboardExample;
  10. {$MODE objfpc}
  11. uses
  12. ptc;
  13. var
  14. console: TPTCConsole = nil;
  15. surface: TPTCSurface = nil;
  16. format: TPTCFormat = nil;
  17. color: TPTCColor = nil;
  18. key: TPTCKeyEvent = nil;
  19. area: TPTCArea;
  20. x, y: Integer;
  21. size: Integer;
  22. delta: Integer;
  23. begin
  24. try
  25. try
  26. { create key }
  27. key := TPTCKeyEvent.Create;
  28. { create console }
  29. console := TPTCConsole.Create;
  30. { create format }
  31. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  32. { open the console }
  33. console.open('Keyboard example', format);
  34. { create surface matching console dimensions }
  35. surface := TPTCSurface.Create(console.width, console.height, format);
  36. { setup cursor data }
  37. x := surface.width div 2;
  38. y := surface.height div 2;
  39. size := surface.width div 10;
  40. color := TPTCColor.Create(1, 1, 1);
  41. { main loop }
  42. repeat
  43. { check for key press }
  44. if console.KeyPressed then
  45. begin
  46. { read console key press }
  47. console.ReadKey(key);
  48. { shift modifier }
  49. if key.shift then
  50. { move fast }
  51. delta := 10
  52. else
  53. { move slow }
  54. delta := 1;
  55. { handle cursor keys }
  56. case key.code of
  57. PTCKEY_LEFT: Dec(x, delta);
  58. PTCKEY_RIGHT: Inc(x, delta);
  59. PTCKEY_UP: Dec(y, delta);
  60. PTCKEY_DOWN: Inc(y, delta);
  61. { exit when escape is pressed }
  62. PTCKEY_ESCAPE: Break;
  63. end;
  64. end;
  65. { clear surface }
  66. surface.clear;
  67. { setup cursor area }
  68. area := TPTCArea.Create(x - size, y - size, x + size, y + size);
  69. try
  70. { draw cursor as a quad }
  71. surface.clear(color, area);
  72. finally
  73. area.Free;
  74. end;
  75. { copy to console }
  76. surface.copy(console);
  77. { update console }
  78. console.update;
  79. until False;
  80. finally
  81. color.Free;
  82. console.close;
  83. console.Free;
  84. surface.Free;
  85. key.Free;
  86. format.Free;
  87. end;
  88. except
  89. on error: TPTCError do
  90. { report error }
  91. error.report;
  92. end;
  93. end.