Files
p2p/build/anjian/main.go

80 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"syscall"
"time"
"unsafe"
)
// 定义Windows API所需的结构体和常量对应SendInput函数参数
const (
INPUT_MOUSE = 0x0000 // 输入类型:鼠标
MOUSEEVENTF_LEFTDOWN = 0x0002 // 左键按下
MOUSEEVENTF_LEFTUP = 0x0004 // 左键释放
)
// INPUT结构体SendInput的输入参数
type INPUT struct {
Type uint32
Mi MOUSEINPUT
}
// MOUSEINPUT结构体鼠标输入详情
type MOUSEINPUT struct {
Dx int32
Dy int32
MouseData uint32
DwFlags uint32
Time uint32
DwExtraInfo uintptr
}
func main() {
// 加载user32.dll并获取SendInput函数地址
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
panic(err)
}
defer syscall.FreeLibrary(user32)
sendInputProc, err := syscall.GetProcAddress(user32, "SendInput")
if err != nil {
panic(err)
}
// 定义一次完整点击的输入(按下+释放)
inputs := []INPUT{
{
Type: INPUT_MOUSE,
Mi: MOUSEINPUT{
DwFlags: MOUSEEVENTF_LEFTDOWN, // 左键按下
},
},
{
Type: INPUT_MOUSE,
Mi: MOUSEINPUT{
DwFlags: MOUSEEVENTF_LEFTUP, // 左键释放
},
},
}
// 循环执行1000次点击间隔10毫秒
for i := 0; i < 1000; i++ {
// 调用SendInput发送鼠标事件参数输入数量、输入数组指针、单个输入大小
var args []uintptr
args = append(args, uintptr(len(inputs)))
args = append(args, uintptr(unsafe.Pointer(&inputs[0])))
args = append(args, uintptr(unsafe.Sizeof(INPUT{})))
ret, _, _ := syscall.SyscallN(sendInputProc, args...)
if ret == 0 {
panic("发送鼠标事件失败")
}
// 间隔10毫秒
time.Sleep(10 * time.Millisecond)
}
println("已完成1000次鼠标点击")
}