Is there a way in Access2003 VBA code to toggle the Caps Lock key on a standard keyboard on and off? I want it to be the automatic equivalent of my manually pressing the key.
Thank you,
John
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Turn on the Caps Lock key in VBA (Access 2003)
John,
The following may be more trouble than it is worth as the keyboard indicator light does not change,
when the keystate changes…
Tested on WindowsXP/Excel 2002
‘—————————————-
Declare Function SetKeyboardState Lib “User32” (kbArray As Byte) As Long
‘Number Lock is &H90
‘Caps Lock is &H14
Sub ToggleKeys()
Dim KeyState(0 To 255) As Byte
‘ KeyState(&H14) = 1 ‘for on
KeyState(&H14) = 0 ‘for off
SetKeyboardState KeyState(0)
End Sub
‘——————————————
Regards,
Jim Cone
San Francisco, CA
Thanks Jim,
Where in the world do you find these kind of VBA techniques? I have Visual Basic for Applications 5 in 21 Days (1213 pages) from Sams publishing, Teach Yourself Access 2000 Programming in 24 Hours also from Sams, and I spent hours going through the Access 2003 VBA Help system but could find no mention of how to control the keyboard. I almost assumed it couldn’t be done.
Thanks again, John Littell, Florence OR
John,
You pay $60.00 for a Windows API book and stick it on the shelf.
Every two or three years when you need it, you look something up and
find out you still don’t understand it.
I did make up a crib sheet of the virtual keycodes that tells you what number to use
to identify a key. You may find it of some use.
Regards,
Jim Cone
San Francisco, CA
(with relatives in Eugene, OR)
John,
You pay $60.00 for a Windows API book and stick it on the shelf.
Every two or three years when you need it, you look something up and
find out you still don’t understand it.
I did make up a crib sheet of the virtual keycodes that tells you what number to use
to identify a key. You may find it of some use.
Regards,
Jim Cone
San Francisco, CA
(with relatives in Eugene, OR)
Thanks Jim,
Where in the world do you find these kind of VBA techniques? I have Visual Basic for Applications 5 in 21 Days (1213 pages) from Sams publishing, Teach Yourself Access 2000 Programming in 24 Hours also from Sams, and I spent hours going through the Access 2003 VBA Help system but could find no mention of how to control the keyboard. I almost assumed it couldn’t be done.
Thanks again, John Littell, Florence OR
John,
Attached is a zip file containing a module I use to simulate keystrokes using the keybd_event API call. I use it instead of SendKeys. Unzip it and add the module file to your project.
The call to press the Caps Lock key is:
Call PressVirtualKey(keyCapsLock)
Remember that this is the equivalent of pressing the key so the function will toggle. i.e, if Caps Lock is already on, this will turn it off. Use the GetLockStatus function in the module to determine if you need to.
It’s a bit more complex than Jim’s solution but it will turn the keyboard lights on and off.
Jim’s comments on buying an API book, keeping it on the shelf and returning to it when you are hopelessly lost sounds exactly right. All hail the mighty Dan Appleman is all I say.
I hope this helps.
Regards,
Kevin Bell
My thanks to Jim and Kevin,
Jim: Ah! A creater of lists. I fully understand this compulsion. I have entire directories full of lists. Lists of what? Anthing listable. Your routine worked perfectly. In fact it worked more that I expected. It seems that it overrides the Caps Lock key so that pressing the physical key does not turn the code set setting off. You have to set it off using code. I assume this is correct.
Kevin: Your code is going to require more study on my part. For your “enumKeys” declaration it’s a shame you can’t simply include Jim’s list.
John Littell, Florence, OR
My thanks to Jim and Kevin,
Jim: Ah! A creater of lists. I fully understand this compulsion. I have entire directories full of lists. Lists of what? Anthing listable. Your routine worked perfectly. In fact it worked more that I expected. It seems that it overrides the Caps Lock key so that pressing the physical key does not turn the code set setting off. You have to set it off using code. I assume this is correct.
Kevin: Your code is going to require more study on my part. For your “enumKeys” declaration it’s a shame you can’t simply include Jim’s list.
John Littell, Florence, OR
John,
Attached is a zip file containing a module I use to simulate keystrokes using the keybd_event API call. I use it instead of SendKeys. Unzip it and add the module file to your project.
The call to press the Caps Lock key is:
Call PressVirtualKey(keyCapsLock)
Remember that this is the equivalent of pressing the key so the function will toggle. i.e, if Caps Lock is already on, this will turn it off. Use the GetLockStatus function in the module to determine if you need to.
It’s a bit more complex than Jim’s solution but it will turn the keyboard lights on and off.
Jim’s comments on buying an API book, keeping it on the shelf and returning to it when you are hopelessly lost sounds exactly right. All hail the mighty Dan Appleman is all I say.
I hope this helps.
Regards,
Kevin Bell
John,
The following may be more trouble than it is worth as the keyboard indicator light does not change,
when the keystate changes…
Tested on WindowsXP/Excel 2002
‘—————————————-
Declare Function SetKeyboardState Lib “User32” (kbArray As Byte) As Long
‘Number Lock is &H90
‘Caps Lock is &H14
Sub ToggleKeys()
Dim KeyState(0 To 255) As Byte
‘ KeyState(&H14) = 1 ‘for on
KeyState(&H14) = 0 ‘for off
SetKeyboardState KeyState(0)
End Sub
‘——————————————
Regards,
Jim Cone
San Francisco, CA
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications