The new vbAccelerator Site - more VB and .NET Code and Controls

Read the NumLock and CapsLock key states

Author:

Steve McMahon(steve@vbaccelerator.com)

Keywords:

API,Keyboard

Updated:

01/08/98

Other Tips
All Tips
By Date
By Subject


API (33)
Bit
Manipulation (3)

Clipboard (3)
Combo
Box (5)

Desktop (3)
GDI (13)
Graphics (13)
Internet (2)
Interprocess
Comms (3)

Keyboard (2)
Mouse (1)
Shell (1)
Sprites (1)
Subclassing (3)
Text
Box (2)

Windows (11)
Windows
Controls (10)



Submit


This sample demonstrates how to determine the state of the NumLock and CapsLock key (plus any other key on the keyboard at the same time).

Create a new project, and add a Label control and a Timer to the project's form. Set the timer's interval to 250, then add the following code:

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

Private Sub Timer1_Timer()
Dim b(0 To 254) As Byte
Dim sMsg As String
    GetKeyboardState b(0)
    If b(vbKeyNumlock) Then
        sMsg = "NUM"
    End If
    If b(vbKeyCapital) Then
        sMsg = sMsg & " CAPS"
    End If
    If (Label1.Caption <> sMsg) Then
        Label1.Caption = sMsg
    End If
End Sub

When you run the code, the label is updated with the current Caps and Num Lock states as they are pressed.


 

Related Tips and Articles:

 
 

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 01/08/98

g="0" cellspacing="0" width="100%">  

About  Contribute  Send Feedback  Privacy

Copyright © 1998-1999, Steve McMahon ( steve@vbaccelerator.com). All Rights Reserved.
Last updated: 01/08/98