Reputation: 11
I'm a beginner in Go and I want to solve a small task: to print the current coordinates of the mouse cursor to the console and update them if the mouse moves. For this, I took the library robotgo, downloaded it using "go get github.com/go-vgo/robotgo", then "go get -u github.com/go-vgo/robotgo", and imported it using import "github.com/go-vgo/robotgo". Here's the code I wrote:
package main
import (
"fmt"
"github.com/go-vgo/robotgo"
)
func main() {
for {
x, y := robotgo.GetMousePos()
fmt.Printf("Current position X=%d, Y=%d", x, y)
if x == 540 && y == 960 {
fmt.Println("Cursor @center")
}
robotgo.Move(x, y)
}
}
But I'm getting errors:
# github.com/robotn/gohook
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:51:10: undefined: addEvent
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:62:7: undefined: Start
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:75:18: undefined: KeyHold
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:83:18: undefined: KeyUp
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:95:22: undefined: KeyUp
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:96:4: undefined: End
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:113:7: undefined: Start
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:121:17: undefined: MouseMove
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:128:22: undefined: MouseDown
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:129:4: undefined: End
C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\[email protected]\event.go:129:4: too many errors
UPD 16.03.2023 problem was solved by completely removing the library and its dependencies, and then reinstalling it.
Upvotes: 1
Views: 1569
Reputation: 29
I had this exact problem so I made a library to fix that since I needed a cross-platform solution https://github.com/JasonLovesDoggo/displayindex
It uses compile time build checks (//go:build
) along with CGO to interface with native system APIs
each platform is unique but for example, on windows you can get the cursor position with the following code
func getCursorPosition() (x, y int, err error) {
user32 := syscall.NewLazyDLL("user32.dll")
getCursorPos := user32.NewProc("GetCursorPos")
pt := POINT{}
ret, _, _ := getCursorPos.Call(uintptr(unsafe.Pointer(&pt)))
if ret == 0 {
return 0, 0, fmt.Errorf("GetCursorPos failed")
}
return int(pt.X), int(pt.Y), nil
}
On linux (x11), you can call the X11 C library then call the XQueryPointer
method and on darwin, you can use the CoreGraphics
Framework
Upvotes: 1
Reputation: 8078
For windows, use the system API :GetCursorPos
//go:build windows
package main
import (
"fmt"
"syscall"
"unsafe"
)
func main() {
userDll := syscall.NewLazyDLL("user32.dll")
getWindowRectProc := userDll.NewProc("GetCursorPos")
type POINT struct {
X, Y int32
}
var pt POINT
_, _, eno := syscall.SyscallN(getWindowRectProc.Addr(), uintptr(unsafe.Pointer(&pt)))
if eno != 0 {
fmt.Println(eno)
}
fmt.Printf("[cursor.Pos] X:%d Y:%d\n", pt.X, pt.Y)
}
Upvotes: 2