commit 3daf896c84b5d5f4f8b194debd2278b9943c10e1 Author: Prashant Gupta Date: Thu Mar 7 18:13:00 2019 -0800 initial commit diff --git a/misc.go b/misc.go new file mode 100644 index 0000000..8f6f9de --- /dev/null +++ b/misc.go @@ -0,0 +1,109 @@ +package main + +import ( + "fmt" + "time" + + "github.com/go-vgo/robotgo" +) + +// func isPointerIdle(comm chan bool) { +// for { +// x1, y1 := robotgo.GetMousePos() +// time.Sleep(time.Second * 3) +// x2, y2 := robotgo.GetMousePos() + +// if x1 == x2 && y1 == y2 { +// fmt.Println("idle") +// //comm <- true +// } else { +// fmt.Println("moving") +// comm <- false +// } +// } +// } + +func checkIfMouseMoved(x1, y1, x2, y2 int, comm chan bool) { + if x1 == x2 && y1 == y2 { + fmt.Println("idle") + //return false + //comm <- true + } else { + fmt.Println("moving") + comm <- false + //return true + } +} + +func isMouseClick(comm chan bool) { + +} + +func moveMouse(comm chan bool) { + ticker := time.NewTicker(time.Second * 3) + val := true + movePixel := 10 + x1, y1 := robotgo.GetMousePos() + for { + select { + case <-ticker.C: + fmt.Println("ticked") + x2, y2 := robotgo.GetMousePos() + checkIfMouseMoved(x1, y1, x2, y2, comm) + if val { + fmt.Println("moving mouse because idle") + //x1, y1 := robotgo.GetMousePos() + robotgo.Move(x2+movePixel, y2+movePixel) + movePixel *= -1 + } else { + val = true + } + x1 = x2 + y1 = y2 + case val = <-comm: + fmt.Println("val received: ", val) + } + } +} +func main() { + // log.SetOutput(os.Stdout) + // log.Println("starting") + // log.Println("logging") + // //robotgo.ScrollMouse(100, "up") + // robotgo.Move(100, 100) + // x, y := robotgo.GetMousePos() + // fmt.Println("pos: ", x, y) + + comm := make(chan bool) + moveMouse(comm) + //isPointerIdle(comm) + + // for { + // // wheelDown := robotgo.AddEvent("wheelDown") + // // wheelRight := robotgo.AddEvent("wheelRight") + + // // fmt.Println("wheelDown : ", wheelDown) + // // fmt.Println("wheelRight : ", wheelRight) + // count := 0 + // go func(count *int) { + // for { + // mleft := robotgo.AddEvent("mleft") + // if mleft == 0 { + // *count++ + // fmt.Println("mleft : ", *count) + // time.Sleep(time.Millisecond * 500) + // } + // } + + // }(&count) + + // mright := robotgo.AddEvent("mright") + + // fmt.Println("mright : ", mright) + + // // if mleft { + // // fmt.Println("you press... ", "mouse left button") + // // } + // } + +} diff --git a/src/mouseMover.go b/src/mouseMover.go new file mode 100644 index 0000000..d7f831f --- /dev/null +++ b/src/mouseMover.go @@ -0,0 +1,87 @@ +package main + +import ( + "fmt" + "time" + + "github.com/go-vgo/robotgo" +) + +const ( + timeToCheck = 3 +) + +type mousePos struct { + mouseX int + mouseY int +} + +func moveMouse(comm chan struct{}, quit chan struct{}) { + ticker := time.NewTicker(time.Second * timeToCheck) + isIdle := true + movePixel := 10 + lastMousePos := getMousePos() + for { + select { + case <-ticker.C: + fmt.Println("ticked : ", isIdle) + currentMousePos := getMousePos() + if isIdle && isPointerIdle(currentMousePos, lastMousePos) { + fmt.Println("moving mouse") + nextMouseMov := &mousePos{ + mouseX: currentMousePos.mouseX + movePixel, + mouseY: currentMousePos.mouseY + movePixel, + } + robotgo.Move(nextMouseMov.mouseX, nextMouseMov.mouseY) + lastMousePos = nextMouseMov + } else { + lastMousePos = currentMousePos + } + isIdle = true + movePixel *= -1 + case <-comm: + isIdle = false + fmt.Println("val received: ", isIdle) + case <-quit: + return + } + } +} + +func isPointerIdle(currentMousePos, lastMousePos *mousePos) bool { + fmt.Println("current : ", currentMousePos) + fmt.Println("last : ", lastMousePos) + + if currentMousePos.mouseX == lastMousePos.mouseX && + currentMousePos.mouseY == lastMousePos.mouseY { + return true + } + return false +} + +func getMousePos() *mousePos { + x, y := robotgo.GetMousePos() + return &mousePos{ + mouseX: x, + mouseY: y, + } +} + +func isMouseClicked(comm chan struct{}) { + go func() { + for { + mleft := robotgo.AddEvent("mleft") + if mleft == 0 { + //fmt.Println("mleft clicked") + comm <- struct{}{} + } + } + }() +} + +func main() { + comm := make(chan struct{}) + quit := make(chan struct{}) + go isMouseClicked(comm) + moveMouse(comm, quit) +}