1010interface
1111
1212uses
13- Classes, SysUtils,
13+ Classes, SysUtils, syncobjs,
1414 simba.base, simba.baseclass, simba.image, simba.image_utils, simba.externalcanvas,
1515 simba.target_eios, simba.target_window, simba.target_image, simba.target_plugin,
1616 simba.colormath, simba.dtm,
@@ -142,6 +142,8 @@ TSimbaTarget = class(TSimbaBaseClass)
142142 FCustomClientArea: TBox;
143143 FLastSize: TSize;
144144
145+ FInputLock: TCriticalSection;
146+
145147 function ValidateBounds (var ABounds: TBox): Boolean;
146148
147149 procedure ChangeTarget (Kind: ESimbaTargetKind);
@@ -433,16 +435,14 @@ constructor TSimbaTargetOptions.Create;
433435 Self.KeyPressMax := 125 ;
434436end ;
435437
436- function TSimbaTarget.MousePressed (Button: EMouseButton): Boolean;
437- begin
438- CheckMethod(FTargetMethods.MousePressed, ' MousePressed' );
439-
440- Result := FTargetMethods.MousePressed(FTarget, Button);
441- end ;
442-
443438procedure TSimbaTarget.MouseMove (Dest: TPoint);
444439begin
445- MoveMouseOnTarget(Self, Dest);
440+ FInputLock.Enter();
441+ try
442+ MoveMouseOnTarget(Self, Dest);
443+ finally
444+ FInputLock.Leave();
445+ end ;
446446end ;
447447
448448procedure TSimbaTarget.MouseMove (Box: TBox; ForcedMove: Boolean);
@@ -463,43 +463,117 @@ procedure TSimbaTarget.MouseClick(Button: EMouseButton);
463463begin
464464 Time := RandomLeft(FOptions.MousePressMin, FOptions.MousePressMax);
465465
466- MouseDown(Button);
467- SimbaNativeInterface.PreciseSleep(Time);
468- MouseUp(Button);
466+ FInputLock.Enter();
467+ try
468+ MouseDown(Button);
469+ SimbaNativeInterface.PreciseSleep(Time);
470+ MouseUp(Button);
471+ finally
472+ FInputLock.Leave();
473+ end ;
469474end ;
470475
471476procedure TSimbaTarget.MouseTeleport (P: TPoint);
472477begin
473478 CheckMethod(FTargetMethods.MouseTeleport, ' MouseTeleport' );
474479
475- FEventManager.CallTeleportEvent(Self, P.X, P.Y);
476- FTargetMethods.MouseTeleport(FTarget, P);
480+ FInputLock.Enter();
481+ try
482+ FEventManager.CallTeleportEvent(Self, P.X, P.Y);
483+ FTargetMethods.MouseTeleport(FTarget, P);
484+ finally
485+ FInputLock.Leave();
486+ end ;
477487end ;
478488
479489procedure TSimbaTarget.MouseDown (Button: EMouseButton);
480490begin
481491 CheckMethod(FTargetMethods.MouseDown, ' MouseDown' );
482492 CheckAutoFocus();
483493
484- FEventManager.CallMouseButtonEvent(Self, Button, True);
485- FTargetMethods.MouseDown(FTarget, Button);
494+ FInputLock.Enter();
495+ try
496+ FEventManager.CallMouseButtonEvent(Self, Button, True);
497+ FTargetMethods.MouseDown(FTarget, Button);
498+ finally
499+ FInputLock.Leave();
500+ end ;
486501end ;
487502
488503procedure TSimbaTarget.MouseUp (Button: EMouseButton);
489504begin
490505 CheckMethod(FTargetMethods.MouseUp, ' MouseUp' );
491506 CheckAutoFocus();
492507
493- FEventManager.CallMouseButtonEvent(Self, Button, False);
494- FTargetMethods.MouseUp(FTarget, Button);
508+ FInputLock.Enter();
509+ try
510+ FEventManager.CallMouseButtonEvent(Self, Button, False);
511+ FTargetMethods.MouseUp(FTarget, Button);
512+ finally
513+ FInputLock.Leave();
514+ end ;
495515end ;
496516
497517procedure TSimbaTarget.MouseScroll (Scrolls: Integer);
498518begin
499519 CheckMethod(FTargetMethods.MouseScroll, ' MouseScroll' );
500520 CheckAutoFocus();
501521
502- FTargetMethods.MouseScroll(FTarget, Scrolls);
522+ FInputLock.Enter();
523+ try
524+ FTargetMethods.MouseScroll(FTarget, Scrolls);
525+ finally
526+ FInputLock.Leave();
527+ end ;
528+ end ;
529+
530+ function TSimbaTarget.MousePressed (Button: EMouseButton): Boolean;
531+ begin
532+ CheckMethod(FTargetMethods.MousePressed, ' MousePressed' );
533+
534+ FInputLock.Enter();
535+ try
536+ Result := FTargetMethods.MousePressed(FTarget, Button);
537+ finally
538+ FInputLock.Leave();
539+ end ;
540+ end ;
541+
542+ function TSimbaTarget.GetMouseX : Integer;
543+ begin
544+ Result := MouseXY.X;
545+ end ;
546+
547+ function TSimbaTarget.GetMouseXY : TPoint;
548+ begin
549+ CheckMethod(FTargetMethods.MousePosition, ' MousePosition' );
550+
551+ FInputLock.Enter();
552+ try
553+ Result := FTargetMethods.MousePosition(FTarget);
554+ finally
555+ FInputLock.Leave();
556+ end ;
557+ end ;
558+
559+ function TSimbaTarget.GetMouseY : Integer;
560+ begin
561+ Result := MouseXY.Y;
562+ end ;
563+
564+ procedure TSimbaTarget.SetMouseX (Value : Integer);
565+ begin
566+ MouseTeleport(TPoint.Create(Value , MouseY));
567+ end ;
568+
569+ procedure TSimbaTarget.SetMouseY (Value : Integer);
570+ begin
571+ MouseTeleport(TPoint.Create(MouseX, Value ));
572+ end ;
573+
574+ procedure TSimbaTarget.SetMouseXY (Value : TPoint);
575+ begin
576+ MouseTeleport(Value );
503577end ;
504578
505579procedure TSimbaTarget.KeySend (Text: String);
@@ -509,14 +583,17 @@ procedure TSimbaTarget.KeySend(Text: String);
509583begin
510584 CheckMethod(FTargetMethods.KeySend, ' KeySend' );
511585 CheckAutoFocus();
586+ if (Length(Text) = 0 ) then
587+ Exit;
588+ SetLength(SleepTimes, Length(Text) * 4 );
589+ for I := 0 to High(SleepTimes) do
590+ SleepTimes[I] := RandomLeft(FOptions.KeyPressMin, FOptions.KeyPressMax);
512591
513- if (Length(Text) > 0 ) then
514- begin
515- SetLength(SleepTimes, Length(Text) * 4 );
516- for I := 0 to High(SleepTimes) do
517- SleepTimes[I] := RandomLeft(FOptions.KeyPressMin, FOptions.KeyPressMax);
518-
592+ FInputLock.Enter();
593+ try
519594 FTargetMethods.KeySend(FTarget, PChar(Text), Length(Text), @SleepTimes[0 ]);
595+ finally
596+ FInputLock.Leave();
520597 end ;
521598end ;
522599
@@ -526,32 +603,52 @@ procedure TSimbaTarget.KeyPress(Key: EKeyCode);
526603begin
527604 Time := RandomLeft(FOptions.KeyPressMin, FOptions.KeyPressMax);
528605
529- KeyDown(Key);
530- SimbaNativeInterface.PreciseSleep(Time);
531- KeyUp(Key);
606+ FInputLock.Enter();
607+ try
608+ KeyDown(Key);
609+ SimbaNativeInterface.PreciseSleep(Time);
610+ KeyUp(Key);
611+ finally
612+ FInputLock.Leave();
613+ end ;
532614end ;
533615
534616procedure TSimbaTarget.KeyDown (Key: EKeyCode);
535617begin
536618 CheckMethod(FTargetMethods.KeyDown, ' KeyDown' );
537619 CheckAutoFocus();
538620
539- FTargetMethods.KeyDown(FTarget, Key);
621+ FInputLock.Enter();
622+ try
623+ FTargetMethods.KeyDown(FTarget, Key);
624+ finally
625+ FInputLock.Leave();
626+ end ;
540627end ;
541628
542629procedure TSimbaTarget.KeyUp (Key: EKeyCode);
543630begin
544631 CheckMethod(FTargetMethods.KeyUp, ' KeyUp' );
545632 CheckAutoFocus();
546633
547- FTargetMethods.KeyUp(FTarget, Key);
634+ FInputLock.Enter();
635+ try
636+ FTargetMethods.KeyUp(FTarget, Key);
637+ finally
638+ FInputLock.Leave();
639+ end ;
548640end ;
549641
550642function TSimbaTarget.KeyPressed (Key: EKeyCode): Boolean;
551643begin
552644 CheckMethod(FTargetMethods.KeyPressed, ' KeyPressed' );
553645
554- Result := FTargetMethods.KeyPressed(FTarget, Key);
646+ FInputLock.Enter();
647+ try
648+ Result := FTargetMethods.KeyPressed(FTarget, Key);
649+ finally
650+ FInputLock.Leave();
651+ end ;
555652end ;
556653
557654function TSimbaTarget.KeyCodeFromChar (C: Char): EKeyCode;
@@ -748,6 +845,7 @@ constructor TSimbaTarget.Create;
748845
749846 FEventManager := TSimbaTargetEventManager.Create();
750847 FOptions := TSimbaTargetOptions.Create();
848+ FInputLock := TCriticalSection.Create();
751849
752850 SetDesktop();
753851end ;
@@ -759,6 +857,8 @@ destructor TSimbaTarget.Destroy;
759857 FreeAndNil(FEventManager);
760858 if (FOptions <> nil ) then
761859 FreeAndNil(FOptions);
860+ if (FInputLock <> nil ) then
861+ FreeAndNil(FInputLock);
762862
763863 inherited Destroy();
764864end ;
@@ -788,38 +888,6 @@ procedure TSimbaTarget.CheckResize(NewSize: TSize);
788888 FEventManager.CallResizeEvent(Self, NewSize.Width, NewSize.Height);
789889end ;
790890
791- function TSimbaTarget.GetMouseX : Integer;
792- begin
793- Result := MouseXY.X;
794- end ;
795-
796- function TSimbaTarget.GetMouseXY : TPoint;
797- begin
798- CheckMethod(FTargetMethods.MousePosition, ' MousePosition' );
799-
800- Result := FTargetMethods.MousePosition(FTarget);
801- end ;
802-
803- function TSimbaTarget.GetMouseY : Integer;
804- begin
805- Result := MouseXY.Y;
806- end ;
807-
808- procedure TSimbaTarget.SetMouseX (Value : Integer);
809- begin
810- MouseTeleport(TPoint.Create(Value , MouseY));
811- end ;
812-
813- procedure TSimbaTarget.SetMouseY (Value : Integer);
814- begin
815- MouseTeleport(TPoint.Create(MouseX, Value ));
816- end ;
817-
818- procedure TSimbaTarget.SetMouseXY (Value : TPoint);
819- begin
820- MouseTeleport(Value );
821- end ;
822-
823891function TSimbaTarget.AddEvent (Event: ETargetEvent; Method: TSimbaTargetEvent; UserData: Pointer; UserDataSize: Integer): Integer;
824892begin
825893 Result := FEventManager.Add(Event, Method, UserData, UserDataSize);
0 commit comments