Can anyone explain why the OnKeyPress Event does not work with a TButton on the same Form. What am I doing wrong?

procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption:='button';
end;

procedure TForm1.KeyPress(Sender: TObject; var Key: Char);
begin
if Key='a' then Label1.Caption:='OK';
end;

You need to set the Form's KeyPreview property to True before you can capture keyboard events.

Thanks...it's easy when you know how. I'm just started to teach myself...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.