Advertisement
TheNoobPolice

TNP's Repeater Class AHK v1.1

Feb 11th, 2023 (edited)
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. repeater class by TheNoobPolice, AHK v1.1 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 := new 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 := new repeater("MyFunc", 500, false, 7)
  32.        
  33.         ; override options 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 := new repeater("Clicker")
  43.         SetBatchlines -1
  44.         Return ; end auto-execute section
  45.        
  46.         *F1::Clik.Start(300) ; no need to worry about Windows key repeat on holding - the class filters it out for you!
  47.         *F1 Up::Clik.Stop()
  48.        
  49.         Clicker(){
  50.             Click
  51.         }
  52.            
  53.         ^Esc::ExitApp
  54.  
  55. */
  56. ; -------------------------------------- End of notes. Actual code below --------------------------------------------------------
  57.  
  58. class repeater {
  59.  
  60.     __New(callback, ms := 200, immediate := 1, iterations := 0) {
  61.         this.ms := ms
  62.         this.immediate := immediate
  63.         this.iterations := iterations
  64.         this.cb := Func(callback)
  65.         this.cfn := this.state := this.suppress := this.counts := 0
  66.     }
  67.    
  68.     Toggle(argTime := 0, argImmediate := -1, argIterations := -1) {
  69.    
  70.         (this.state := !this.state) ? this.Start(argTime, argImmediate, argIterations) : this.Stop()
  71.     }
  72.    
  73.     Start(argTime := 0, argImmediate := -1, argIterations := -1) {
  74.    
  75.         if (this.suppress)
  76.             return
  77.         this.suppress := 1, fn := this.cb
  78.        
  79.         if (argImmediate == 1 || (argImmediate == -1 && this.immediate))
  80.             fn.call()
  81.         SetTimer % fn, % argTime ? argTime : this.ms
  82.        
  83.         if (argIterations > 0 || (argIterations == -1 && this.iterations)) {   
  84.             this.cfn := ObjBindMethod(this, "Counter", argIterations > 0 ? argIterations : this.iterations)
  85.             cf := this.cfn
  86.             if (argImmediate == 1 || (argImmediate == -1 && this.immediate))
  87.                 cf.call()
  88.             SetTimer % cf, % argTime ? argTime : this.ms
  89.         }  
  90.     }
  91.    
  92.     Stop() {
  93.         this.state := this.suppress := this.counts := 0, fn := this.cb, cf := this.cfn
  94.  
  95.         SetTimer % fn, off
  96.        
  97.         if (cf) {
  98.             SetTimer % cf, off
  99.             this.cfn := 0
  100.         }
  101.     }
  102.    
  103.     Counter(repeats) {
  104.         ++this.counts
  105.  
  106.         if (this.counts >= repeats)
  107.             this.Stop()
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement