changed mouse moving logic
previously it would see if user is active each 10 seconds, and once in 5 minutes, it would move mouse if user wasn't active. It caused issues because if user wasn't active before 5 minute ticker, then mouse would still move. Now gover counts for how long user has been idle, and moves mouse only when time exceeds preset value in seconds
This commit is contained in:
63
main.go
63
main.go
@@ -7,53 +7,68 @@ import (
|
|||||||
"github.com/go-vgo/robotgo"
|
"github.com/go-vgo/robotgo"
|
||||||
)
|
)
|
||||||
|
|
||||||
func checkActive(x *int, y *int, active *bool) {
|
const (
|
||||||
|
CHECKING_INTERVAL_SECONDS = 10
|
||||||
|
MOVE_AFTER_SECONDS = 300
|
||||||
|
)
|
||||||
|
|
||||||
|
func moveMouse() {
|
||||||
|
// Move mouse around 100px
|
||||||
|
robotgo.MoveSmoothRelative(100, 0)
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
robotgo.MoveSmoothRelative(-100, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func displayStatus(lastMove *int) {
|
||||||
|
// Clear display, and write last move
|
||||||
|
fmt.Print("\033[H\033[2J")
|
||||||
|
fmt.Printf("Last move %d seconds ago\n", *lastMove)
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkActive(x *int, y *int, lastMove *int) {
|
||||||
// Current x, and Current Y
|
// Current x, and Current Y
|
||||||
cX, cY := robotgo.Location()
|
cX, cY := robotgo.Location()
|
||||||
|
|
||||||
if cX == *x && cY == *y {
|
if cX == *x && cY == *y {
|
||||||
*active = false
|
// Hasn't moved
|
||||||
|
*lastMove += CHECKING_INTERVAL_SECONDS
|
||||||
} else {
|
} else {
|
||||||
*active = true
|
// Has moved
|
||||||
|
*lastMove = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set current x and y
|
||||||
*x = cX
|
*x = cX
|
||||||
*y = cY
|
*y = cY
|
||||||
}
|
|
||||||
|
|
||||||
func moveMouse(active *bool) {
|
// Check if we should move
|
||||||
// If inactive, move mouse to 100px right, and back
|
if *lastMove >= MOVE_AFTER_SECONDS {
|
||||||
if !*active {
|
moveMouse()
|
||||||
fmt.Println("Moving mouse")
|
*lastMove = 0
|
||||||
robotgo.MoveSmoothRelative(100, 0)
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
robotgo.MoveSmoothRelative(-100, 0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
displayStatus(lastMove)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// reactive variables haha (useState, or reactive(), but just pointers)
|
// reactive variables haha (useState, or reactive(), but just pointers)
|
||||||
x := 0
|
x := 0
|
||||||
y := 0
|
y := 0
|
||||||
active := true
|
// Meant to represent seconds ago
|
||||||
|
lastMove := 0
|
||||||
|
|
||||||
// Create tickers
|
// Create tickers
|
||||||
ticker1 := time.NewTicker(10 * time.Second)
|
ticker := time.NewTicker(CHECKING_INTERVAL_SECONDS * time.Second)
|
||||||
ticker2 := time.NewTicker(5 * time.Minute)
|
fmt.Printf("Checking active every %d seconds", CHECKING_INTERVAL_SECONDS)
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
defer ticker1.Stop()
|
defer ticker.Stop()
|
||||||
defer ticker2.Stop()
|
|
||||||
|
|
||||||
// Run initial calls (optional)
|
// Run initial calls (optional)
|
||||||
checkActive(&x, &y, &active)
|
checkActive(&x, &y, &lastMove)
|
||||||
|
|
||||||
// Goroutine to handle ticks
|
// Goroutine to handle ticks
|
||||||
for {
|
for range ticker.C {
|
||||||
select {
|
checkActive(&x, &y, &lastMove)
|
||||||
case <-ticker1.C:
|
|
||||||
checkActive(&x, &y, &active)
|
|
||||||
case <-ticker2.C:
|
|
||||||
moveMouse(&active)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user