ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   "Caps Lock/Unlock" (https://www.excelbanter.com/excel-programming/428253-caps-lock-unlock.html)

DEG

"Caps Lock/Unlock"
 
Hi Guys and Gals,

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

Don

royUK[_117_]

"Caps Lock/Unlock"
 

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


--------------------


--
royUK

Hope that helps, RoyUK
For tips & examples visit my 'web site' (http://www.excel-it.com/)
------------------------------------------------------------------------
royUK's Profile: http://www.thecodecage.com/forumz/member.php?userid=15
View this thread: http://www.thecodecage.com/forumz/sh...ad.php?t=94967


Mike H

"Caps Lock/Unlock"
 
Hi,

Have a look here on how to do it but it seems like a lot of bother to me,
why don't you simple convert to upper & lower case as required using UCASE or
LCASE in you code.

http://support.microsoft.com/kb/177674/EN-US/

Mike

"DEG" wrote:

Hi Guys and Gals,

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

Don


DEG

"Caps Lock/Unlock"
 
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

"Mike H" wrote:

Hi,

Have a look here on how to do it but it seems like a lot of bother to me,
why don't you simple convert to upper & lower case as required using UCASE or
LCASE in you code.

http://support.microsoft.com/kb/177674/EN-US/

Mike

"DEG" wrote:

Hi Guys and Gals,

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

Don



All times are GMT +1. The time now is 11:44 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com