Advertisement
TheNoobPolice

TNP's Repeater Class AHK v2

Feb 11th, 2023 (edited)
1,163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. repeater class by TheNoobPolice, AHK v2 version
  3.  
  4. Usage:
  5.  
  6. 1)  Save this script as repeater.ahk in Documents/Autohotkey/Lib and include it at the top of your script:
  7.        
  8.         #Include <repeater>
  9.  
  10. 2)  In your script, instantiate the class in the auto-execute section and name a callback function you want to repeat:
  11.    
  12.         MyObject := repeater(MyFunc)   
  13.        
  14. 3)  Create the named callback function anywhere in your script. e.g:
  15.    
  16.         MyFunc() {
  17.             static counter := 0
  18.             ++counter
  19.             Tooltip "My callback function has fired " counter " times!"
  20.         }
  21.        
  22. 4)  Call the repeater methods to fire the function any way you like. This example starts and stops on left mouse button:
  23.    
  24.         *~LButton::MyObject.Start
  25.         *~LButton Up::MyObject.Stop
  26.    
  27. 5)  You can change defaults on initialisation, or override them in each start() or toggle() call.
  28.     Options are: Repeat interval time (milliseconds), whether to call immediately (true/false), and max number of repeats. e.g:
  29.    
  30.         ; sets defaults to repeat "MyFunc" every 500ms, only after 500ms has first passed, for a max of 7 times
  31.         MyObject := repeater(MyFunc, 500, false, 7)
  32.        
  33.         ; override the above in this call only to 100ms repeats, starting immediately, with no repeat limit
  34.         RButton::MyObject.Start(100, true, 0)
  35.    
  36.     Any call of the same object with new values must first be halted by calling MyObject.Stop if already running
  37.     You can run multiple objects asynchronously, of course.
  38.    
  39. 6)  Example script of a simple auto-clicker on F1 key that will always place nice with any other code:
  40.    
  41.         #Include <repeater>
  42.         Clik := repeater(Clicker)
  43.         Return ; end auto-execute section
  44.        
  45.         *F1::Clik.Start(300) ; no need to worry about Windows key repeat on holding - the class filters it out for you!
  46.         *F1 Up::Clik.Stop
  47.        
  48.         Clicker(){
  49.             Click
  50.         }
  51.            
  52.         ^Esc::ExitApp
  53. */
  54. ; -------------------------------------- End of notes. Actual code below --------------------------------------------------------
  55.  
  56. class repeater {
  57.  
  58.     __New(callback, ms := 200, immediate := 1, iterations := 0) {
  59.         this.ms := ms
  60.         this.immediate := immediate
  61.         this.iterations := iterations
  62.         this.cb := callback
  63.         this.cfn := this.state := this.suppress := this.counts := 0
  64.     }
  65.    
  66.     Toggle(argTime := this.ms, argImmediate := this.immediate, argIterations := this.iterations) {
  67.    
  68.         (this.state ^= 1) ? this.Start(argTime, argImmediate, argIterations) : this.Stop()
  69.     }
  70.    
  71.     Start(argTime := this.ms, argImmediate := this.immediate, argIterations := this.iterations) {
  72.    
  73.         if (this.suppress)
  74.             return
  75.         this.suppress := 1
  76.        
  77.         if (argImmediate)
  78.             this.cb.call
  79.         SetTimer(this.cb, argTime)
  80.        
  81.         if (argIterations) {   
  82.             this.cfn := ObjBindMethod(this, "Counter", argIterations)
  83.            
  84.             if (argImmediate)
  85.                 this.cfn.call
  86.             SetTimer(this.cfn, argTime)
  87.         }  
  88.     }
  89.    
  90.     Stop() {
  91.         this.state := this.suppress := this.counts := 0
  92.  
  93.         SetTimer(this.cb, 0)
  94.        
  95.         if (this.cfn) {
  96.             SetTimer(this.cfn, 0)
  97.             this.cfn := 0
  98.         }
  99.     }
  100.    
  101.     Counter(repeats) {
  102.         ++this.counts
  103.  
  104.         if (this.counts >= repeats)
  105.             this.Stop()
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement