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
&nbsp &nbsp GetKeyboardState b(0)
&nbsp &nbsp If b(vbKeyNumlock) Then
&nbsp &nbsp &nbsp &nbsp sMsg = "NUM"
&nbsp &nbsp End If
&nbsp &nbsp If b(vbKeyCapital) Then
&nbsp &nbsp &nbsp &nbsp sMsg = sMsg & " CAPS"
&nbsp &nbsp End If
&nbsp &nbsp If (Label1.Caption sMsg) Then
&nbsp &nbsp &nbsp &nbsp Label1.Caption = sMsg
&nbsp &nbsp 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.


&nbsp

Related Tips and Articles:

&nbsp

AboutContributeSend FeedbackPrivacy

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