@@ -5,12 +5,15 @@ import { Point } from "./point.class";
55
66export class Mouse {
77 public config = {
8- autoDelayMs : 0 ,
8+ autoDelayMs : 100 ,
99 mouseSpeed : 1000 ,
1010 } ;
1111
12+ private lastAction : number ;
13+
1214 constructor ( private native : NativeAdapter ) {
13- this . native . setMouseDelay ( this . config . autoDelayMs ) ;
15+ this . native . setMouseDelay ( 0 ) ;
16+ this . lastAction = Date . now ( ) ;
1417 }
1518
1619 public setPosition ( target : Point ) : Mouse {
@@ -27,50 +30,72 @@ export class Mouse {
2730 for ( let idx = 0 ; idx < path . length ; ++ idx ) {
2831 const node = path [ idx ] ;
2932 const minTime = timeSteps [ idx ] ;
30- const previous = Date . now ( ) ;
31- let current = Date . now ( ) ;
32- while ( current - previous < minTime ) {
33- current = Date . now ( ) ;
34- }
33+ this . waitForNextTick ( minTime ) ;
3534 this . native . setMousePosition ( node ) ;
35+ this . updateTick ( ) ;
3636 }
3737 return this ;
3838 }
3939
4040 public leftClick ( ) : Mouse {
41+ this . waitForNextTick ( this . config . autoDelayMs ) ;
4142 this . native . leftClick ( ) ;
43+ this . updateTick ( ) ;
4244 return this ;
4345 }
4446
4547 public rightClick ( ) : Mouse {
48+ this . waitForNextTick ( this . config . autoDelayMs ) ;
4649 this . native . rightClick ( ) ;
50+ this . updateTick ( ) ;
4751 return this ;
4852 }
4953
5054 public scrollDown ( amount : number ) : Mouse {
55+ this . waitForNextTick ( this . config . autoDelayMs ) ;
5156 this . native . scrollDown ( amount ) ;
57+ this . updateTick ( ) ;
5258 return this ;
5359 }
5460
5561 public scrollUp ( amount : number ) : Mouse {
62+ this . waitForNextTick ( this . config . autoDelayMs ) ;
5663 this . native . scrollUp ( amount ) ;
64+ this . updateTick ( ) ;
5765 return this ;
5866 }
5967
6068 public scrollLeft ( amount : number ) : Mouse {
69+ this . waitForNextTick ( this . config . autoDelayMs ) ;
6170 this . native . scrollLeft ( amount ) ;
71+ this . updateTick ( ) ;
6272 return this ;
6373 }
6474
6575 public scrollRight ( amount : number ) : Mouse {
76+ this . waitForNextTick ( this . config . autoDelayMs ) ;
6677 this . native . scrollRight ( amount ) ;
78+ this . updateTick ( ) ;
6779 return this ;
6880 }
6981
7082 public drag ( path : Point [ ] ) : Mouse {
83+ this . waitForNextTick ( this . config . autoDelayMs ) ;
7184 this . native . pressButton ( Button . LEFT ) ;
7285 this . move ( path ) ;
7386 this . native . releaseButton ( Button . LEFT ) ;
87+ this . updateTick ( ) ;
7488 return this ;
7589 }
90+
91+ private updateTick ( ) {
92+ this . lastAction = Date . now ( ) ;
93+ }
94+
95+ private waitForNextTick ( minTime : number ) {
96+ let current = Date . now ( ) ;
97+ while ( current - this . lastAction < minTime ) {
98+ current = Date . now ( ) ;
99+ }
100+ }
76101}
0 commit comments