Recent

Author Topic: How control keypress on process  (Read 1406 times)

eldonfsr

  • Hero Member
  • *****
  • Posts: 553
How control keypress on process
« on: May 29, 2025, 03:12:39 am »
Hi well sorry for question but i will tried to get control on sending file to serial, how i get if user push key escape to abort sending file some idea how i can do that...
Code: Pascal  [Select][+][-]
  1. procedure TFormMain.MISendfileClick(Sender: TObject);
  2. var filesent:Boolean;
  3.   i, tlines,incr,Coils: integer;
  4.   ltext: string;
  5.   VSPort:TlazSerial;
  6.   VTerm:TMemo;
  7.   VPBar:TProgressBar;
  8.   Sbar:TSTatusBar;
  9. begin
  10.     Sending:=true;
  11.     filesent:=false;
  12.     FProgress:=0;
  13.      VSPort:= TlazSerial.Create(nil);
  14.      VTerm:= TMemo.Create(nil);
  15.      Sbar:= TStatusbar.Create(nil);
  16.      case JvPageCtrl.ActivePageIndex of
  17.         0:begin  VSport:= LSp1;VTerm:=MtermSp1;Sbar1.Panels.Items[1].Style:= psOwnerDraw;SBar1.Update;Sbar:=Sbar1; Coils:=CoilsSp1 ; end;
  18.         1:begin  VSport:= LSp2;VTerm:=MtermSp2;Sbar2.Panels.Items[1].Style:= psOwnerDraw;SBar2.Update;Sbar:=Sbar2; Coils:=CoilsSp2 ; end;
  19.         2:begin  VSport:= LSp3;VTerm:=MtermSp3;Sbar3.Panels.Items[1].Style:= psOwnerDraw;SBar1.Update;Sbar:=Sbar3; Coils:=CoilsSp3 ; end;
  20.         3:begin  VSport:= LSp4;VTerm:=MtermSp4;Sbar4.Panels.Items[1].Style:= psOwnerDraw;SBar4.Update;Sbar:=Sbar4; Coils:=CoilsSp4 ; end;
  21.         4:begin  VSport:= LSp5;VTerm:=MtermSp5;Sbar5.Panels.Items[1].Style:= psOwnerDraw;SBar5.Update;Sbar:=Sbar5; Coils:=CoilsSp5 ; end;
  22.      end;
  23.  
  24.     if( (VSPort.Active)  ) then begin
  25.       Odf.Filter := 'Text files (*.txt)|*.TXT|Any file (*.*)|*.*';
  26.       if( (Coils=1) or (coils=0)) then begin
  27.            Odf.InitialDir:= GetCurrentDir+'\Progms\SingleCoil';
  28.       end else begin
  29.           Odf.InitialDir:= GetCurrentDir+'\Progms\MultipleCoil';
  30.       end;
  31.       if Odf.Execute then begin
  32.  
  33.         { First check if the file exists. }
  34.         if FileExists(Odf.FileName) then begin
  35.           { If it exists, load the data into the memo box. }
  36.  
  37.           VMTerminal.Lines.Clear;
  38.           FormMain.Update;
  39.           FileSent:=true;
  40.           VMTerminal.Lines.LoadFromFile(Odf.FileName);
  41.           tlines := VMTerminal.Lines.Count-1;
  42.           VSport.WriteData(chr(27)+LineEnding);
  43.           sleep(100);
  44.           VSPort.WriteData('RM'+LineEnding);
  45.           sleep(2000);
  46.           FormMain.SetFocus;
  47.           incr:= Round(100/tlines);
  48.           if( Assigned(VSport) ) then begin
  49.             for i := 1 to tlines  do begin  // start control process to send file
  50.               ltext := VMTerminal.Lines[i-1];
  51.               if( FProgress< 100) then begin
  52.                  FProgress:= incr*i;
  53.                  Sbar.Panels.Items[1].Text:= IntToStr(FProgress);
  54.               end;
  55.               Sbar.Refresh;
  56.               if ((ltext = 'rm') or (ltext = 'RM') or (ltext = 'MF') or (ltext = 'EM')) then  begin
  57.                 VSPort.WriteData(ltext+LineEnding);
  58.                 sleep(2000);
  59.               end else begin
  60.                 VSPort.WriteData(ltext+LineEnding);
  61.                 sleep(120);
  62.               end;
  63.              if Sending = false then break; // I create sending global to change false and break for
  64.  
  65.           end;
  66.           sleep(1000);
  67.         end else begin
  68.           { Otherwise, raise an exception. }
  69.           raise Exception.Create('File does not exist.');
  70.         end;
  71.       end else begin
  72.         ShowMessage('Not false seleted to transfer');
  73.       end;
  74.  
  75.     end;
  76.       ;
  77.     Sbar.Panels.Items[1].Style:= PsText;
  78.     Sbar.Panels.Items[1].text:='' ;
  79.     Sbar.Update;
  80.     case JvPageCtrl.ActivePageIndex of
  81.        0: begin  SBar1:= SBar;end;
  82.        1: begin  SBar2:= SBar;end;
  83.        2: begin  SBar3:= SBar;end;
  84.        3: begin  SBar4:= SBar;end;
  85.        4: begin  Sbar5:= SBar;end;
  86.     end;
  87.  
  88.     VTerm.SetFocus;
  89.   end;
  90. end;
  91.  
  92.  
 

regs

  • Jr. Member
  • **
  • Posts: 94
Re: How control keypress on process
« Reply #1 on: June 03, 2025, 06:59:07 pm »
As your Click method is located right in TFormMain, you can use TForm's OnKeyDown event. You need to switch on KeyPreview property for the form.

Then make some protected var in TFormMain, like FAborted. Set it to False in the beginning of MISendfileClick. And set it to True in OnKeyDown. Then check it in your loop.

procedure TFormMain.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if Key = VK_ESCAPE then
    FAborted := True;
end;

Include LCLType in uses for key constants.

But mind that the app will be locked while you using Sleep. So even if user presses Escape he would still have to wait. I never used TLazSerial, so don't know purpose though.

« Last Edit: June 03, 2025, 07:07:26 pm by regs »

Handoko

  • Hero Member
  • *****
  • Posts: 5485
  • My goal: build my own game engine using Lazarus
Re: How control keypress on process
« Reply #2 on: June 03, 2025, 07:33:26 pm »

CM630

  • Hero Member
  • *****
  • Posts: 1521
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: How control keypress on process
« Reply #3 on: June 03, 2025, 09:45:38 pm »
What is the purpose of the SLEEPs?
The solution proposed by @regs should works, but if it does not put
application.processmessages
before the Sleeps.
Лазар 4,2 32 bit (sometimes 64 bit); FPC3,2,2

RAW

  • Hero Member
  • *****
  • Posts: 871
Re: How control keypress on process
« Reply #4 on: June 03, 2025, 11:48:40 pm »
SLEEP ?
PROCESSMESSAGES ?

Really? Is that stable production code?

And I thought people used to say: "Change the structure..."

 

TinyPortal © 2005-2018