Recent

Author Topic: Activate button with keypress  (Read 472 times)

Centauri

  • New Member
  • *
  • Posts: 25
Activate button with keypress
« 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?

Handoko

  • Hero Member
  • *****
  • Posts: 3613
  • My goal: build my own game engine using Lazarus
Re: Activate button with keypress
« Reply #1 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.

GetMem

  • Hero Member
  • *****
  • Posts: 3743
Re: Activate button with keypress
« Reply #2 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. :)
« Last Edit: March 19, 2020, 04:10:34 pm by GetMem »

lucamar

  • Hero Member
  • *****
  • Posts: 2955
Re: Activate button with keypress
« Reply #3 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.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Centauri

  • New Member
  • *
  • Posts: 25
Re: Activate button with keypress
« Reply #4 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 ....

Centauri

  • New Member
  • *
  • Posts: 25
Re: Activate button with keypress
« Reply #5 on: March 20, 2020, 08:43:36 am »
Thankyou also lucamar - that solution also works without additional coding.

 

TinyPortal © 2005-2018