lxn / win

A Windows API wrapper package for the Go Programming Language
Other
1.19k stars 313 forks source link

SendInput problem (maybe user error) #34

Closed jordansissel closed 6 years ago

jordansissel commented 6 years ago

I'm having trouble getting win.SendInput to behave.

package main

import (
    "fmt"
    "unsafe"

    "github.com/lxn/win"
)

func main() {
    println("Moving mouse")

    input := []win.MOUSE_INPUT{
        {
            Type: win.INPUT_MOUSE,
            Mi: win.MOUSEINPUT{
                Dx:      int32(200),
                Dy:      int32(200),
                DwFlags: win.MOUSEEVENTF_MOVE,
            },
        },
    }

    fmt.Printf("SendInput(%d, %#v, %d)\n", len(input), input, unsafe.Sizeof(input[0]))
    i := win.SendInput(uint32(len(input)), unsafe.Pointer(&input), int32(unsafe.Sizeof(input[0])))
    fmt.Printf(" => %d (lastError: %d)\n", i, win.GetLastError())
}

When run, this outputs:

Moving mouse
SendInput(1, []win.MOUSE_INPUT{win.MOUSE_INPUT{Type:0x0, Mi:win.MOUSEINPUT{Dx:200, Dy:200, MouseData:0x0, DwFlags:0x8001, Time:0x0, DwExtraInfo:0x0}}}, 40)
 => 1 (lastError: 0)

But the mouse doesn't move.

I've also tried sending win.KBD_INPUT events, and I am also unsuccessful there.

SendInput is returning 1 which indicates it successfully did the event, but my mouse doesn't seem to move. Ideas?

Windows 10 Version 1607 Build 14393.1715

jordansissel commented 6 years ago

For troubleshooting, I tested AutoHotKey's Send that works, so I'm thinking I'm doing something wrong or maybe there's a bug?

jordansissel commented 6 years ago

Found my issue.

-unsafe.Pointer(&input)
+unsafe.Pointer(&input[0])

I needed to pass a pointer to the first element of the slice, not the slice itself. Oops.

Closing! :)