Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate mouse click from Mac App to other Application

Tags:

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) 
like image 330
Faisal Khalid Avatar asked Jan 28 '17 10:01

Faisal Khalid


People also ask

Can you simulate a mouse click?

Open the Activities overview and start typing Accessibility. Click Accessibility to open the panel. Press Click Assist in the Pointing & Clicking section. In the Click Assist window, switch the Simulated Secondary Click switch to on.

How do you simulate a mouse event?

An easier and more standard way to simulate a mouse click would be directly using the event constructor to create an event and dispatch it. Though the MouseEvent. initMouseEvent() method is kept for backward compatibility, creating of a MouseEvent object should be done using the MouseEvent() constructor.

How do I simulate a mouse click on my website?

click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.


1 Answers

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

like image 179
Tarun Lalwani Avatar answered Sep 20 '22 04:09

Tarun Lalwani