954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

OnKeyPress Event

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;

docdoc
Newbie Poster
8 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

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

Terry Robinson
Light Poster
27 posts since Apr 2007
Reputation Points: 12
Solved Threads: 1
 

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

docdoc
Newbie Poster
8 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You