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

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

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"导入它。以下是我编写的代码:

package main

import (
	"fmt"
	"github.com/go-vgo/robotgo"
)

func main() {
	for {
		x, y := robotgo.GetMousePos()
		fmt.Printf("当前位置 X=%d, Y=%d", x, y)
		if x == 540 && y == 960 {
			fmt.Println("光标在中心位置")
		}

		robotgo.Move(x, y)
	}
}

但是我遇到了错误:

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

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

//go:build windows

package main

import (
	"fmt"
	"syscall"
	"unsafe"
)

func main() {
	userDll := syscall.NewLazyDLL("user32.dll")
	getCursorPosProc := userDll.NewProc("GetCursorPos")
	type POINT struct {
		X, Y int32
	}
	var pt POINT
	_, _, eno := syscall.Syscall(getCursorPosProc.Addr(), 1, uintptr(unsafe.Pointer(&pt)), 0, 0)
	if eno != 0 {
		fmt.Println(eno)
	}
	fmt.Printf("[cursor.Pos] X:%d Y:%d\n", pt.X, pt.Y)
}

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

英文:

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)
}

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:

确定