Results 1 to 8 of 8
  1. #1
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928

    Set NumLock

    Found myself using numeric keypad when NumLock was off. Of course I wasn't aware of that and was going nuts over why cursor was jumping around and couldn't enter value. Doh!



    So went hunting for code to programmatically control NumLock state. Found two, neither fully works.
    http://www.tek-tips.com/viewthread.c...9487&page=1987
    http://c85.cemi.rssi.ru/Access/Queri...l.asp?TipID=36

    Debug shows that the state value is changed from off to on but the keyboard isn't actually changed. The NumLock light stays off and key entry behavior isn't altered. Here is how I implemented code from the second link.
    Code:
    'following used with code to set NumLock on keyboard
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte) As Long
     
    Private Sub SetKeyState(intKey As Integer, fTurnOn As Boolean)
    ' Retrieve the keyboard state, set the particular
    ' key in which you're interested, and then set
    ' the entire keyboard state back the way it
    ' was, with the one key altered.
    Dim abytBuffer(0 To 255) As Byte
    GetKeyboardState abytBuffer(0)
    abytBuffer(intKey) = CByte(Abs(fTurnOn))
    SetKeyboardState abytBuffer(0)
    End Sub
     
    Private Sub Form_Load()
    ' Return or set the Numlock toggle.
    Call SetKeyState(vbKeyNumlock, True)
    End Sub
    Found another, SendKeys works but it wants to add record to my UserComments table. That's weird but it does toggle the NumLock. Problem is if NumLock is on it will be turned off.
    http://www.ozgrid.com/forum/showthre...t=13239&page=1

    Well, this works, and with the True argument no longer writes record to table.
    Code:
    Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
     
    Private Sub Form_Load()
    If GetKeyState(vbKeyNumlock) = 1 Then
        'set the Numlock on
        SendKeys "NumLock", True
    End If
    End Sub
    I spoke too soon. Additional testing shows that this still does not work as expected. My goal was to make sure the NumLock is turned on when attempting entry into a particular textbox. So I changed this code to the GotFocus event for that control and everything fell apart. Even tries to write that record again. I give up.

    What I really wanted was code that would detect key press from the numeric keypad but everything I read says VBA can't distinguish whether the ASCII is from keyboard or keypad. In fact, when NumLock is off, the number 5 on keypad sends no ASCII at all.
    Last edited by June7; 06-30-2011 at 01:58 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  2. #2
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Sorry I have no solution but want to subscribe to this thread in case anyone else has the solution. (Have found no way of subscribing without posting.)

    It has to be there somewhere; Windows manipulates the NumLock property at startup. There has to be an API for this.

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    I tried my SendKeys method again and now not getting anything to happen. These links do show using API.
    Last edited by June7; 07-01-2011 at 02:54 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    drexasaurus's Avatar
    drexasaurus is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    Santa Ana, CA
    Posts
    60
    Don't know how hard it is to control this behavior, and may try to play around with the API when I have more free time, but if you can get the keyboard successfully, even without changing it with that API, you could do something less elegant, like give a pop-up (annoying), play a sound, change the status bar, or create a custom pop-up that dissapears by itself in a few seconds as a less irritating option. At least users would know that their numlock is off. If you wanted to be fancy, you could even record whether the user wants num-lock messages or not.

  5. #5
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    Do I get a medal for this?

    Code:
     
    Private Sub Text2_Enter()
    If GetKeyState(vbKeyNumlock) = 0 Then
    'set the Numlock on
    SendKeys "{NUMLOCK}", True
    End If
    End Sub
    Don't know what it does with the keyboard light as I don't have one.

    Goes crazy in debug mode as SendKeys sends to the VBA code.

    PS Yes you have to declare the API!
    Last edited by Rod; 07-05-2011 at 06:47 AM. Reason: Forgot about the API

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Quote Originally Posted by Rod View Post
    Sorry I have no solution but want to subscribe to this thread in case anyone else has the solution. (Have found no way of subscribing without posting.)
    Rod...you will find "Subscribe to this Thread" under the Thread Tools at the top of each thread.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,928
    Thanks Rod, but that still doesn't work for me. The SendKeys code does change the Numlock indicator in lower right corner of app but the keyboard state and behavior is not changed. I include a Debug.Print GetKeyState(vbKeyNumlock) before and after and I do see changed values. Also tried the other code again, still can't get consistent results. Maybe has something to do with manually clicking Numlock off and on for testing - which is the whole point of this, if user accidentally clicks Numlock off I want code to turn it on.

    Now just a matter of curiosity, not really worth spending more business time on this.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    Rod is offline Expert
    Windows Vista Access 2007
    Join Date
    Jun 2011
    Location
    Metro Manila, Philippines
    Posts
    679
    RuralGuy: Thanks. Yes found it.
    June7: Works for me, everytime, including mixtures of manual and programmatic setting. Strange. OK, we leave it there.

Please reply to this thread with any new information or opinions.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums