delphi prevent click fast

drama22
drama22 used Ask the Experts™
on
i have this function that detect fast click

  function IsFlood:Boolean;
  var
  i, x : integer;
  begin
    Result := False;
    i := trunc(GetTickCount/2);
    x := trunc((i - LastFloodTime)/4);
    Result := False;
    if x < 180 then Result := True;
    LastFloodTime := i;
  end;

Open in new window


this function uses GetTickCount i want to use Tstopwatch instead any one can convert this function to Tstopwatch ?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Oracle dba
Top Expert 2009
Commented:
why all the math ?
is it because of the integers instead of int64 ?

i := GetTickCount;
x := i - LastFloodTime;

x < (180 * 4 * 2 )
or x < 1440

i'd just go for a check on the milliseconds elapsed
this will be the same on all systems ...

type
 TForm1 = class(TForm)
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    fSW: TStopwatch;
    function IsFlood: Boolean;
  public
    constructor Create(AOwner: TComponent); override;
  end;

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fSW := TStopwatch.Create;
end;

function TForm1.IsFlood: Boolean;
begin
  Result := False;
  if not fSW.IsRunning then
  begin
    fSW.Start;
  end
    else
  begin
    if fSW.ElapsedMilliseconds <= 250 then
      Result := True;
    fSW.Stop;
    fSW.Start;
  end;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (Button = mbLeft) and IsFlood then
   ShowMessage('Boo !');
end;

Open in new window


if you doubleclick within 250ms it will show 'Boo !'

Author

Commented:
i dont know if you test this code but its working only one time if i click too many times and very fast it does not detect the fast clicks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial