Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use SendInput instead of keybd_event #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
module github.com/micmonay/keybd_event

go 1.16

48 changes: 42 additions & 6 deletions keybd_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package keybd_event

import (
"syscall"
"unsafe"
)

var dll = syscall.NewLazyDLL("user32.dll")
var procKeyBd = dll.NewProc("keybd_event")
var (
user32 = syscall.NewLazyDLL("user32.dll")
procSendInput = user32.NewProc("SendInput")
)

// Press key(s)
func (k *KeyBonding) Press() error {
Expand Down Expand Up @@ -37,7 +40,7 @@ func (k *KeyBonding) Press() error {
return nil
}

//Release key(s)
// Release key(s)
func (k *KeyBonding) Release() error {
if k.hasALT {
upKey(_VK_ALT)
Expand Down Expand Up @@ -76,6 +79,7 @@ func (k *KeyBonding) Launching() error {
err = k.Release()
return err
}

func downKey(key int) {
flag := 0
if key < 0xFFF { // Detect if the key code is virtual or no
Expand All @@ -84,7 +88,7 @@ func downKey(key int) {
key -= 0xFFF
}
vkey := key + 0x80
procKeyBd.Call(uintptr(key), uintptr(vkey), uintptr(flag), 0)
sendInput(key, vkey, flag)
}
func upKey(key int) {
flag := _KEYEVENTF_KEYUP
Expand All @@ -94,8 +98,26 @@ func upKey(key int) {
key -= 0xFFF
}
vkey := key + 0x80
procKeyBd.Call(uintptr(key), uintptr(vkey), uintptr(flag), 0)
sendInput(key, vkey, flag)
}

func sendInput(key int, vkey int, flag int) {
var i input
i.inputType = _INPUT_KEYBOARD
i.ki = keyboardInput{
wVk: uint16(key),
wScan: uint16(vkey),
dwFlags: uint32(flag),
time: 0,
dwExtraInfo: 0,
}
procSendInput.Call(
uintptr(1),
uintptr(unsafe.Pointer(&i)),
uintptr(unsafe.Sizeof(i)),
)
}

func initKeyBD() error { return nil }

const (
Expand All @@ -111,6 +133,7 @@ const (
_VK_RWIN = 0x5C + 0xFFF
_KEYEVENTF_KEYUP = 0x0002
_KEYEVENTF_SCANCODE = 0x0008
_INPUT_KEYBOARD = 1
)
const (
VK_SP1 = 41
Expand Down Expand Up @@ -188,7 +211,7 @@ const (
VK_F22 = 0x85 + 0xFFF
VK_F23 = 0x86 + 0xFFF
VK_F24 = 0x87 + 0xFFF

VK_NUMLOCK = 69
VK_SCROLLLOCK = 70
VK_RESERVED = 0
Expand Down Expand Up @@ -308,3 +331,16 @@ const (
VK_PA1 = 0xFD + 0xFFF
VK_OEM_CLEAR = 0xFE + 0xFFF
)

type input struct {
inputType uint32
ki keyboardInput
padding uint64
}
type keyboardInput struct {
wVk uint16
wScan uint16
dwFlags uint32
time uint32
dwExtraInfo uint64
}