Recent

Author Topic: While in Canvas how to read inputs from users?  (Read 1194 times)

Groeneg

  • Newbie
  • Posts: 5
While in Canvas how to read inputs from users?
« on: July 08, 2020, 11:04:57 am »
I am programming a simulation using Canvas and it works fine and looks good. Nice graphics and text outputs work fine.

However I now want to use user input to control the flow of the simulation.
1. Wait until user presses key
2. Increase animation speed by pressing +/- (decrease and increase delay value)
3. End generation by pressing N
3. End simulation by pressing Q
And so on.

But if I use Keypressed or Read etc. it doesn't work. I think it is because the latter functions only work in CRT mode but I'm not sure.
So if there are any tips, pointers or examples I would much appreciate it.

With regards Gerard
« Last Edit: July 08, 2020, 11:13:17 am by Groeneg »

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: While in Canvas how to read inputs from users?
« Reply #1 on: July 08, 2020, 12:45:18 pm »
Try with TForm events related with keys, they are OnKeyDown, OnKeyPress and OnKeyUp
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: While in Canvas how to read inputs from users?
« Reply #2 on: July 08, 2020, 12:52:30 pm »
Hi!

As Ñuño_Martínez said: use the keyboard events of the main form.

Set the KeyboardPreview of Form1 in the OI to true.

Connect to the keypress event of Form1:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
  2. begin
  3. case Key of
  4. '+' : .....
  5. '-' :  ....
  6. 'N': .....
  7. 'Q': self.close;
  8. end; // case
  9. end;
  10.  
  11.  
Winni

 

Groeneg

  • Newbie
  • Posts: 5
Re: While in Canvas how to read inputs from users?
« Reply #3 on: July 08, 2020, 02:11:55 pm »
Ok thanks, I will try this and keep you posted.

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: While in Canvas how to read inputs from users?
« Reply #4 on: July 08, 2020, 03:42:48 pm »
KeyPress, KeyUp and KeyDown are okay for GUI applications but for animations or games I recommend GetKeyState, using it you can get smooth even inertia movement. You can get the demo on recent versions of Lazarus:

Lazarus main menu > Tools > Example Projects > provide the keyword: sprite

For your information, don't open any demo directly from the source. It is better to copy it to a new location (for example Desktop) to prevent security or other issues.

If you interested to learn from the previous discussion, here:
https://forum.lazarus.freepascal.org/index.php/topic,41178.msg285678.html#msg285678

 

TinyPortal © 2005-2018