Главная страница | назад





Article #27707: How to close your application using the Escape key.

QUESTION:

How can I make my application close when the "Escape" key is pressed?

ANSWER:

Make sure that you set the KeyView property of the Form to True. This will make keyboard events occur on the form, before the active control. You should set this in the create event of the form. Below, you will find a code sample that demonstates how to do this.

procedure TForm1.FormCreate(Sender: TObject);
begin
Form1.KeyPreview := True;
end;
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
//The Escape key is #27.
if key = #27 then
Close;
end;

Last Modified: 16-SEP-01