2

我是在 VBA 中使用 Windows API 函数的新手,我找不到 keys(0) 的确切作用/含义。它也不在括号/括号中,所以我不明白它是如何工作的。提前致谢!

Private Declare PtrSafe Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long

Property Get Value() As Boolean

    Dim keys(0 To 255) As Byte
    GetKeyboardState keys(0) '<< Can someone explain what this is doing?
    Value = keys(VK_NUMLOCK)

End Property
4

1 回答 1

4

我假设您已经知道GetKeyboardState用于获取键状态的数组。

当您传递 keys(0) 时,您实际上是在将数组的内存位置提供给 Win API 函数。通过这样做,您的数组通过引用传递给函数,并且您传递的数组被数据填充。

这是从我提供的链接页面复制的示例用法,只是因为它有很多评论:

' Display the key status of the Enter and Backspace keys
' Enter's virtual key code = 13; Backspace's virtual key code = 8
Dim keystat(0 To 255) As Byte ' receives key status information for all keys
Dim retval As Long ' return value of function

retval = GetKeyboardState(keystat(0)) ' In VB, the array is passed by referencing element #0.
' Display info about Enter
If (keystat(13) And &H01) = &H01 Then Debug.Print "Enter is being pressed."
If (keystat(13) And &H80) = &H80 Then Debug.Print "Enter is toggled."
' Display info about Backspace
If (keystat(8) And &H01) = &H01 Then Debug.Print "Backspace is being pressed."
If (keystat(8) And &H80) = &H80 Then Debug.Print "Backspace is toggled.
于 2012-10-29T19:12:31.657 回答