Below is some info from the MSDN library to detect CAPS/INS/Scroll lock state, and toggle it:
Sample Project
Start a new Standard EXE project in Visual Basic. Form1 is created by default.
Add a CommandButton to Form1.
Add the following code to the General Declarations section of Form1:
Code:
' Declare Type for API call:
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128 ' Maintenance string for PSS usage
End Type
' API declarations:
Private Declare Function GetVersionEx Lib "kernel32" _
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, _
ByVal bScan As Byte, _
ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" _
(lppbKeyState As Byte) As Long
' Constant declarations:
Const VKNUMLOCK = &H90
Const VKSCROLL = &H91
Const VKCAPITAL = &H14
Const KEYEVENTFEXTENDEDKEY = &H1
Const KEYEVENTFKEYUP = &H2
Const VERPLATFORMWIN32NT = 2
Const VERPLATFORMWIN32_WINDOWS = 1
Add the following code to the Click event of the CommandButton:
Code:
Private Sub Command1_Click()
Dim o As OSVERSIONINFO
Dim NumLockState As Boolean
Dim ScrollLockState As Boolean
Dim CapsLockState As Boolean
o.dwOSVersionInfoSize = Len(o)
GetVersionEx o
Dim keys(0 To 255) As Byte
GetKeyboardState keys(0)
' NumLock handling:
NumLockState = keys(VKNUMLOCK)
If NumLockState <> True Then 'Turn numlock on
If o.dwPlatformId = VERPLATFORMWIN32WINDOWS Then '=== Win95/98
keys(VKNUMLOCK) = 1
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VERPLATFORMWIN32NT Then '=== WinNT
'Simulate Key Press
keybdevent VKNUMLOCK, &H45, KEYEVENTFEXTENDEDKEY Or 0, 0
'Simulate Key Release
keybdevent VKNUMLOCK, &H45, KEYEVENTFEXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End If
' CapsLock handling:
CapsLockState = keys(VKCAPITAL)
If CapsLockState <> True Then 'Turn capslock on
If o.dwPlatformId = VERPLATFORMWIN32WINDOWS Then '=== Win95/98
keys(VKCAPITAL) = 1
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VERPLATFORMWIN32NT Then '=== WinNT
'Simulate Key Press
keybdevent VKCAPITAL, &H45, KEYEVENTFEXTENDEDKEY Or 0, 0
'Simulate Key Release
keybdevent VKCAPITAL, &H45, KEYEVENTFEXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End If
' ScrollLock handling:
ScrollLockState = keys(VKSCROLL)
If ScrollLockState <> True Then 'Turn Scroll lock on
If o.dwPlatformId = VERPLATFORMWIN32WINDOWS Then '=== Win95/98
keys(VKSCROLL) = 1
SetKeyboardState keys(0)
ElseIf o.dwPlatformId = VERPLATFORMWIN32NT Then '=== WinNT
'Simulate Key Press
keybdevent VKSCROLL, &H45, KEYEVENTFEXTENDEDKEY Or 0, 0
'Simulate Key Release
keybdevent VKSCROLL, &H45, KEYEVENTFEXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End If
End Sub
Press the F5 key to run the program. Click the CommandButton. The state of the CAPS LOCK, the NUM LOCK, and the SCROLL LOCK keys should all be "on."
Enter your message below
Sign in or Join us (it's free).