首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >VBA和GetRawInputDeviceList

VBA和GetRawInputDeviceList
EN

Stack Overflow用户
提问于 2016-03-26 16:59:04
回答 1查看 1.4K关注 0票数 3

我正在Access 2013年工作,并试图为VBA获取GetRawInputDeviceList、GetRawInputDeviceInfo、RegisterRawInputDevices和GetRawInputData的等价物,但没有成功。我还徒劳地搜索了一个程序、功能或模块,以便将连接的HID设备列表发送给计算机,以选择条形码扫描器。这是第三周的开始,所以我跪在那里乞求帮助。你们中的任何一个人是否都有一个你愿意分享的模块,一个链接到一个处理这个问题的网站?任何帮助都是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-26 20:10:13

使用VBA的GetRawInputDeviceList API将非常棘手,因为pRawInputDeviceList参数。除非您愿意跳过大量的循环来管理自己的内存,并在原始内存中手动处理RAWINPUTDEVICELIST的结果数组,否则最好从另一个方向来处理。

我处理过的大多数条形码扫描器都以键盘的形式出现在Windows上。一种可能的解决方案是使用WMI查询枚举附加的键盘设备:

代码语言:javascript
运行
复制
Private Sub ShowKeyboardInfo()
    Dim WmiServer As Object
    Dim ResultSet As Object
    Dim Keyboard As Object
    Dim Query As String

    Query = "SELECT * From Win32_Keyboard"
    Set WmiServer = GetObject("winmgmts:root/CIMV2")
    Set ResultSet = WmiServer.ExecQuery(Query)

    For Each Keyboard In ResultSet
        Debug.Print Keyboard.Name & vbTab & _
                    Keyboard.Description & vbTab & _
                    Keyboard.DeviceID & vbTab & _
                    Keyboard.Status
    Next Keyboard
End Sub

注意:如果没有出现,可以通过查询USBDeviceQuery = "SELECT * From Win32_Keyboard"来枚举所有的USB设备。

编辑:对于注释,上面的代码不会返回注册接收原始输入事件所需的句柄。不过,这应该会让您开始-- RegisterRawInputDevices和GetRawInputData方面超出了答案的范围。对此进行黑客攻击,如果遇到任何问题,请在另一个问题中发布您的代码。

声明:

代码语言:javascript
运行
复制
Private Type RawInputDeviceList
    hDevice As Long
    dwType As Long
End Type

Private Type RidKeyboardInfo
    cbSize As Long
    dwType As Long
    dwKeyboardMode As Long
    dwNumberOfFunctionKeys As Long
    dwNumberOfIndicators As Long
    dwNumberOfKeysTotal As Long
End Type

Private Enum DeviceType
    TypeMouse = 0
    TypeKeyboard = 1
    TypeHID = 2
End Enum

Private Enum DeviceCommand
    DeviceName = &H20000007
    DeviceInfo = &H2000000B
    PreParseData = &H20000005
End Enum

Private Declare Function GetRawInputDeviceList Lib "user32" ( _
    ByVal pRawInputDeviceList As Long, _
    ByRef puiNumDevices As Long, _
    ByVal cbSize As Long) As Long

Private Declare Function GetRawInputDeviceInfo Lib "user32" Alias "GetRawInputDeviceInfoW" ( _
    ByVal hDevice As Long, _
    ByVal uiCommand As Long, _
    ByVal pData As Long, _
    ByRef pcbSize As Long) As Long

Private Declare Function GetLastError Lib "kernel32" () As Long

用GetRawInputDeviceInfo检索设备名称的示例:

代码语言:javascript
运行
复制
Private Sub SampleCode()
    Dim devices() As RawInputDeviceList

    devices = GetRawInputDevices
    Dim i As Long
    For i = 0 To UBound(devices)
        'Inspect the type - only looking for a keyboard.
        If devices(i).dwType = TypeKeyboard Then
            Dim buffer As String
            Dim size As Long
            'First call with a null pointer returns the string length in size.
            If GetRawInputDeviceInfo(devices(i).hDevice, DeviceName, 0&, size) = -1 Then
                Debug.Print "GetRawInputDeviceInfo error " & GetLastError()
            Else
                'Size the string buffer.
                buffer = String(size, Chr$(0))
                'The second call copies the name into the passed buffer.
                If GetRawInputDeviceInfo(devices(i).hDevice, DeviceName, StrPtr(buffer), size) = -1 Then
                    Debug.Print "GetRawInputDeviceInfo error " & GetLastError()
                Else
                    Debug.Print buffer
                End If
            End If
        End If
    Next i

End Sub

Private Function GetRawInputDevices() As RawInputDeviceList()
    Dim devs As Long
    Dim output() As RawInputDeviceList

    'First call with a null pointer returns the number of devices in devs
    If GetRawInputDeviceList(0&, devs, LenB(output(0))) = -1 Then
        Debug.Print "GetRawInputDeviceList error " & GetLastError()
    Else
        'Size the output array.
        ReDim output(devs - 1)
        'Second call actually fills the array.
        If GetRawInputDeviceList(VarPtr(output(0)), devs, LenB(output(0))) = -1 Then
            Debug.Print "GetRawInputDeviceList error " & GetLastError()
        Else
            GetRawInputDevices = output
        End If
    End If
End Function

对侧滚动很抱歉。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36238125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档