Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    May 2002
    Location
    Florence, Oregon, USA
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Turn on the Caps Lock key in VBA (Access 2003)

    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

  2. #2
    3 Star Lounger Jim Cone's Avatar
    Join Date
    Feb 2002
    Location
    Portland, Oregon, USA
    Posts
    238
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: 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

  3. #3
    3 Star Lounger
    Join Date
    May 2002
    Location
    Florence, Oregon, USA
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Turn on the Caps Lock key in VBA (Access 2003)

    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

  4. #4
    3 Star Lounger Jim Cone's Avatar
    Join Date
    Feb 2002
    Location
    Portland, Oregon, USA
    Posts
    238
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Turn on the Caps Lock key in VBA (Access 2003)

    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)

  5. #5
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Turn on the Caps Lock key in VBA (Access 2003)

    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

  6. #6
    3 Star Lounger
    Join Date
    May 2002
    Location
    Florence, Oregon, USA
    Posts
    209
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Turn on the Caps Lock key in VBA (Access 2003)

    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

  7. #7
    3 Star Lounger
    Join Date
    Aug 2001
    Location
    Jeddah, Saudi Arabia
    Posts
    243
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Turn on the Caps Lock key in VBA (Access 2003)

    Here's a version that uses Jims list.

    Regards,
    Kevin Bell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •