录制鼠标事件脚本
用到自己封装好的工具 MAC代码模拟鼠标和键盘事件
var timeStamp:CLongLong = -1 //保存上次时间戳
var isStart:Bool = false //保存是否开始录制脚本
var events:[CGEvent] = CGEvent //保存所有鼠标事件
var times:[Int] = Int //保存鼠标事件事件间隔(完美时间还原脚本)
1.录制脚本
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| override func viewDidLoad() { super.viewDidLoad() monitor = NSEvent.addGlobalMonitorForEvents(matching:[.mouseMoved,.leftMouseDown,.leftMouseUp,.leftMouseDragged,.rightMouseDown,.rightMouseUp,.rightMouseDragged,.scrollWheel]) { [weak self](event) in self?.label.cell?.title = "x=\(String(format: "%.0f", event.locationInWindow.x))\ny=\(String(format: "%.0f", ScreenHeight - event.locationInWindow.y))" if(self!.isStart){ self!.events.append(event.cgEvent!) if(self!.events.count > 1){ self!.times.append(Int(Date().milliStamp - self!.timeStamp)) self!.timeStamp = Date().milliStamp }else{ self!.times.append(10) self!.timeStamp = Date().milliStamp } } }
}
|
2.播放脚本
1 2 3 4 5 6 7 8 9 10 11 12
| func playEvent(){ var index = 0 for event in events { if(index > times.count - 1){ Thread.sleep(forTimeInterval: TimeInterval(0.01)) }else{ Thread.sleep(forTimeInterval: TimeInterval( Double(times[index]) / 1000.0)) } index += 1; event.post(tap: .cghidEventTap) } }
|