Lazarus

Programming => Graphics and Multimedia => Graphics => Topic started by: Groeneg on July 08, 2020, 11:04:57 am

Title: While in Canvas how to read inputs from users?
Post by: Groeneg 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
Title: Re: While in Canvas how to read inputs from users?
Post by: Ñuño_Martínez on July 08, 2020, 12:45:18 pm
Try with TForm events (https://wiki.lazarus.freepascal.org/TForm#Events) related with keys, they are OnKeyDown, OnKeyPress and OnKeyUp
Title: Re: While in Canvas how to read inputs from users?
Post by: winni 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

 
Title: Re: While in Canvas how to read inputs from users?
Post by: Groeneg on July 08, 2020, 02:11:55 pm
Ok thanks, I will try this and keep you posted.
Title: Re: While in Canvas how to read inputs from users?
Post by: Handoko 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