I am trying to simulate mouse click on iphone simulator from macos App for that I am using CGEvents .
我试图从macos App模拟鼠标点击iphone模拟器,因为我正在使用CGEvents。
the process id is 33554 for iPhone simulator
iPhone模拟器的进程ID为33554
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
我也注意到,当ios模拟器窗口被聚焦时它模拟鼠标点击并且仅适用于此工具栏但不适用于模拟器,例如,如果我将CGPoint更改为(0,30)它将单击模拟器选项
but when I am giving CGPoints to click app inside iOS Simulator its not working
但是,当我提供CGPoints点击iOS模拟器内的应用程序时,它无法正常工作
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)
4 个解决方案
#1
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.
您说您可以发布键盘事件,因此它不太可能是权限问题,但您可以尝试以root身份执行您的程序来排除这种情况。
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.
此外,屏幕坐标系中的坐标是?也许事件是由模拟器接收并丢弃的,因为没有窗口“在它下面”将它传递给它。尝试将模拟器窗口移动到屏幕的每个角落,并使用类似(10,10)的坐标。
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.
您还可以尝试使用NSEvent及其CGEvent方法来获取可能根据您的需要正确初始化的基础cgevent。如果这样可行,那么找出你所缺少的东西会更容易。
Lastly, try not passing nil
for the mouseEventSource
. Something like CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
maybe?
最后,尝试不为mouseEventSource传递nil。像CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);也许?
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.
我有一个(旧的)mac应用程序,它将制造的鼠标事件发送到其他程序;也许它的源代码可以揭示有用的东西。
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.
现在......如果这些都不起作用,那么告诉我们你究竟想要完成什么可能对你有益。它有可能有更好的方法。
#2
3
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
你也可以看到它的工作原理
#3
2
You were close (Swift 4):
你很亲密(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.
一开始没有意识到的一个重要细节是必须禁用应用程序沙盒才能使此API正常工作。
#4
0
Post here because this question is the top most in Google on send click event in OS X.
发布此处是因为此问题是Google在OS X中发送点击事件中最重要的问题。
You may take a look on the working examples of drug-n-drop and make a click function.
您可以查看药物降落的工作示例并进行点击功能。
Only one note, the window where you are going to click must be visible.
只有一个注释,您要点击的窗口必须可见。
#1
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.
您说您可以发布键盘事件,因此它不太可能是权限问题,但您可以尝试以root身份执行您的程序来排除这种情况。
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.
此外,屏幕坐标系中的坐标是?也许事件是由模拟器接收并丢弃的,因为没有窗口“在它下面”将它传递给它。尝试将模拟器窗口移动到屏幕的每个角落,并使用类似(10,10)的坐标。
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.
您还可以尝试使用NSEvent及其CGEvent方法来获取可能根据您的需要正确初始化的基础cgevent。如果这样可行,那么找出你所缺少的东西会更容易。
Lastly, try not passing nil
for the mouseEventSource
. Something like CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
maybe?
最后,尝试不为mouseEventSource传递nil。像CGEventSourceRef eventSource = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);也许?
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.
我有一个(旧的)mac应用程序,它将制造的鼠标事件发送到其他程序;也许它的源代码可以揭示有用的东西。
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.
现在......如果这些都不起作用,那么告诉我们你究竟想要完成什么可能对你有益。它有可能有更好的方法。
#2
3
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
你也可以看到它的工作原理
#3
2
You were close (Swift 4):
你很亲密(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.
一开始没有意识到的一个重要细节是必须禁用应用程序沙盒才能使此API正常工作。
#4
0
Post here because this question is the top most in Google on send click event in OS X.
发布此处是因为此问题是Google在OS X中发送点击事件中最重要的问题。
You may take a look on the working examples of drug-n-drop and make a click function.
您可以查看药物降落的工作示例并进行点击功能。
Only one note, the window where you are going to click must be visible.
只有一个注释,您要点击的窗口必须可见。