VBA 和 GetRawInputDeviceList

作者:编程家 分类: vba 时间:2025-11-28

使用VBA和GetRawInputDeviceList进行原始输入设备列表的获取

VBA是一种用于编写宏和自动化任务的编程语言,可以与Microsoft Office套件中的各种应用程序(如Excel、Word、PowerPoint等)进行交互。VBA提供了许多功能和方法,以便开发人员可以更好地控制和操作这些应用程序。

在VBA中,GetRawInputDeviceList是一个非常有用的函数,它可以帮助我们获取原始输入设备的列表。原始输入设备是指直接与计算机进行交互的设备,如键盘、鼠标、触摸屏等。通过获取原始输入设备列表,我们可以获取有关这些设备的详细信息,如设备名称、设备类型、设备ID等。

下面是一个使用VBA和GetRawInputDeviceList函数的案例代码:

Sub GetRawInputDeviceListExample()

Dim numDevices As Long

Dim rawInputDevices() As RAWINPUTDEVICELIST

' 获取原始输入设备列表的数量

numDevices = GetRawInputDeviceList(Null, numDevices, Len(rawInputDevices(0)))

If numDevices > 0 Then

' 调整数组大小

ReDim rawInputDevices(1 To numDevices) As RAWINPUTDEVICELIST

' 获取原始输入设备列表

numDevices = GetRawInputDeviceList(rawInputDevices(1), numDevices, Len(rawInputDevices(0)))

' 遍历设备列表并输出设备信息

For i = 1 To numDevices

Debug.Print "设备名称:" & GetRawInputDeviceName(rawInputDevices(i).hDevice)

Debug.Print "设备类型:" & GetRawInputDeviceType(rawInputDevices(i).dwType)

Debug.Print "设备ID:" & rawInputDevices(i).dwDeviceId

Debug.Print "-------------------------"

Next i

End If

End Sub

在上面的代码中,首先使用GetRawInputDeviceList函数获取原始输入设备列表的数量。然后,根据数量调整rawInputDevices数组的大小,并再次调用GetRawInputDeviceList函数来获取原始输入设备列表。最后,通过遍历设备列表,我们可以输出每个设备的相关信息。

案例代码解析

获取原始输入设备列表的数量

numDevices = GetRawInputDeviceList(Null, numDevices, Len(rawInputDevices(0)))

在此代码行中,我们首先将原始输入设备列表设为Null,将numDevices变量设置为0,并将rawInputDevices数组的长度设置为RAWINPUTDEVICELIST结构体的长度。然后,通过调用GetRawInputDeviceList函数,我们可以获取原始输入设备列表的数量。

调整数组大小并获取原始输入设备列表

ReDim rawInputDevices(1 To numDevices) As RAWINPUTDEVICELIST

numDevices = GetRawInputDeviceList(rawInputDevices(1), numDevices, Len(rawInputDevices(0)))

在这段代码中,我们使用ReDim语句调整rawInputDevices数组的大小,以便适应获取到的原始输入设备列表的数量。然后,我们再次调用GetRawInputDeviceList函数,这次将rawInputDevices(1)作为参数来获取原始输入设备列表。

遍历设备列表并输出设备信息

For i = 1 To numDevices

Debug.Print "设备名称:" & GetRawInputDeviceName(rawInputDevices(i).hDevice)

Debug.Print "设备类型:" & GetRawInputDeviceType(rawInputDevices(i).dwType)

Debug.Print "设备ID:" & rawInputDevices(i).dwDeviceId

Debug.Print "-------------------------"

Next i

在此代码段中,我们使用For循环遍历设备列表,并使用GetRawInputDeviceName函数获取设备名称,使用GetRawInputDeviceType函数获取设备类型,以及直接访问dwDeviceId属性获取设备ID。然后,我们通过调用Debug.Print函数将设备信息输出到VBA的调试窗口。

通过使用VBA和GetRawInputDeviceList函数,我们可以方便地获取原始输入设备列表,并获取有关这些设备的详细信息。这对于开发人员来说非常有用,可以帮助他们更好地了解和控制计算机上的输入设备。

参考代码

vba

Option Explicit

Private Type RAWINPUTDEVICELIST

hDevice As Long

dwType As Long

End Type

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

Private Declare Function GetRawInputDeviceName Lib "user32" Alias "GetRawInputDeviceNameA" (ByVal hDevice As Long, ByVal pData As String, ByRef pcbSize As Long) As Long

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

Private Const RID_INPUT As Long = &H10000003

Private Const RIDI_DEVICENAME As Long = &H20000007

Private Const RIDI_DEVICEINFO As Long = &H2000000B

Sub GetRawInputDeviceListExample()

Dim numDevices As Long

Dim rawInputDevices() As RAWINPUTDEVICELIST

' 获取原始输入设备列表的数量

numDevices = GetRawInputDeviceList(Null, numDevices, Len(rawInputDevices(0)))

If numDevices > 0 Then

' 调整数组大小

ReDim rawInputDevices(1 To numDevices) As RAWINPUTDEVICELIST

' 获取原始输入设备列表

numDevices = GetRawInputDeviceList(rawInputDevices(1), numDevices, Len(rawInputDevices(0)))

' 遍历设备列表并输出设备信息

For i = 1 To numDevices

Debug.Print "设备名称:" & GetRawInputDeviceName(rawInputDevices(i).hDevice)

Debug.Print "设备类型:" & GetRawInputDeviceType(rawInputDevices(i).dwType)

Debug.Print "设备ID:" & rawInputDevices(i).dwDeviceId

Debug.Print "-------------------------"

Next i

End If

End Sub

Function GetRawInputDeviceName(ByVal hDevice As Long) As String

Dim bufferSize As Long

Dim nameBuffer As String

bufferSize = 0

GetRawInputDeviceInfo hDevice, RIDI_DEVICENAME, Null, bufferSize

nameBuffer = Space(bufferSize)

GetRawInputDeviceInfo hDevice, RIDI_DEVICENAME, ByVal nameBuffer, bufferSize

GetRawInputDeviceName = Left(nameBuffer, bufferSize - 1)

End Function

Function GetRawInputDeviceType(ByVal dwType As Long) As String

Select Case dwType

Case 1

GetRawInputDeviceType = "鼠标"

Case 2

GetRawInputDeviceType = "键盘"

Case 3

GetRawInputDeviceType = "硬盘"

Case Else

GetRawInputDeviceType = "未知"

End Select

End Function

参考文献

1. Microsoft VBA官方文档:https://docs.microsoft.com/zh-cn/office/vba/api/

2. GetRawInputDeviceList函数官方文档:https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-getrawinputdevicelist