Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    send keys? (vb6)

    Just out of curiosity, I know that the 'sendkeys' command can be used to send certain keys to whatever program you want but what about mouse clicks. Is there a way to send a right/left mouse click??

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: send keys? (vb6)

    Sure. Here is some sample code that I use in Excel:

    Option Explicit

    Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal _
    bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
    Declare Sub mouse_event Lib "user32.dll" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy _
    As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long

    Const VK_LWIN = &H5B
    Const KEYEVENTF_KEYUP = &H2
    Const VK_APPS = &H5D
    Const MOUSEEVENTF_ABSOLUTE = &H8000
    Const MOUSEEVENTF_LEFTDOWN = &H2
    Const MOUSEEVENTF_LEFTUP = &H4
    Const MOUSEEVENTF_MIDDLEDOWN = &H20
    Const MOUSEEVENTF_MIDDLEUP = &H40
    Const MOUSEEVENTF_MOVE = &H1
    Const MOUSEEVENTF_RIGHTDOWN = &H8
    Const MOUSEEVENTF_RIGHTUP = &H10
    Const MOUSEEVENTF_WHEEL = &H80
    Const MOUSEEVENTF_XDOWN = &H100
    Const MOUSEEVENTF_XUP = &H200
    Const WHEEL_DELTA = 120
    Const XBUTTON1 = &H1
    Const XBUTTON2 = &H2
    Const VK_ENTER = &HD
    Const VK_MENU = &H12
    Const VK_SPACE = &H20
    Const VK_A = &H41
    Const VK_B = &H42
    Const VK_C = &H43
    Const VK_D = &H44
    Const VK_E = &H45
    Const VK_F = &H46
    Const VK_G = &H47
    Const VK_H = &H48
    Const VK_I = &H49
    Const VK_J = &H4A
    Const VK_K = &H4B
    Const VK_L = &H4C
    Const VK_M = &H4D
    Const VK_N = &H4E
    Const VK_O = &H4F
    Const VK_P = &H50
    Const VK_Q = &H51
    Const VK_R = &H52
    Const VK_S = &H53
    Const VK_T = &H54
    Const VK_U = &H55
    Const VK_V = &H56
    Const VK_W = &H57
    Const VK_X = &H58
    Const VK_Y = &H59
    Const VK_Z = &H5A
    Const VK_LeftArrow = &H25
    Const VK_UpArrow = &H26
    Const VK_RightArrow = &H27
    Const VK_DownArrow = &H28
    Dim bKey As Byte
    Dim iCount As Integer

    Sub Test()
    Dim retval As Long ' return value
    AppActivate "YourApplications Caption"
    Maximize
    ' move the mousepointer
    retval = SetCursorPos(520, 350)
    mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
    mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
    SendKeys "^{home}"
    retval = SetCursorPos(520, 350)
    mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
    mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
    End Sub

    Sub SendAKey(hKey As Byte)
    keybd_event hKey, 0, 0, 0 ' press the key
    keybd_event hKey, 0, KEYEVENTF_KEYUP, 0 ' release the key
    End Sub

    Sub Maximize()
    keybd_event VK_MENU, 0, 0, 0 ' press Alt
    SendAKey VK_SPACE
    keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0 ' release Alt
    SendAKey VK_X
    End Sub
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    2 Star Lounger
    Join Date
    Jul 2001
    Posts
    132
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: send keys? (vb6)

    I am sorry, I am not really sure what I am supposed to do with the example code and for that matter, what it even does.
    Could somebody please clear my head?

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: send keys? (vb6)

    OK.

    Lets assume this is Excel (sorry, no VB expert talking here <g>).
    Paste all of the code into a normal module.

    Look at the sub called Test.
    Change the Appactivate command to match the application's name that you want to send mouseclicks to.
    Now run test. You should see the mouse move and do a click inside the application's window.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

Posting Permissions

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