"Caps Lock/Unlock"

D

DEG

Hi Guys and Gals,

Looking for code to lock and unlock caps that will work in both XP and
Vista....anything out there??

Don
 
R

royUK

This Function checks if caps lock is on


Code:
--------------------
Option Explicit
Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

Private Sub x()
Dim lngCapsStatus As Long

lngCapsStatus = GetKeyState(vbKeyCapital)
If lngCapsStatus = 1 Then
MsgBox "The Caps Lock Is On"
Else
MsgBox "The Caps Lock Is Off"
End If
End Sub
--------------------


The following code can be used to switch Caps Lock on and off


O
Code:
--------------------
ption Explicit

Private Const VER_PLATFORM_WIN32_NT = 2
Private Const VER_PLATFORM_WIN32_WINDOWS = 1
Private Const VK_CAPITAL = &H14
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2


Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
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

Public Sub ToggleCapsLock(bTurnOn As Boolean)

'To turn capslock on, set bTurnOn to true
'To turn capslock off, set bTurnOn to false

Dim bytKeys(255) As Byte
Dim bCapsLockOn As Boolean

'Get status of the 256 virtual keys
GetKeyboardState bytKeys(0)

bCapsLockOn = bytKeys(VK_CAPITAL)
Dim typOS As OSVERSIONINFO

If bCapsLockOn <> bTurnOn Then 'if current state <>
'requested state

If typOS.dwPlatformId = _
VER_PLATFORM_WIN32_WINDOWS Then '=== Win95/98

bytKeys(VK_CAPITAL) = 1
SetKeyboardState bytKeys(0)

Else '=== WinNT/2000

'Simulate Key Press
keybd_event VK_CAPITAL, &H45, _
KEYEVENTF_EXTENDEDKEY Or 0, 0
'Simulate Key Release
keybd_event VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY _
Or KEYEVENTF_KEYUP, 0
End If
End If


End Sub

Sub switchOn()
ToggleCapsLock (True)
End Sub

Sub switchOff()
ToggleCapsLock (False)
End Sub
 
D

DEG

Hi guys,

Tried to thank you both yesterday for the quick replies but MSN wouldn't
take the reply...so...better late than never...thanks a bunch. Mike your
method worked out great for what I had to do as I didn't have to really lock
the caps, just take data from a combobox and cap it when it was transferred
to the main sheet. But I thank you both...lots of good info...

Have a great weekend,

Don
 
Top