5

I'm Trying to Simulate a left click in Swift Xcode. I've found this code online but it moves the cursour, Is there a way to keep the cursor in it's current spot and click?

import Foundation

let source = CGEventSource.init(stateID: .hidSystemState)
let position = CGPoint(x: 75, y: 100)
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)

//eventDown?.postToPid(71028)
usleep(500_000)
//eventUp?.postToPid(71028)
eventUp?.post(tap: .cghidEventTap)

2 Answers 2

2

I think the problem in your current code is the following line :

let position = CGPoint(x: 75, y: 100)

You clearly assign a new position to your mouse cursor. You should use the mouseLocationOutsideOfEventStream, like this :

let position = self.window.mouseLocationOutsideOfEventStream as CGPoint

Be aware that this returns a NSPoint that you may have to cast as a CGPoint (like I did).

As you are working with a Playground, here is a much detailed sample :

import Cocoa
import PlaygroundSupport

// You need to create a view to provide to your current PlaygroundPage
let currentView = NSView()
PlaygroundPage.current.liveView = currentView

// Here you are able to get your cursor current location
let cursorPosition = currentView.window!.mouseLocationOutsideOfEventStream as CGPoint

// Then you are able to run your code below using the cursorPosition variable
let source = CGEventSource.init(stateID: .hidSystemState)
let eventDown = CGEvent(mouseEventSource: source, mouseType: .leftMouseDown, mouseCursorPosition: cursorPosition , mouseButton: .left)
let eventUp = CGEvent(mouseEventSource: source, mouseType: .leftMouseUp, mouseCursorPosition: cursorPosition , mouseButton: .left)


eventDown?.post(tap: .cghidEventTap)

//eventDown?.postToPid(71028)
usleep(500_000)
//eventUp?.postToPid(71028)
eventUp?.post(tap: .cghidEventTap)
Sign up to request clarification or add additional context in comments.

5 Comments

Would you mind showing me how to implement this in my current code? Thanks in advance!
Of course, I think you only have to replace your let position = CGPoint(x: 75, y: 100) with mine (sorry, I will edit as I named it location instead of position like you did). I think this will do the trick, but I haven't tested it in a playground to be honest
I've applied the change to my code but I'm getting the error: Cannot find 'self' in scope.
Oh, indeed, are you working with a Playground here ? Or in a single-file script without any class ? (self is used when you use a class or an object, here you don't) If you are working with a Playground, there are several imports to do to get the current window, just tell me and I will update my answer :)
I've edited my answer above to a full detailed example, this should work the way you wanted to
0

You should disable application sandboxing in order to make your code work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.