Skip to content

Commit

Permalink
Added SendInput()
Browse files Browse the repository at this point in the history
closes #19
  • Loading branch information
bbigras committed Sep 10, 2013
1 parent 36c3bcb commit d32a557
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -11,5 +11,6 @@

Allen Dang <allengnr@gmail.com>
Benny Siegert <bsiegert@gmail.com>
Bruno Bigras <bigras.bruno@gmail.com>
Gerald Rosenberg <gerald.rosenberg@gmail.com>
Michael Henke
23 changes: 23 additions & 0 deletions constants.go
Expand Up @@ -2636,3 +2636,26 @@ const (
PFD_DOUBLEBUFFER_DONTCARE = 0x40000000
PFD_STEREO_DONTCARE = 0x80000000
)

const (
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
)

const (
MOUSEEVENTF_ABSOLUTE = 0x8000
MOUSEEVENTF_HWHEEL = 0x01000
MOUSEEVENTF_MOVE = 0x0001
MOUSEEVENTF_MOVE_NOCOALESCE = 0x2000
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
MOUSEEVENTF_RIGHTDOWN = 0x0008
MOUSEEVENTF_RIGHTUP = 0x0010
MOUSEEVENTF_MIDDLEDOWN = 0x0020
MOUSEEVENTF_MIDDLEUP = 0x0040
MOUSEEVENTF_VIRTUALDESK = 0x4000
MOUSEEVENTF_WHEEL = 0x0800
MOUSEEVENTF_XDOWN = 0x0080
MOUSEEVENTF_XUP = 0x0100
)
49 changes: 49 additions & 0 deletions typedef.go
Expand Up @@ -838,3 +838,52 @@ type PIXELFORMATDESCRIPTOR struct {
DwVisibleMask uint32
DwDamageMask uint32
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646270(v=vs.85).aspx
type INPUT struct {
Type uint32
Mi MOUSEINPUT
Ki KEYBDINPUT
Hi HARDWAREINPUT
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646273(v=vs.85).aspx
type MOUSEINPUT struct {
Dx int32
Dy int32
MouseData uint32
DwFlags uint32
Time uint32
DwExtraInfo uintptr
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646271(v=vs.85).aspx
type KEYBDINPUT struct {
WVk uint16
WScan uint16
DwFlags uint32
Time uint32
DwExtraInfo uintptr
}

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms646269(v=vs.85).aspx
type HARDWAREINPUT struct {
UMsg uint32
WParamL uint16
WParamH uint16
}

type KbdInput struct {
typ uint32
ki KEYBDINPUT
}

type MouseInput struct {
typ uint32
mi MOUSEINPUT
}

type HardwareInput struct {
typ uint32
hi HARDWAREINPUT
}
31 changes: 31 additions & 0 deletions user32.go
Expand Up @@ -5,6 +5,8 @@
package w32

import (
// #include <WTypes.h>
"C"
"fmt"
"syscall"
"unsafe"
Expand Down Expand Up @@ -108,6 +110,7 @@ var (
procEnumDisplayMonitors = moduser32.NewProc("EnumDisplayMonitors")
procEnumDisplaySettingsEx = moduser32.NewProc("EnumDisplaySettingsExW")
procChangeDisplaySettingsEx = moduser32.NewProc("ChangeDisplaySettingsExW")
procSendInput = moduser32.NewProc("SendInput")
)

func RegisterClassEx(wndClassEx *WNDCLASSEX) ATOM {
Expand Down Expand Up @@ -915,3 +918,31 @@ func ChangeDisplaySettingsEx(szDeviceName *uint16, devMode *DEVMODE, hwnd HWND,
)
return int32(ret)
}

func SendInput(inputs []INPUT) uint32 {
var validInputs []C.INPUT

for _, oneInput := range inputs {
input := C.INPUT{_type: C.DWORD(oneInput.Type)}

switch oneInput.Type {
case INPUT_MOUSE:
(*MouseInput)(unsafe.Pointer(&input)).mi = oneInput.Mi
case INPUT_KEYBOARD:
(*KbdInput)(unsafe.Pointer(&input)).ki = oneInput.Ki
case INPUT_HARDWARE:
(*HardwareInput)(unsafe.Pointer(&input)).hi = oneInput.Hi
default:
panic("unkown type")
}

validInputs = append(validInputs, input)
}

ret, _, _ := procSendInput.Call(
uintptr(len(validInputs)),
uintptr(unsafe.Pointer(&validInputs[0])),
uintptr(unsafe.Sizeof(C.INPUT{})),
)
return uint32(ret)
}

0 comments on commit d32a557

Please sign in to comment.