Mouse click using code

  1. 3 months ago
    Edited 3 months ago

    Hello everyone, I am currently using a mouse click code that I found while browsing the forums. The code is awesome but for some reason, the click isn't recognized sometimes. Maybe 5 out of 10 times the click will happen. Any suggestions?

    //declare necessary functions, etc
    Declare sub CFRelease lib "Cocoa" ( ref as ptr )
    
    'CGEventRef CGEventCreate (
    'CGEventSourceRef source
    ');
    Declare Function CGEventCreate lib "ApplicationServices" (CGEventSourceRef as ptr) as Ptr
    
    'CGPoint CGEventGetLocation (
    'CGEventRef event
    ');
    Declare Function CGEventGetLocation lib "ApplicationServices" (CGEventRef as Ptr) as CGPoint
    
    'CGEventRef CGEventCreateMouseEvent (
    'CGEventSourceRef source,
    'CGEventType mouseType,
    'CGPoint mouseCursorPosition,
    'CGMouseButton mouseButton
    ');
    
    '/* mouse events */
    '
    '#define NX_LMOUSEDOWN         1    /* left mouse-down event */
    '#define NX_LMOUSEUP               2    /* left mouse-up event */
    '#define NX_RMOUSEDOWN         3    /* right mouse-down event */
    '#define NX_RMOUSEUP               4    /* right mouse-up event */
    '#define NX_MOUSEMOVED          5    /* mouse-moved event */
    '#define NX_LMOUSEDRAGGED     6    /* left mouse-dragged event */
    '#define NX_RMOUSEDRAGGED     7    /* right mouse-dragged event */
    '#define NX_MOUSEENTERED        8    /* mouse-entered event */
    '#define NX_MOUSEEXITED           9    /* mouse-exited event */
    Declare Function CGEventCreateMouseEvent lib "ApplicationServices" (CGEventSourceRef as ptr, _
    CGEventType as UInt32, position as CGPoint, CGMouseButton as UInt32) as ptr
    //////////
    'using the other constants above in place of these allows for right clicking, simulating mouse moved 
    'events, etc
    //////////
    const kCGEventLeftMouseDown = 1
    const kCGEventLeftMouseUp = 2
    
    'void CGEventPost (
    'CGEventTapLocation tap,
    'CGEventRef event
    ');
    Declare sub CGEventPost lib "ApplicationServices" (CGEventTapLocation as UInt32, CGEventRef as ptr)
    const tapLocation = 0 
    
    'CGEventSourceRef CGEventSourceCreate (
    'CGEventSourceStateID sourceState
    ');
    Declare Function CGEventSourceCreate lib "ApplicationServices" (CGEventSourceStateID as UInt32) as ptr
    const kCGEventSourceStatePrivate = -1
    
    //now for the clicking
    //get mouse location
    dim CGMouseEvent as Ptr = CGEventCreate(nil)
    dim mouseLoc as CGPoint = CGEventGetLocation(CGMouseEvent)
    CFRelease(CGMouseEvent)
    
    //create event source
    dim sourceRef as ptr = CGEventSourceCreate(kCGEventSourceStatePrivate)
    
    //press down
    dim clickMouse as ptr = CGEventCreateMouseEvent(sourceRef, kCGEventLeftMouseDown, mouseLoc, 0)
    CGEventPost(tapLocation, clickMouse)
    CFRelease(clickMouse)
    
    //release mouse
    dim releaseMouse as ptr = CGEventCreateMouseEvent(sourceRef, kCGEventLeftMouseUp, mouseLoc, 0)
    CGEventPost(tapLocation, clickMouse)
    CFRelease(releaseMouse)

    Yes, I observed the same. Somehow, it seems the software click is too short, as compared to the actual hardware one, which takes significantly more time.

    The problem on a web site is if for instance you click twice on a button, it may trigger it's action twice. Depends on the responsiveness of the site, and the quality of your connection.

  2. Michel B

    Apr 14 Pre-Release Testers RubberViews.com

    What are you trying to click ?

  3. I'll be doing all of the clicks inside of an HTMLViewer.

  4. Michel B

    Apr 14 Pre-Release Testers RubberViews.com

    Some sites are kind of finicky about software clicks. I am not surprised you have problems with a software click.

    There is no workaround.

  5. Emile S

    Apr 14 Europe (France, Strasbourg)

    Can you share an example of a need for a Mouse Click in HTMLViewer ?

  6. I should add that if I call the "leftmouseclick" method twice, I have a better result. Maybe 7/10 clicks. Like this

    Leftmouseclick
    Leftmouseclick
  7. Emile S

    Apr 14 Europe (France, Strasbourg)

    @Marcus M leftmouseclick

    Isn’t this a standard Mouse Click ?

  8. Michel B

    Apr 14 Pre-Release Testers Answer RubberViews.com
    Edited 3 months ago

    Yes, I observed the same. Somehow, it seems the software click is too short, as compared to the actual hardware one, which takes significantly more time.

    The problem on a web site is if for instance you click twice on a button, it may trigger it's action twice. Depends on the responsiveness of the site, and the quality of your connection.

  9. Emile the code I previously posted is in a method called "Leftmouseclick". It's not a need, more of me testing certain things out. As such as having the mouse move and click with code. The example that I'm trying is an automated tweet on my Twitter.

  10. Michel, you're right. I also noticed that adding a delay helps the odds.

  11. Michel B

    Apr 14 Pre-Release Testers RubberViews.com

    @Marcus M Emile the code I previously posted is in a method called "Leftmouseclick". It's not a need, more of me testing certain things out. As such as having the mouse move and click with code. The example that I'm trying is an automated tweet on my Twitter.

    I posted the method I used in the forum, but I am now at a loss to find it back. At any rate, it used exactly the same calls to the Cocoa framework, so it would be no point to change what you are doing.

or Sign Up to reply!