Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Tests to see if CAPSLOCK is on and allows you to t...
Author:
JD
Submitted:
5/15/2003
Version:
VB6
Compatibility:
VB6
Category:
Windows API
Views:
15354
Tests to see if CAPSLOCK is on and allows you to turn it off or on.
Declarations:
'ADD A COMMAND BUTTON TO A FORM CALLED COMMAND1 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 ' 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 Private Declare Function GetKeyState Lib _ "user32" (ByVal nVirtKey As Long) As Integer
Code:
Public Function CapsLockOn() As Boolean Dim iKeyState As Integer iKeyState = GetKeyState(vbKeyCapital) CapsLockOn = (iKeyState = 1 Or iKeyState = -127) End Function Public Sub ToggleCapsLock(TurnOn As Boolean) 'To turn capslock on, set turnon to true 'To turn capslock off, set turnon 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 <> TurnOn Then 'if current state <> 'requested stae 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 Private Sub Command1_Click() Dim mres As String If CapsLockOn = False Then mres = MsgBox("Capslock is OFF. Would you like to turn it on?", vbYesNo + vbQuestion, "CAPSLOCK STATUS") If mres = vbYes Then ToggleCapsLock (True) End If End If If CapsLockOn = True Then mres = MsgBox("Capslock is ON. Would you like to turn it off?", vbYesNo + vbQuestion, "CAPSLOCK STATUS") If mres = vbYes Then ToggleCapsLock (False) End If End If End Sub
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement