Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- repeater class by TheNoobPolice, AHK v1.1 version
- Usage:
- 1) Save this script as repeater.ahk in Documents/Autohotkey/Lib and include it at the top of your script:
- #Include <repeater>
- 2) In your script, instantiate the class in the auto-execute section and name a callback function you want to repeat:
- MyObject := new repeater("MyFunc")
- 3) Create the named callback function anywhere in your script. e.g:
- MyFunc() {
- static counter := 0
- ++counter
- Tooltip, % "My callback function has fired " counter " times!"
- }
- 4) Call the repeater methods to fire the function any way you like. This example starts and stops on left mouse button:
- *~LButton::MyObject.Start()
- *~LButton Up::MyObject.Stop()
- 5) You can change defaults on initialisation, or override them in each start() or toggle() call.
- Options are: Repeat interval time (milliseconds), whether to call immediately (true/false), and max number of repeats. e.g:
- ; sets defaults to repeat "MyFunc" every 500ms, only after 500ms has first passed, for a max of 7 times
- MyObject := new repeater("MyFunc", 500, false, 7)
- ; override options in this call only to 100ms repeats, starting immediately, with no repeat limit
- RButton::MyObject.Start(100, true, 0)
- Any call of the same object with new values must first be halted by calling MyObject.Stop() if already running
- You can run multiple objects asynchronously, of course.
- 6) Example script of a simple auto-clicker on F1 key that will always place nice with any other code:
- #Include <repeater>
- Clik := new repeater("Clicker")
- SetBatchlines -1
- Return ; end auto-execute section
- *F1::Clik.Start(300) ; no need to worry about Windows key repeat on holding - the class filters it out for you!
- *F1 Up::Clik.Stop()
- Clicker(){
- Click
- }
- ^Esc::ExitApp
- */
- ; -------------------------------------- End of notes. Actual code below --------------------------------------------------------
- class repeater {
- __New(callback, ms := 200, immediate := 1, iterations := 0) {
- this.ms := ms
- this.immediate := immediate
- this.iterations := iterations
- this.cb := Func(callback)
- this.cfn := this.state := this.suppress := this.counts := 0
- }
- Toggle(argTime := 0, argImmediate := -1, argIterations := -1) {
- (this.state := !this.state) ? this.Start(argTime, argImmediate, argIterations) : this.Stop()
- }
- Start(argTime := 0, argImmediate := -1, argIterations := -1) {
- if (this.suppress)
- return
- this.suppress := 1, fn := this.cb
- if (argImmediate == 1 || (argImmediate == -1 && this.immediate))
- fn.call()
- SetTimer % fn, % argTime ? argTime : this.ms
- if (argIterations > 0 || (argIterations == -1 && this.iterations)) {
- this.cfn := ObjBindMethod(this, "Counter", argIterations > 0 ? argIterations : this.iterations)
- cf := this.cfn
- if (argImmediate == 1 || (argImmediate == -1 && this.immediate))
- cf.call()
- SetTimer % cf, % argTime ? argTime : this.ms
- }
- }
- Stop() {
- this.state := this.suppress := this.counts := 0, fn := this.cb, cf := this.cfn
- SetTimer % fn, off
- if (cf) {
- SetTimer % cf, off
- this.cfn := 0
- }
- }
- Counter(repeats) {
- ++this.counts
- if (this.counts >= repeats)
- this.Stop()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement