by Jeffry O'Neil ()
Category: Windows API Call/Explanation
Compatability: Visual Basic 5.0
Difficulty: Unknown Difficulty
Originally Published: Mon 18th June 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Another Get-Set Key state program
API Declarations
Const VK_NUMLOCK = &H90
Const VK_SCROLL = &H91
Const VK_USED = VK_SCROLL
Private Type KeyboardBytes
kbByte(0 To 255) As Byte
End Type
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Long
Private Declare Function GetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Private Declare Function SetKeyboardState Lib "user32" (kbArray As KeyboardBytes) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Dim kbArray As KeyboardBytes, CapsLock As Boolean, kbOld As KeyboardBytes
'code
Private Sub Form_Load()
Dim start As Single
'Get,or save, the current keyboardstate
GetKeyboardState kbOld
'Hide the form
Me.Hide
MsgBox "Look at the num-, shift- and scrolllock lights on the keyboard." + vbCrLf _
+ "& Wait a few secs..."
TurnOff VK_CAPITAL
TurnOff VK_NUMLOCK
TurnOff VK_SCROLL
Sleep 1000
start = Timer
Dim Leds, i As Integer, tmp As Long, signum As Integer
Leds = Array(VK_NUMLOCK, VK_CAPITAL, VK_SCROLL)
signum = 1
Do
DoEvents 'yield execution to op. system
tmp = Leds(i)
TurnOn tmp 'turn on the one of the leds on kbd
Sleep 403
TurnOff tmp 'turn off the one of the leds on kbd
If i = 2 Then
signum = -1
ElseIf i = 0 Then
signum = 1
End If
i = i + signum * 1
Loop Until Timer > start + 15
Unload Me
End Sub
Private Sub TurnOn(vkKey As Long)
'Get the keyboard state
GetKeyboardState kbArray
'Change a key
kbArray.kbByte(vkKey) = 1
'Set the keyboard state
SetKeyboardState kbArray
End Sub
Private Sub TurnOff(vkKey As Long)
'Get the keyboard state
GetKeyboardState kbArray
'change a key
kbArray.kbByte(vkKey) = 0
'set the keyboard state
SetKeyboardState kbArray
End Sub
Private Sub Form_Unload(Cancel As Integer)
'restore the old keyboard state
SetKeyboardState kbOld
End Sub
'Jeffry O'Neil 2001
'There is no got but Allah and Muhammad is the Messenger of Allah