在VBA中的GetKeyboardState键(0)之后是什么“keys(0)”?

时间:2012-10-29 18:45:13

标签: excel vba excel-vba

我是VBA中使用Windows API函数的新手,我找不到键(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

1 个答案:

答案 0 :(得分:4)

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

当您传递密钥(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.