The first method:
Put a button on the form:
1>, set the Cancel property of the button to true;
2>, write in the Click event of the button:
Procedure Tform1.btn1click (Sender:tobject);
Begin
Close;
End
button's Cancel property:
This property determines whether the button is a Cancel button. When set to True, the Cancel button, which executes the button's OnClick event handler, is executed when the user presses the ESC key. When there are multiple cancel buttons in the form, only the OnClick event of the tab Order Property minimum button is called.
The second method:
1> Set the KeyPreview property of the form to: true;//can receive keyboard actions
2>, write in the keyboard event of the form:
Procedure tform1.formkeypress (Sender:tobject; var key:char);
Begin
If key = #27 Then
Application.terminate; Close Program
End
Delphi easy way to exit a program by pressing ESC shortcut key