How to simulate mouse click from Mac App to other Application
Asked Answered
R

4

25

I am trying to simulate mouse click on iphone simulator from macos App for that I am using CGEvents .

the process id is 33554 for iPhone simulator

let point = CGPoint(x: 500  , y:300)
let eventMouseDown = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: point, mouseButton: .left)
let eventMouseUp = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: point, mouseButton: .left)
eventMouseDown?.postToPid(33554)
eventMouseUp?.postToPid(33554)

I have also noticed that It simulates mouse click when ios simulator window is focused and only works for this toolbar but not for the simulator for example if I change CGPoint to (0,30) it will click on Simulator option

enter image description here

but when I am giving CGPoints to click app inside iOS Simulator its not working enter image description here

However, I am able to post Keyboard Event to Simulator using

let keyboardDown = CGEvent(keyboardEventSource: nil, virtualKey: 6, keyDown: true)
let keyboardUp = CGEvent(keyboardEventSource: nil, virtualKey: 6, keyDown: false)
keyboardDown?.postToPid(33554)
keyboardUp?.postToPid(33554)
Rubiaceous answered 28/1, 2017 at 10:18 Comment(2)
Do you remember, that the y-axis is reversed on macOS?Attenborough
Did you find a way to get this working? I have exactly the same issue. Keyboard events work like a charm but mouse does not.Smedley
P
15

First of if you look at postToPid there is no documentation at all

PostToPID

So not even sure if it is suppose to work and if yes then when and when not. But you can post the event directly to screen using below code

//
//  main.swift
//  mouseplay
//
//  Created by Tarun Lalwani on 22/05/18.
//  Copyright © 2018 Tarun Lalwani. All rights reserved.
//

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)

print("Hello, World!")

You can see it working also

Run demo

Purview answered 22/5, 2018 at 8:48 Comment(4)
I've gotten postToPid to work for keyboard events, but needed post(tap:) for mouse eventsCoussoule
Can you please tell how you made postToPid work for keyboard events?Purview
You can look at my implementation here: github.com/dldnh/sendkey/blob/master/main.swift - hope it helps!Coussoule
Ensure you have allowed your app to control this computer in accessibility settings for this to workDaisie
D
6

You say you are able to post keyboard events, so it isn't likely a permission issue, but you can try executing your program as root to rule that out.

Event coordinates might simply be different from what you expect (flipped?). Try setting up something in a simulated app to report on all received events at the application level.

Furthermore, are the coordinates in the screen coordinate system? Maybe the event is received by the simulator and discarded because there is no window "beneath" it to deliver it to. Try moving the simulator window to each corner of the screen and use something like (10,10) for a coordinate.

You can also try NSEvent and its CGEvent method to get the underlying cgevent which may be initialized correctly for your needs. If that works it will be easier to work out what you are missing.

Lastly, try not passing nil for the mouseEventSource. Something like CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); maybe?

Last ditch attempt at help:

I have an (old) mac application that dispatches manufactured mouse events to other programs; perhaps its source code can reveal something useful.

Now... if none of that works it might be beneficial for you to tell us what exactly you are trying to accomplish. Its possible there is a much better way.

Disarmament answered 3/2, 2017 at 6:44 Comment(0)
S
4

You were close (Swift 4):

let mousePosition: CGPoint = .zero // your position here
let mouseEvent = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, 
mouseCursorPosition: mousePosition, mouseButton: .left)
mouseEvent?.postToPid(33554)

An important detail that did not realize at first is that app sandboxing must be disabled for this API to work.

Secretary answered 6/1, 2018 at 22:18 Comment(0)
Q
0

Post here because this question is the top most in Google on send click event in OS X.

You may take a look on the working examples of drag'n'drop and make a click function.

Only one note, the window where you are going to click must be visible.

Quillet answered 21/4, 2017 at 12:0 Comment(1)
sending mouse event to system(you'll see mouse cursor move) is not sending mouse event to an app(you won't see mouse cursor move)Scharaga

© 2022 - 2024 — McMap. All rights reserved.