Post

Replies

Boosts

Views

Activity

emulate mouse click
I need to emulate a left mouse click on my Mac. Before I upgraded to Big Sur 11.5, the following code worked just fine - but now it no longer does. What happened, and what's the remedy? In a related question: how does one emulate a double-click?         let position = NSPoint(100,75)         CGDisplayMoveCursorToPoint(0, position)         let source = CGEventSource.init(stateID: .hidSystemState)         let eventDown = CGEvent(mouseEventSource: source, mouseType: .leftMouseDown, mouseCursorPosition: position , mouseButton: .left)         let eventUp = CGEvent(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: position , mouseButton: .left)         eventDown?.post(tap: .cghidEventTap)         usleep(50_000)         eventUp?.post(tap: .cghidEventTap)
19
0
5.3k
Jul ’21
Emulate double leftclick on MacOS
I've built an "interactive laserpointer" iPhone app that positions the cursor at the point of aim of the iPhone, called Ultimatepointer - or Upoint for short. For this, I need a UpointDriver on the Mac to receive the cursor coordinates and any "mouse-click" the iPhone app sends over, and execute them on the Mac. In an earlier thread (https://developer.apple.com/forums/thread/685618?answerId=682733022#682733022) the problems arising from the introduction of Big Sur 11.5 were discussed. It turns out you have to de-check and re-check the macOS app's permissions in Preferences - Security and Privacy - Accessibility every time you recompile. While that problem was solved, I'm still left with the problem of how to effect a double-click. For the Windows-equivalent of the Driver, it's easy: just effect two clicks within a short timeframe. For Mac, the solution eludes me. It's not as simple as effecting two mouseDown-mouseUp sequences, or two mouseDown and then a mouseUp.... I'm stumped. Any suggestions? The code I use is below. Note that I'm using a Dispatch because the function is sometimes called by the app from a different thread, and I don't want to run into trouble with the UI thread. If you're certain the function only gets called from the main thread, Dispatch is not needed. func leftClick() {     DispatchQueue.main.async {         let source = CGEventSource.init(stateID: .hidSystemState)         let position = self.correctedCursorPosition // an internal variable holding the intended position of the click         let eventDown = CGEvent(mouseEventSource: source, mouseType: .leftMouseDown, mouseCursorPosition: position , mouseButton: .left)         let eventUp = CGEvent(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: position , mouseButton: .left)         eventDown?.post(tap: .cghidEventTap)         eventUp?.post(tap: .cghidEventTap)     } }
9
0
1.4k
Jul ’21