keyboard.pp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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: IPTCConsole;
  15. surface: IPTCSurface;
  16. format: IPTCFormat;
  17. color: IPTCColor;
  18. key: IPTCKeyEvent;
  19. x, y: Integer;
  20. size: Integer;
  21. delta: Integer;
  22. begin
  23. try
  24. try
  25. { create console }
  26. console := TPTCConsoleFactory.CreateNew;
  27. { create format }
  28. format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  29. { open the console }
  30. console.open('Keyboard example', format);
  31. { create surface matching console dimensions }
  32. surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
  33. { setup cursor data }
  34. x := surface.width div 2;
  35. y := surface.height div 2;
  36. size := surface.width div 10;
  37. color := TPTCColorFactory.CreateNew(1, 1, 1);
  38. { main loop }
  39. repeat
  40. { check for key press }
  41. if console.KeyPressed then
  42. begin
  43. { read console key press }
  44. console.ReadKey(key);
  45. { shift modifier }
  46. if key.shift then
  47. { move fast }
  48. delta := 10
  49. else
  50. { move slow }
  51. delta := 1;
  52. { handle cursor keys }
  53. case key.code of
  54. PTCKEY_LEFT: Dec(x, delta);
  55. PTCKEY_RIGHT: Inc(x, delta);
  56. PTCKEY_UP: Dec(y, delta);
  57. PTCKEY_DOWN: Inc(y, delta);
  58. { exit when escape is pressed }
  59. PTCKEY_ESCAPE: Break;
  60. end;
  61. end;
  62. { clear surface }
  63. surface.clear;
  64. { draw cursor as a quad }
  65. surface.clear(color, TPTCAreaFactory.CreateNew(x - size, y - size, x + size, y + size));
  66. { copy to console }
  67. surface.copy(console);
  68. { update console }
  69. console.update;
  70. until False;
  71. finally
  72. if Assigned(console) then
  73. console.close;
  74. end;
  75. except
  76. on error: TPTCError do
  77. { report error }
  78. error.report;
  79. end;
  80. end.