|
| 1 | +package autoclicker |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "math/rand" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/go-vgo/robotgo" |
| 10 | +) |
| 11 | + |
| 12 | +var toggled bool = false |
| 13 | + |
| 14 | +// doClick simulates the mouse click |
| 15 | +func doClick(ratio float32, timespan int, mb string) { |
| 16 | + robotgo.MouseToggle("down", mb) |
| 17 | + time.Sleep(time.Duration(int(float32(timespan)*ratio)) * time.Millisecond) |
| 18 | + robotgo.MouseToggle("up", mb) |
| 19 | + time.Sleep(time.Duration(int(float32(timespan)*(1-ratio))) * time.Millisecond) |
| 20 | +} |
| 21 | + |
| 22 | +// doPress simulates a keyboard press |
| 23 | +func doPress(ratio float32, timespan int, key string) { |
| 24 | + robotgo.KeyTap(key) |
| 25 | + time.Sleep(time.Duration(int(float32(timespan)*ratio)) * time.Millisecond) |
| 26 | +} |
| 27 | + |
| 28 | +// startMouseClicker starts the clicking process |
| 29 | +// mb = (left|center|right) |
| 30 | +// lower = min click amount |
| 31 | +// upper = max click amount |
| 32 | +// timespan = how long for all clicks to complete (ms) |
| 33 | +// ratio = how long holding down mb and how long waiting for next click |
| 34 | +func startMouseClicker(lower, upper, ratio float32, timespan int, mb string) { |
| 35 | + toggled = true |
| 36 | +OUTER: |
| 37 | + for toggled { |
| 38 | + cps := int(lower + rand.Float32()*(upper-lower)) |
| 39 | + for i := 0; i < cps; i++ { |
| 40 | + if !toggled { |
| 41 | + break OUTER |
| 42 | + } |
| 43 | + doClick(ratio, timespan/cps, mb) |
| 44 | + } |
| 45 | + time.Sleep(time.Duration(int(1000-timespan)) * time.Millisecond) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// StartClicker starts either the mouse clicker or the keyboard clicker |
| 50 | +func StartClicker(lower, upper, ratio float32, timespan int, key string) { |
| 51 | + key = strings.TrimSpace(key) |
| 52 | + if key == "" { |
| 53 | + return |
| 54 | + } |
| 55 | + log.Println("starting clicker") |
| 56 | + if key == "left" || key == "right" || key == "center" { |
| 57 | + startMouseClicker(lower, upper, ratio, timespan, key) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + toggled = true |
| 62 | +OUTER: |
| 63 | + for toggled { |
| 64 | + cps := int(lower + rand.Float32()*(upper-lower)) |
| 65 | + for i := 0; i < cps; i++ { |
| 66 | + if !toggled { |
| 67 | + break OUTER |
| 68 | + } |
| 69 | + doPress(ratio, timespan/cps, key) |
| 70 | + } |
| 71 | + time.Sleep(time.Duration(int(1000-timespan)) * time.Millisecond) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// StopClicker stops the clicker |
| 76 | +func StopClicker() { |
| 77 | + if toggled { |
| 78 | + log.Println("Stopping clicker") |
| 79 | + } |
| 80 | + toggled = false |
| 81 | +} |
0 commit comments