Recent

Author Topic: KeyPress  (Read 4886 times)

eoneuk

  • New member
  • *
  • Posts: 7
KeyPress
« on: December 31, 2014, 01:27:17 pm »
I can't seem to find a way to use a key press or button2 click to break out of a loop in the following code example.
If it is possible, any help will be appreciated.

Code: [Select]

procedure TForm1Button1Click(Sender:TObject);

begin

while (xxxxx) do
      begin
// code
// at this point detect key press or  button click to break out of loop before it is finished
      end;

end;

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: KeyPress
« Reply #1 on: December 31, 2014, 01:54:50 pm »
declare (private section):
  doNext : Boolean and set = True//eg on btnClick;
also you need a button to set this doNext:= False;
then you can something like...
begin
while(...) do
begin
  if doNext then
    what_you_want
  else
    Exit;
« Last Edit: December 31, 2014, 01:59:35 pm by exdatis »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: KeyPress
« Reply #2 on: December 31, 2014, 01:58:36 pm »
This works:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  writeln('BL');
  while not (ssCtrl in GetKeyShiftState) do
    begin
      //your code
      Application.ProcessMessages;
    end;
  writeln('EL');
end;     
CTRL breaks the loop.
However, it is not a nice code. This is the case where you should use thread definitely.
http://wiki.freepascal.org/Multithreaded_Application_Tutorial
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: KeyPress
« Reply #3 on: December 31, 2014, 04:05:50 pm »
Once I did something like this:

Code: [Select]
procedure TForm1.CancelButtonClick(Sender: TObject);
begin
  FCanceled := TRUE;
end; 

procedure TForm1.Button1Click(Sender: TObject);
begin
  writeln('BL');
  FCanceled := FALSE;
  while not FCanceled do
    begin
      //your code
      Application.ProcessMessages;
    end;
  writeln('EL');
end; 

And nothing stops you to use the TForm.OnKeyDown event. You must set TForm.KeyPreview to true.

you can go further and do something like this:
Code: [Select]
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if FIsWorking then begin
    case key of
      VK_CANCEL: FCanceled := TRUE; // Cancel job only Escape key is pressed (you must add lcl_types to the uses clause in order to use VK_* constants).
    end;
  end;
end; 

procedure TForm1.Button1Click(Sender: TObject);
begin
  writeln('BL');
  FCanceled := FALSE;
  FIsWorking := TRUE;
  try
    while not FCanceled do
      begin
        //your code
        Application.ProcessMessages;
      end;
    writeln('EL');
  finally
    FIsWorking := FALSE;
  end;
end; 

A thread will produce a smooth, fluent and responsive interface use when the working loop is long and heavy.
But if thats not the case you can do what I posted.

eoneuk

  • New member
  • *
  • Posts: 7
Re: KeyPress
« Reply #4 on: December 31, 2014, 05:58:49 pm »
I thank all of you for the prompt and helpful responses.

Ed

 

TinyPortal © 2005-2018