如何模拟从Mac App到其他应用程序的鼠标单击 [英] How to simulate mouse click from Mac App to other Application
问题描述
我正在尝试从macos App的iphone模拟器上模拟鼠标单击,因为我正在使用CGEvents.
I am trying to simulate mouse click on iphone simulator from macos App for that I am using CGEvents .
iPhone模拟器的进程ID为33554
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)
我还注意到,当ios模拟器窗口聚焦时,它将模拟鼠标单击,并且仅适用于此工具栏,但不适用于模拟器,例如,如果将CGPoint更改为(0,30),它将单击模拟器选项
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
但是当我给CGPoints单击iOS Simulator中的应用程序时,它不起作用
but when I am giving CGPoints to click app inside iOS Simulator its not working
但是,我可以使用以下方式将键盘事件发布到模拟器中
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)
推荐答案
首先,如果您查看postToPid
,根本没有文档
First of if you look at postToPid
there is no documentation at all
所以甚至不确定是否应该工作,如果可以,什么时候什么时候不可以.但是您可以使用以下代码将事件直接发布到屏幕上
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!")
您可以看到它也起作用
这篇关于如何模拟从Mac App到其他应用程序的鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!