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

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Turn on the Caps Lock key in VBA (Access 2003)

    Author
    Topic
    #405843

    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

    Viewing 1 reply thread
    Author
    Replies
    • #836951

      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

      • #836965

        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

        • #836975

          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)

        • #836976

          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)

      • #836966

        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

      • #837095

        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

        • #837161

          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

          • #837409

            Here’s a version that uses Jims list.

            Regards,
            Kevin Bell

          • #837410

            Here’s a version that uses Jims list.

            Regards,
            Kevin Bell

        • #837162

          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

      • #837096

        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

    • #836952

      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

    Viewing 1 reply thread
    Reply To: Turn on the Caps Lock key in VBA (Access 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: