Tuesday, July 19, 2011

How to check status of Keybord?


Option Explicit
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
Dim kstatus As Integer

Private Function CapsLockOn() As Boolean
    Dim xState As Integer
    xState = GetKeyState(vbKeyCapital)
    CapsLockOn = (xState = 1 Or xState = -127)
End Function

Private Sub Command1_Click()
Dim str As String
If CapsLockOn = True Then
    Select Case kstatus
        Case 1
            str = "Shift pressed"
        Case 2
            str = "Ctrl pressed"
        Case 4
            str = "Alt pressed"
        Case Else
            str = "No shift"
    End Select
    Text1.Text = "CapsLock is on" & " - " & str
Else
    Select Case kstatus
        Case 1
            str = "Shift pressed"
        Case 2
            str = "Ctrl pressed"
        Case 4
            str = "Alt pressed"
        Case Else
            str = "No shift"
    End Select
    Text1.Text = "CapsLock is off" & " - " & str
End If
End Sub


Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
kstatus = Shift
End Sub

Private Sub Command1_KeyPress(KeyAscii As Integer)
    Dim str As String
    str = Chr(KeyAscii)
    Text1.Text = str
End Sub

Private Sub Command1_KeyUp(KeyCode As Integer, Shift As Integer)
kstatus = Shift
End Sub

No comments:

Post a Comment