How to Toggle CapsLock (in two ways)
Posted on January 4, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | 'System & API - How to Toggle CapsLock (in two ways) 'Method 1: Option 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 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 Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer Public Function CapsLockOn() As Boolean 'This function will tell us whether the capslock is on or off Dim iKeyState As Integer iKeyState = GetKeyState(vbKeyCapital) CapsLockOn = (iKeyState = 1 Or iKeyState = -127) End Function Public Sub ToggleCapsLock(ByVal 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 typOS.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS Then 'Win9x 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 Private Sub Main() If Not CapsLockOn Then If MsgBox("Capslock is OFF. Would you like to turn it on?", vbYesNo + vbQuestion, "CapsLock") = vbYes Then Call ToggleCapsLock(True) End If Else If MsgBox("Capslock is ON. Would you like to turn it off?", vbYesNo + vbQuestion, "CapsLock") = vbYes Then Call ToggleCapsLock(False) End If End If End Sub 'Method 2: 'This method will change the state of Num lock internally but will not change status on keyboard Declare Sub GetKeyboardState Lib "user32" (lpKeyState As Any) Declare Sub SetKeyboardState Lib "user32" (lpKeyState As Any) Public Const VK_CAPITAL = &H14 Private Sub Main() ReDim KeyboardBuffer(256) As Byte GetKeyboardState KeyboardBuffer(0) If KeyboardBuffer(VK_CAPITAL) And 1 Then KeyboardBuffer(VK_CAPITAL) = 0 Else KeyboardBuffer(VK_CAPITAL) = 1 End If SetKeyboardState KeyboardBuffer(0) End Sub |