简体   繁体   中英

Is it possible to simulate click using CGEvent for iOS app on Mac with M1?

I am trying to send click event to iOS-base app from App Store on ARM Mac(Apple Silicone, M1).

Here is snippet that works for any non-ios app:

import Foundation

func testClick(){
    let source = CGEventSource.init(stateID: .hidSystemState)
    let position = CGPoint(x: 100, 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)
    usleep(500_000)
    eventUp?.post(tap: .cghidEventTap)
}

With any iOS-app this code will move cursor, but won't simulate click.

After the last Big Sur update this code works, but you need to focus window first time.

if(firstTime) testClick()
testClick()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM