Mouse click using code

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?

[code]//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)[/code]

What are you trying to click ?

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

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

There is no workaround.

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

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

Leftmouseclick Leftmouseclick

Isn’t this a standard Mouse Click ?

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.

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.

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

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.