Results 1 to 3 of 3

Thread: SendKeys causes NumLock to turn off

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Location
    Columbus, Ohio
    Posts
    4

    Angry SendKeys causes NumLock to turn off

    When using SendKeys to turn an Enter keystroke into a Tab keystroke (SendKeys "{TAB}"), NumLock will briefly turn off. When users are keying rapidly, they enter data into a field and hit the Enter key. The focus moves to the next field, but if they enter something else on the numeric keypad, it is interpreted as PageUp, PageDown, or whatever the number key represents without NumLock.

    There is a database call on LostFocus that may take 1 or 2 seconds, and NumLock will not turn back on until this operation completes. However, if I actually use the Tab key, this delay still occurs, but NumLock will not turn off.

    I have tried using the Wait option after SendKeys, but it does not make a difference. I have also inserted DoEvents at various places without success.

    This happens only on Windows NT and Windows 2000. Windows 95 is not affected, and I have not tried it on 98 or XP.

  2. #2
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    This is a know issue with Sendkeys - use this fix:

    VB Code:
    1. '\\ For saving and resetting caps/scroll and numlock keys
    2. Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    3. Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    4. Private Const VK_NUMLOCK = &H90
    5. Private Const VK_SCROLL = &H91
    6. Private Const VK_CAPITAL = &H14
    7. Private Const KEYEVENTF_EXTENDEDKEY = &H1
    8. Private Const KEYEVENTF_KEYUP = &H2
    9.  
    10. Public Sub SafeSendkeys(ByVal keys As String, Optional bWait As Variant)
    11.  
    12. '\\ Save the special keys' state...
    13. Dim keysBefore(0 To 255) As Byte
    14. Dim keysAfter(0 To 255) As Byte
    15.  
    16. Call GetKeyboardState(keysBefore(0))
    17.  
    18. If IsMissing(bWait) Then
    19.     Call SendKeys(keys, False)
    20. Else
    21.     Call SendKeys(keys, CBool(bWait))
    22. End If
    23.  
    24. Call GetKeyboardState(keysAfter(0))
    25. '\\ "Press" numlock if it has changed
    26. If keysBefore(VK_NUMLOCK) <> keysAfter(VK_NUMLOCK) Then
    27.     '\\ key down
    28.     Call keybd_event(VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY, 0)
    29.     '\\ key up = 1 key press
    30.     Call keybd_event(VK_NUMLOCK, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
    31. End If
    32. '\\ "Press" caps if it has changed
    33. If keysBefore(VK_CAPITAL) <> keysAfter(VK_CAPITAL) Then
    34.     '\\ key down
    35.     Call keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY, 0)
    36.     '\\ key up = 1 key press
    37.     Call keybd_event(VK_CAPITAL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
    38. End If
    39. '\\ "Press" scroll lock if it has changed
    40. If keysBefore(VK_SCROLL) <> keysAfter(VK_SCROLL) Then
    41.     '\\ key down
    42.     Call keybd_event(VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY, 0)
    43.     '\\ key up = 1 key press
    44.     Call keybd_event(VK_SCROLL, &H45, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0)
    45. End If
    46.  
    47. End Sub

    Hope this helps,
    Duncan
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  3. #3
    New Member
    Join Date
    Jan 2023
    Posts
    1

    Smile Re: SendKeys causes NumLock to turn off

    Thanks a lot for this very useful VBA Sub !

Posting Permissions

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



Click Here to Expand Forum to Full Width