1. Home
  2. Computing & Technology
  3. Delphi Programming

Abort a Delphi For/While/Repeat Loop by Pressing a Key

By , About.com Guide

It is a question that I see many times. There is an option to use a global boolean variable set from an OnClick event. Another one, used here, consists on reading the state of the key in the loop, to know if it is or not pressed...

var
  j : integer;
begin
  for j:=0 to 9999999 do
  begin
   Label1.Caption := IntToStr(j) ;
   {.... loop main body here ...}
   Application.ProcessMessages;

   {ESC key stops the loop}
   if GetKeyState(VK_Escape) AND 128 = 128 then break;
  end;
end;
Delphi tips navigator:
» How to "HotTrack" any control
« Scrolling Memo text from code
Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2000 Delphi Tips
  7. Abort a Delphi For/While/Repeat Loop by Pressing a Key

©2009 About.com, a part of The New York Times Company.

All rights reserved.