Go语言 – 使用RobotGo获取当前鼠标光标位置

huangapple go评论127阅读模式
英文:

Go lang - Get current mouse cursor position with RobotGo

问题

我是一个Go语言的初学者,我想解决一个小任务:将鼠标光标的当前坐标打印到控制台,并在鼠标移动时更新坐标。为此,我使用了robotgo库,通过以下命令下载它:"go get github.com/go-vgo/robotgo",然后使用"go get -u github.com/go-vgo/robotgo"进行更新,并使用import "github.com/go-vgo/robotgo"导入它。以下是我编写的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/go-vgo/robotgo"
  5. )
  6. func main() {
  7. for {
  8. x, y := robotgo.GetMousePos()
  9. fmt.Printf("当前位置 X=%d, Y=%d", x, y)
  10. if x == 540 && y == 960 {
  11. fmt.Println("光标在中心位置")
  12. }
  13. robotgo.Move(x, y)
  14. }
  15. }

但是我遇到了错误:

  1. # github.com/robotn/gohook
  2. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:51:10: undefined: addEvent
  3. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:62:7: undefined: Start
  4. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:75:18: undefined: KeyHold
  5. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:83:18: undefined: KeyUp
  6. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:95:22: undefined: KeyUp
  7. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:96:4: undefined: End
  8. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:113:7: undefined: Start
  9. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:121:17: undefined: MouseMove
  10. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:128:22: undefined: MouseDown
  11. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:129:4: undefined: End
  12. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:129:4: too many errors

更新 2023年3月16日 问题已通过完全删除库及其依赖项,然后重新安装解决。

英文:

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:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/go-vgo/robotgo"
  5. )
  6. func main() {
  7. for {
  8. x, y := robotgo.GetMousePos()
  9. fmt.Printf("Current position X=%d, Y=%d", x, y)
  10. if x == 540 && y == 960 {
  11. fmt.Println("Cursor @center")
  12. }
  13. robotgo.Move(x, y)
  14. }
  15. }

But I'm getting errors:

  1. # github.com/robotn/gohook
  2. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:51:10: undefined: addEvent
  3. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:62:7: undefined: Start
  4. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:75:18: undefined: KeyHold
  5. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:83:18: undefined: KeyUp
  6. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:95:22: undefined: KeyUp
  7. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:96:4: undefined: End
  8. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:113:7: undefined: Start
  9. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:121:17: undefined: MouseMove
  10. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:128:22: undefined: MouseDown
  11. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\event.go:129:4: undefined: End
  12. C:\CODING\SDK\GO-PATH\pkg\mod\github.com\robotn\gohook@v0.40.0\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.

答案1

得分: 1

对于Windows系统,可以使用系统API:GetCursorPos

  1. //go:build windows
  2. package main
  3. import (
  4. "fmt"
  5. "syscall"
  6. "unsafe"
  7. )
  8. func main() {
  9. userDll := syscall.NewLazyDLL("user32.dll")
  10. getCursorPosProc := userDll.NewProc("GetCursorPos")
  11. type POINT struct {
  12. X, Y int32
  13. }
  14. var pt POINT
  15. _, _, eno := syscall.Syscall(getCursorPosProc.Addr(), 1, uintptr(unsafe.Pointer(&pt)), 0, 0)
  16. if eno != 0 {
  17. fmt.Println(eno)
  18. }
  19. fmt.Printf("[cursor.Pos] X:%d Y:%d\n", pt.X, pt.Y)
  20. }

请注意,这是一个使用Go语言编写的示例代码,用于获取鼠标光标的位置。它使用了Windows系统的user32.dll库中的GetCursorPos函数。

英文:

For windows, use the system API :GetCursorPos

  1. //go:build windows
  2. package main
  3. import (
  4. "fmt"
  5. "syscall"
  6. "unsafe"
  7. )
  8. func main() {
  9. userDll := syscall.NewLazyDLL("user32.dll")
  10. getWindowRectProc := userDll.NewProc("GetCursorPos")
  11. type POINT struct {
  12. X, Y int32
  13. }
  14. var pt POINT
  15. _, _, eno := syscall.SyscallN(getWindowRectProc.Addr(), uintptr(unsafe.Pointer(&pt)))
  16. if eno != 0 {
  17. fmt.Println(eno)
  18. }
  19. fmt.Printf("[cursor.Pos] X:%d Y:%d\n", pt.X, pt.Y)
  20. }

huangapple
  • 本文由 发表于 2023年3月16日 04:58:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75750013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定