Results 1 to 3 of 3

Thread: SendKeys turning off NumLock and CapsLock

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2001
    Location
    Chicago
    Posts
    40

    Question SendKeys turn off the NumLock, Capslock, & etc

    When using the following code, the NumLock, CapsLock, and Scroll gets Turn off.

    AppActivate IDIdentifier, False

    'Send a Ctrl-A (Select All) and Ctrl-Ins (Copy)
    SendKeys "^a"
    SendKeys "^{INSERT}"

    I would like to prevent this from happening. My alternate solution was to turn the keys on through the key_bd_event.

    However it only worked in step mode. Any ideas?

    Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte) As Long
    Public Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)

    Public Const VK_NUMLOCK = &H90
    Public Const KEYEVENTF_KEYUP = &H2
    Public Const VK_Capital = &H14


    Dim AllKeys(255) As Byte
    Dim NumLock As Byte
    Dim retVal As Long
    retVal = GetKeyboardState(AllKeys(0))
    NumLock = AllKeys(VK_NUMLOCK)

    'Set focus to ID ToolTip
    AppActivate IDIdentifier, False
    'Send a Ctrl-A (Select All) and Ctrl-Ins (Copy)
    SendKeys "^a"
    SendKeys "^{INSERT}"

    DoEvents
    If NumLock = 1 Then
    keybd_event VK_NUMLOCK, 0, 0, 0
    keybd_event VK_NUMLOCK, 0, KEYEVENTF_KEYUP, 0
    MsgBox NumLock

    End If

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    Try using the Wait subparameter of SendKeys.


    SendKeys string[, wait]

    The SendKeys statement syntax has thesenamed arguments:

    Part Description
    string Required.String expression specifying the keystrokes to send.
    Wait Optional.Boolean value specifying the wait mode. If False (default), control is returned to theprocedure immediately after the keys are sent. If True, keystrokes must be processed before control is returned to the procedure.

  3. #3
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    Instead of turning numlock back on, you could avoid using SendKeys at all. Turning numlock off is a known bug in SendKeys. You could use the key_bd_event to press the Ctrl-A and Ctrl-Ins.

    eg
    Code:
    Option Explicit
    Private Const VK_CONTROL = &H11
    Private Const VK_INSERT = &H2D
    Private Const VK_NUMLOCK = &H90
    Private Const KEYEVENTF_KEYUP = &H2
    
    Private Declare Function VkKeyScan Lib "user32" Alias "VkKeyScanA" (ByVal cChar As Byte) As Integer
    Private Declare Sub keybd_event Lib "user32.dll" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    
    Private Sub Command1_Click()
    Dim retVal As Long
    Dim VK_A As Integer
    
        AppActivate "Untitled - Notepad"
        'DoEvents
        ' find the Virtual key code for char A
        VK_A = VkKeyScan(Asc("a"))
        ' send  CTRL-A
        ' press CTRL down
        keybd_event VK_CONTROL, 0, 0, 0
        ' press A down
        keybd_event VK_A, 0, 0, 0
        ' release the A key
        keybd_event VK_A, 0, KEYEVENTF_KEYUP, 0
        ' release CTRL key
        keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
    
        ' send  CTRL-INS
        ' press CTRL down
        keybd_event VK_CONTROL, 0, 0, 0
        ' press INS down
        keybd_event VK_INSERT, 0, 0, 0
        ' release the INS key
        keybd_event VK_INSERT, 0, KEYEVENTF_KEYUP, 0
        ' release CTRL key
        keybd_event VK_CONTROL, 0, KEYEVENTF_KEYUP, 0
    
    End 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