登录
首页 >  Golang >  Go问答

win.RegisterRawInputDevices 始终返回 false

来源:stackoverflow

时间:2024-04-12 08:36:23 456浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《win.RegisterRawInputDevices 始终返回 false》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我正在使用 https://github.com/lxn/win 包来访问 go 中的低级 windows 调用。我正在调用 win.registerrawinputdevices 来注册原始输入数据的设备,但它总是返回 false。我用 c# 完成了这个,没有任何问题。下面是我的代码:

package main

import (
    "fmt"
    "syscall"
    "unsafe"
    "github.com/lxn/win"
)

func WndProc(hWnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
    switch msg {
    case win.WM_CREATE:
        fmt.Println("Received WM_CREATE message")
        devices := getRawInputDevices(hWnd)
        len := uint32(len(devices))
        size := uint32(unsafe.Sizeof(devices[0]))
        if !win.RegisterRawInputDevices(&devices[0], len, size) {
            panic("Unable to register devices")
        }

    case win.WM_DESTROY:
        fmt.Println("Posting quit message")
        win.PostQuitMessage(0)
    default:
        return win.DefWindowProc(hWnd, msg, wParam, lParam)
    }

    return 0
}

func getRawInputDevices(hWnd win.HWND) []win.RAWINPUTDEVICE {
    devices := make([]win.RAWINPUTDEVICE, 5)
    devices[0].UsUsagePage = 0x01
    devices[0].UsUsage = 0x02
    devices[0].DwFlags = win.RIDEV_INPUTSINK
    devices[0].HwndTarget = hWnd
    devices[1].UsUsagePage = 0x01
    devices[1].UsUsage = 0x06
    devices[1].DwFlags = win.RIDEV_INPUTSINK
    devices[1].HwndTarget = hWnd
    devices[2].UsUsagePage = 0x00
    devices[2].UsUsage = 0x51
    devices[2].DwFlags = win.RIDEV_INPUTSINK
    devices[2].HwndTarget = hWnd
    devices[3].UsUsagePage = 0x00
    devices[3].UsUsage = 0x04
    devices[3].DwFlags = win.RIDEV_INPUTSINK
    devices[3].HwndTarget = hWnd
    devices[4].UsUsagePage = 0x00
    devices[4].UsUsage = 0x00
    devices[4].DwFlags = win.RIDEV_INPUTSINK
    devices[4].HwndTarget = hWnd
    return devices
}

func WinMain() int {

    hInstance := win.GetModuleHandle(syscall.StringToUTF16Ptr(""))
    lpszClassName := syscall.StringToUTF16Ptr("WNDclass")
    var wcex win.WNDCLASSEX
    wcex.HInstance = hInstance
    wcex.LpszClassName = lpszClassName
    wcex.LpfnWndProc = syscall.NewCallback(WndProc)
    wcex.CbSize = uint32(unsafe.Sizeof(wcex))
    win.RegisterClassEx(&wcex)
    win.CreateWindowEx(
        0, lpszClassName, syscall.StringToUTF16Ptr("Simple Go Window!"),
        win.WS_OVERLAPPEDWINDOW,
        win.CW_USEDEFAULT, win.CW_USEDEFAULT, 400, 400, 0, 0, hInstance, nil)
    var msg win.MSG
    for {
        if win.GetMessage(&msg, 0, 0, 0) == 0 {
            break
        }
        win.TranslateMessage(&msg)
        win.DispatchMessage(&msg)
    }
    return int(msg.WParam)
}

func main() {
    WinMain()
    return
}

我收到 wm_create 消息,但 win.registerrawinputdevices 始终返回 false,并显示“参数不正确”。错误。正如我提到的,我已经用 c# 完成了此操作,没有出现任何问题,因此我熟悉基本步骤。我是 go 新手,所以我可能会遗漏一些东西。有什么想法吗?


解决方案


经过几个小时研究 Go 的结构对齐策略和各种其他低级内容后,我终于意识到我的 RAWINPUTDEVICE.UsUsagePage 值不正确!元素 2、3 和 4 应将 UsUsagePage 设置为 0x0D,而不是 0x00!哦,好吧,至少我学到了很多关于 Go 如何将结构元素与单词边界对齐的知识!

今天关于《win.RegisterRawInputDevices 始终返回 false》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>