Results 1 to 6 of 6

Thread: Generating Mouse Events.

  1. #1
    Guest

    Exclamation Anyone know how to Generate Mouse Events?

    I can get VB to move the cursor to a location on the screen, but how can I get it to click there? I have tried SendInput, but I don't know how to use it. Can someone tell me how to use SendInput or another method on how to click somewhere.

    Thanks.

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    You Should use the Mouse_Event API


    Code:
    Private Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    dwflags is constant for the type of mouse event you want, here they all are

    Code:
    Private Const MOUSEEVENTF_ABSOLUTE = &H8000 
    Private Const MOUSEEVENTF_LEFTDOWN = &H2 
    Private Const MOUSEEVENTF_LEFTUP = &H4 
    Private Const MOUSEEVENTF_MIDDLEDOWN = &H20 
    Private Const MOUSEEVENTF_MIDDLEUP = &H40 
    Private Const MOUSEEVENTF_MOVE = &H1 
    Private Const MOUSEEVENTF_RIGHTDOWN = &H8 
    Private Const MOUSEEVENTF_RIGHTUP = &H10
    dx and dy are the mouse coords in pixels (on the screen, not for the window) and you can just set the last 2 parameters to 0

  3. #3
    Guest
    I just tried that (when you told it to me). Is there something I am supposed to do rather than just this:

    Code:
    mouse_event MOUSEEVENTF_LEFTDOWN, 475, 50, 0, 0
    mouse_event MOUSEEVENTF_LEFTUP, 475, 50, 0, 0
    
    'the second one is for the mouse to be released.
    If I have done something wrong, please tell me.

  4. #4
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    I'm not sure, I've never actually used it, try putting a doevents between the calls.

  5. #5
    Guest
    Do you know any other ways?

    I've tried putting DoEvents in and it doesn't work. I've also used the GetLastError thing, and it returns "0".

    This also happens with SendInput. Nothing happens and it returns 0. Have I got a bad DLL or something?

  6. #6
    Guest

    mouse_event

    i also had same problems with mouse_event.

    solutions:

    use "SetCursorPos" to move the mouse to destination
    use "mouse_event" to fire the event at that pos.

    when you call "mouse_event", DO NOT specify,
    'dx' and 'dy', ie. only use the first parameter to
    specify the event and specify last 4 parameters as 0.

    mousedown and mouseup are directly available.
    to send a CLICK use: (simple bitwise OR)

    Private Const MOUSEEVENTF_LEFTDOWN = &H2
    Private Const MOUSEEVENTF_LEFTUP = &H4
    mouse_event MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP, 0&, 0&, 0&, 0&

    '---- or ----

    Private Const MOUSEEVENTF_LEFTDOWN = &H2
    Private Const MOUSEEVENTF_LEFTUP = &H4
    Private Const MOUSEEVENTF_LEFTCLICK = MOUSEEVENTF_LEFTDOWN Or MOUSEEVENTF_LEFTUP

    mouse_event MOUSEEVENTF_LEFTCLICK, 0&, 0&, 0&, 0&

    to send a DOUBLE CLICK: (send 2 single clicks)
    (this will work regardless of the system setting for mouse double click interval)

    mouse_event MOUSEEVENTF_LEFTCLICK, 0&, 0&, 0&, 0&
    mouse_event MOUSEEVENTF_LEFTCLICK, 0&, 0&, 0&, 0&

    and same applies for middle/right mouse buttons.
    NOTE: you do not need 'doevents' for this.



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