dppcyt6157 2018-07-09 13:16
浏览 136
已采纳

无法将virtualKey转换为unicode

I am trying to transform virtualKey to unicode using ToUnicodeEx function from user32.dll. According to the documentation, this method should return 1 or more if success. In my case, it always 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)
  • 写回答

1条回答 默认 最新

  • doupeng5320 2018-07-15 10:44
    关注

    I have found the solution. You should call ToUnicode function instead of 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])
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab如何绘制三维瀑布图
  • ¥15 关于用abap来解决动态规划的问题,但是要求输出索引值,这个是难点
  • ¥15 在ISIS中什么是IP从地址
  • ¥15 压测时,并发量过高时,响应时间出现尖刺
  • ¥15 关于vmprotect3.8.4虚拟文件一项
  • ¥15 在不用IT调试的情况下怎样能连外网
  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决