Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleTurn CapsLock on and off
KeywordsCapsLock, Caps Lock
CategoriesAPI
 
Use the GetKeyboardState API function to get the current keyboard state. Set the VK_CAPITAL entry and use SetKeyboardState to update the keyboard.

Thanks to Joost Tack.

 
Function CapsLock() As Boolean
    CapsLock = (GetKeyState(VK_CAPITAL) And 1 = 1)
End Function

Private Sub Form_Load()
    If CapsLock() = 1 Then Label1 = "On" Else Label1 = "Off"
End Sub

Private Sub cmdToggle_Click()
    GetKeyboardState kbArray
    kbArray.kbByte(VK_CAPITAL) = _
        IIf(kbArray.kbByte(VK_CAPITAL) = 1, 0, 1)
    SetKeyboardState kbArray

    If CapsLock() Then
        Label1.Caption = "On"
    Else
        Label1.Caption = "Off"
    End If
End Sub

Private Sub cmdTurnOn_Click()
    GetKeyboardState kbArray
    kbArray.kbByte(VK_CAPITAL) = 1
    SetKeyboardState kbArray

    If CapsLock() Then
        Label1.Caption = "On"
    Else
        Label1.Caption = "Off"
    End If
End Sub

Private Sub cmdTurnOff_Click()
    GetKeyboardState kbArray
    kbArray.kbByte(VK_CAPITAL) = 0
    SetKeyboardState kbArray

    If CapsLock() Then
        Label1.Caption = "On"
    Else
        Label1.Caption = "Off"
    End If
End Sub
 
Formatted by Neil Crosby
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated