Maybe:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, Forms, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure KeyPress(Sender: TObject; var Key: char); virtual;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.OnKeyPress := @KeyPress;
Button2.OnKeyPress := @KeyPress;
Button3.OnKeyPress := @KeyPress;
end;
procedure TForm1.KeyPress(Sender: TObject; var Key: char);
var
S: string;
begin
if Sender = Button1 then
S := 'first button.'
else
if Sender = Button2 then
S := 'second button.'
else
if Sender = Button3 then
S := 'third button.'
else Exit;
Label1.Caption := Key + ' is pressed on the ' + S;
end;
end.
I'd rather not do it this way for two reasons: 1) I'd have to do this for every widget in the window which is more than the example in this post. 2) The keys are still be handled by the buttons. If the user hits enter,
KeyPress does catch it but so does the button which means the button is clicked.
Edit: If you can prevent the second, this isn't so bad.