You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac端无需接管鼠标实现iPhone Mirroring模拟点击的技术求助

在Mac的iPhone Mirroring中无控制权模拟鼠标点击的方案探索

问题说明

我需要在Mac的iPhone Mirroring功能里模拟鼠标点击,目前已能通过接管鼠标的代码实现(移动鼠标并执行点击),但希望找到无需获取电脑全局控制权的方式,以此自动化iPhone上的部分任务。试过StackOverFlow上所有相关方案均无效,当前使用的代码如下:

当前实现代码

import SwiftUI
import CoreGraphics
import AppKit

struct ContentView: View {
    func simulateMouseClick() {
        // 定位目标窗口
        guard let targetApp = NSWorkspace.shared.runningApplications
            .first(where: { $0.localizedName?.contains("iPhone Mirroring") == true }),
              let windowList = CGWindowListCopyWindowInfo(.optionOnScreenOnly, kCGNullWindowID) as? [[String: Any]],
              let targetWindow = windowList.first(where: { ($0[kCGWindowOwnerPID as String] as? pid_t) == targetApp.processIdentifier }),
              let bounds = targetWindow[kCGWindowBounds as String] as? [String: Any],
              let x = bounds["X"] as? CGFloat,
              let y = bounds["Y"] as? CGFloat,
              let width = bounds["Width"] as? CGFloat,
              let height = bounds["Height"] as? CGFloat,
              let windowNumber = targetWindow[kCGWindowNumber as String] as? Int else {
            return
        }
        
        // 移动鼠标并确定点击点
        let point = CGPoint(x: x + width/2, y: y + height/2)
        CGDisplayMoveCursorToPoint(CGMainDisplayID(), point)
        
        // 创建点击事件
        guard let mouseDown = NSEvent.mouseEvent(
            with: .leftMouseDown,
            location: point,
            modifierFlags: [],
            timestamp: ProcessInfo.processInfo.systemUptime,
            windowNumber: windowNumber,
            context: nil,
            eventNumber: 0,
            clickCount: 1,
            pressure: 1.0
        )?.cgEvent,
        let mouseUp = NSEvent.mouseEvent(
            with: .leftMouseUp,
            location: point,
            modifierFlags: [],
            timestamp: ProcessInfo.processInfo.systemUptime,
            windowNumber: windowNumber,
            context: nil,
            eventNumber: 0,
            clickCount: 1,
            pressure: 1.0
        )?.cgEvent else {
            return
        }
        
        // 尝试执行两次点击
        mouseDown.post(tap: .cghidEventTap)
        usleep(100_000)
        mouseUp.post(tap: .cghidEventTap)
        usleep(200_000)
        mouseDown.post(tap: .cghidEventTap)
        usleep(100_000)
        mouseUp.post(tap: .cghidEventTap)
    }
    
    var body: some View {
        Button("点击目标窗口") { simulateMouseClick() }
    }
}

核心需求

  • 不移动物理鼠标、不占用系统全局控制权,直接向iPhone Mirroring窗口发送模拟点击事件
  • 实现iPhone端任务的自动化操作

内容的提问来源于stack exchange,提问作者SamHoque

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 20:03:13