Lazarus

Programming => General => Topic started by: Centauri on March 19, 2020, 02:59:17 pm

Title: Activate button with keypress
Post by: Centauri on March 19, 2020, 02:59:17 pm
Another newby question here. I have a simple login form with two buttons, LOGIN and CANCEL. I can set the default property of the LOGIN button to true so that it responds to the ENTER key, but what is the simplest method to get the CANCEL button to respond to the ESC key?
Title: Re: Activate button with keypress
Post by: Handoko on March 19, 2020, 04:08:31 pm
Try this:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     btnLogin: TButton;
  16.     btnCancel: TButton;
  17.     procedure btnCancelClick(Sender: TObject);
  18.     procedure btnLoginClick(Sender: TObject);
  19.     procedure FormCreate(Sender: TObject);
  20.     procedure FormKeyPress(Sender: TObject; var Key: char);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.btnLoginClick(Sender: TObject);
  33. begin
  34.   ShowMessage('You pressed Login button.');
  35. end;
  36.  
  37. procedure TForm1.FormCreate(Sender: TObject);
  38. begin
  39.   KeyPreview := True;
  40. end;
  41.  
  42. procedure TForm1.FormKeyPress(Sender: TObject; var Key: char);
  43. begin
  44.   if Key = #27 then btnCancelClick(Self);
  45. end;
  46.  
  47. procedure TForm1.btnCancelClick(Sender: TObject);
  48. begin
  49.   ShowMessage('You pressed Cancel button.');
  50. end;
  51.  
  52. end.

Note:
#27 is Esc key.
Title: Re: Activate button with keypress
Post by: GetMem on March 19, 2020, 04:08:49 pm
There are many ways to achieve this. The following method will work no matter which control has the focus:
1. Set KeyPreview to true(form property)
2. Create an OnKeyDown event for the form:
Code: Pascal  [Select][+][-]
  1. uses LCLType;
  2.  
  3. procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  4. begin
  5.   if Key = VK_ESCAPE then
  6.     ModalResult := mrCancel;
  7. end;

PS: @Handoko was faster. :)
Title: Re: Activate button with keypress
Post by: lucamar on March 19, 2020, 04:23:07 pm
Another newby question here. I have a simple login form with two buttons, LOGIN and CANCEL. I can set the default property of the LOGIN button to true so that it responds to the ENTER key, but what is the simplest method to get the CANCEL button to respond to the ESC key?

The related property to Default is Cancel; just as a button responds to <Enter> if it has its Default property set to True, so does it respond to <Esc> if it has its Cancel property set to True.

But note that this is basically intended for modal forms, to set the ModalResult; it might not work as expected in modaless forms.
Title: Re: Activate button with keypress
Post by: Centauri on March 19, 2020, 09:46:30 pm
@Handoko, thankyou, that worked fine. Thought I tried something like that without success, but probably my rusty syntax knowledge at fault ....
Title: Re: Activate button with keypress
Post by: Centauri on March 20, 2020, 08:43:36 am
Thankyou also lucamar - that solution also works without additional coding.
TinyPortal © 2005-2018