无法将 virtualKey 转换为 unicode
Can't transform virtualKey into unicode
我正在尝试使用 user32.dll 中的 ToUnicodeEx 函数将 virtualKey 转换为 unicode。根据文档,如果成功,此方法应该 return 1 或更多。在我的例子中,它总是 returns 0.
procGetKeyboardState = user32.NewProc("GetKeyboardState")
procMapVirtualKey = user32.NewProc("MapVirtualKeyA")
procToUnicodeEx = user32.NewProc("ToUnicodeEx")
...
func getKeyboardState(keyboardState *uint16) (len int32, err error) {
r0, _, e1 := syscall.Syscall(procGetKeyboardState.Addr(), 1, uintptr(unsafe.Pointer(keyboardState)), 0, 0)
len = int32(r0)
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
...
func mapVirtualKey(uCode syscall.Handle) (scanCode syscall.Handle, err error) {
r0, _, e1 := syscall.Syscall(procMapVirtualKey.Addr(), 2, uintptr(uCode), 0, 0)
if e1 != 0 {
err = error(e1)
return
}
scanCode = syscall.Handle(r0)
return
}
...
func toUnicodeEx(virtKey syscall.Handle, scanCode syscall.Handle, keyState *uint16, pwszBuff *uint16) (value syscall.Handle) {
r0, _, _ := syscall.Syscall6(
procToUnicodeEx.Addr(),
6,
uintptr(virtKey),
uintptr(scanCode),
uintptr(unsafe.Pointer(keyState)),
uintptr(unsafe.Pointer(pwszBuff)),
1,
0,
)
value = syscall.Handle(r0)
return
}
...
keyboardBuf := make([]uint16, 200)
_, getKeyboardStateErr := getKeyboardState(&keyboardBuf[0])
if getKeyboardStateErr != nil {
log.Fatalf("getKeyboardState -> %v", getKeyboardStateErr)
}
scanCode, mapVirtualKeyErr := mapVirtualKey(virtualCode)
if mapVirtualKeyErr != nil {
log.Fatalf("mapVirtualKey -> %v", mapVirtualKeyErr)
}
unicodeBuf := make([]uint16, 200)
state := toUnicodeEx(virtualCode, scanCode, &keyboardBuf[0], &unicodeBuf[0])
fmt.Println("KEY BUFF ", keyboardBuf)
fmt.Println("SCAN CODE ", scanCode)
fmt.Println("UNICODE ", state, unicodeBuf)
我找到了解决办法。您应该调用 ToUnicode 函数而不是 ToUnicodeEx。
procToUnicode = user32.NewProc("ToUnicode")
...
func toUnicode(virtKey syscall.Handle, scanCode syscall.Handle, keyState *uint16, pwszBuff *uint16) (value syscall.Handle) {
r0, _, _ := syscall.Syscall6(
procToUnicode.Addr(),
6,
uintptr(virtKey),
uintptr(scanCode),
uintptr(unsafe.Pointer(keyState)),
uintptr(unsafe.Pointer(pwszBuff)),
1,
0,
)
value = syscall.Handle(r0)
return
}
...
state := toUnicode(virtualCode, scanCode, &keyboardBuf[0], &unicodeBuf[0])
我正在尝试使用 user32.dll 中的 ToUnicodeEx 函数将 virtualKey 转换为 unicode。根据文档,如果成功,此方法应该 return 1 或更多。在我的例子中,它总是 returns 0.
procGetKeyboardState = user32.NewProc("GetKeyboardState")
procMapVirtualKey = user32.NewProc("MapVirtualKeyA")
procToUnicodeEx = user32.NewProc("ToUnicodeEx")
...
func getKeyboardState(keyboardState *uint16) (len int32, err error) {
r0, _, e1 := syscall.Syscall(procGetKeyboardState.Addr(), 1, uintptr(unsafe.Pointer(keyboardState)), 0, 0)
len = int32(r0)
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
...
func mapVirtualKey(uCode syscall.Handle) (scanCode syscall.Handle, err error) {
r0, _, e1 := syscall.Syscall(procMapVirtualKey.Addr(), 2, uintptr(uCode), 0, 0)
if e1 != 0 {
err = error(e1)
return
}
scanCode = syscall.Handle(r0)
return
}
...
func toUnicodeEx(virtKey syscall.Handle, scanCode syscall.Handle, keyState *uint16, pwszBuff *uint16) (value syscall.Handle) {
r0, _, _ := syscall.Syscall6(
procToUnicodeEx.Addr(),
6,
uintptr(virtKey),
uintptr(scanCode),
uintptr(unsafe.Pointer(keyState)),
uintptr(unsafe.Pointer(pwszBuff)),
1,
0,
)
value = syscall.Handle(r0)
return
}
...
keyboardBuf := make([]uint16, 200)
_, getKeyboardStateErr := getKeyboardState(&keyboardBuf[0])
if getKeyboardStateErr != nil {
log.Fatalf("getKeyboardState -> %v", getKeyboardStateErr)
}
scanCode, mapVirtualKeyErr := mapVirtualKey(virtualCode)
if mapVirtualKeyErr != nil {
log.Fatalf("mapVirtualKey -> %v", mapVirtualKeyErr)
}
unicodeBuf := make([]uint16, 200)
state := toUnicodeEx(virtualCode, scanCode, &keyboardBuf[0], &unicodeBuf[0])
fmt.Println("KEY BUFF ", keyboardBuf)
fmt.Println("SCAN CODE ", scanCode)
fmt.Println("UNICODE ", state, unicodeBuf)
我找到了解决办法。您应该调用 ToUnicode 函数而不是 ToUnicodeEx。
procToUnicode = user32.NewProc("ToUnicode")
...
func toUnicode(virtKey syscall.Handle, scanCode syscall.Handle, keyState *uint16, pwszBuff *uint16) (value syscall.Handle) {
r0, _, _ := syscall.Syscall6(
procToUnicode.Addr(),
6,
uintptr(virtKey),
uintptr(scanCode),
uintptr(unsafe.Pointer(keyState)),
uintptr(unsafe.Pointer(pwszBuff)),
1,
0,
)
value = syscall.Handle(r0)
return
}
...
state := toUnicode(virtualCode, scanCode, &keyboardBuf[0], &unicodeBuf[0])