Recent

Author Topic: How to call a form using keypress  (Read 3362 times)

rblakn

  • Newbie
  • Posts: 1
How to call a form using keypress
« on: July 30, 2018, 08:12:40 am »
i made two forms and i want to link the up by pressing "enter"
How to write the procedure?
following the procedure that lazarus provide
procedure TForm2.FormKeyPress(Sender: TObject; var Key: char);
begin
  if key = #13
  then
...
end;

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: How to call a form using keypress
« Reply #1 on: July 30, 2018, 08:54:29 am »
Hello rblakn,
Welcome to the forum.

Maybe something like this:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, Forms, StdCtrls, unit2;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Label1: TLabel;
  16.     procedure FormCreate(Sender: TObject);
  17.     procedure FormKeyPress(Sender: TObject; var Key: char);
  18.   end;
  19.  
  20. var
  21.   Form1: TForm1;
  22.  
  23. const
  24.   TextShowForm = 'Press [Enter] to open side form';
  25.   TextHideForm = 'Press [Enter] to hide side form';
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.FormCreate(Sender: TObject);
  34. begin
  35.   Label1.Caption := TextShowForm;
  36. end;
  37.  
  38. procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
  39. begin
  40.   if not(Key = #13) then Exit;
  41.   case Form2.Showing of
  42.     True:  Form2.Visible  := False;
  43.     False: begin
  44.              Form2.Visible  := True;
  45.              Form2.Left     := Form1.Left + Form1.Width + 10;
  46.              Form2.Top      := Form1.Top;
  47.              Label1.Caption := TextHideForm;
  48.            end;
  49.   end;
  50. end;
  51.  
  52. end.

You can download the source FormTest.zip for testing.

nouzi

  • Sr. Member
  • ****
  • Posts: 298
Re: How to call a form using keypress
« Reply #2 on: July 30, 2018, 04:36:05 pm »
Handoko nice code    add this Label1.Caption := TextshowForm;

Code: Pascal  [Select][+][-]
  1. case Form2.Showing of
  2.     True:  begin
  3.              Form2.Visible  := False;
  4.              Label1.Caption := TextshowForm;
  5.            end;  

My English is  bad
Lazarus last version free pascal last version
Lazarus trunk  free pascal trunk 
System : Linux mint  64bit  Windows 7 64bit

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: How to call a form using keypress
« Reply #3 on: July 30, 2018, 04:39:50 pm »
:) I knew and already thought and solved it. That is handled by TForm2.FormClose. I tested it several times, it won't work correctly if the code is put on Form1 because after Form2 opened, Form1 cannot detect keypress because the focus is on Form2.

Test it, and you will know what I mean.
« Last Edit: July 30, 2018, 04:54:03 pm by Handoko »

nouzi

  • Sr. Member
  • ****
  • Posts: 298
Re: How to call a form using keypress
« Reply #4 on: July 31, 2018, 10:31:27 am »
 8) Smart movement Handoko
My English is  bad
Lazarus last version free pascal last version
Lazarus trunk  free pascal trunk 
System : Linux mint  64bit  Windows 7 64bit

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: How to call a form using keypress
« Reply #5 on: July 31, 2018, 11:24:34 am »
I didn't know "showing" existed. I would always simply test/set visibility.

 

TinyPortal © 2005-2018