将窗体的KeyPreview属性设为True
窗体上放一个Label,一个Button
var
Form1 : TForm1;
//[begin code]
implementation
var
vl_Continue : Boolean;
{$R *.DFM}
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = VK_ESCAPE then
vl_Continue := False;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i : Integer;
begin
vl_Continue := True;
for I := 0 to 10000 do
begin
Application.ProcessMessages;
if not vl_Continue then
Break;
Label1.Caption := IntToStr(I);
end;
end;
//[end code]