用于鼠标单击的Swift命令行工具不会在Finder中进行多次单击
我在命令行工具应用程序中使用下面的代码来执行鼠标单击。我使用一个shell脚本来执行代码,并使用一个口述命令来触发它。除了Finder,这段代码几乎可以在所有程序中使用,我不知道为什么。单次鼠标点击效果很好,但不是两次或三次。我不知道是什么问题用于鼠标单击的Swift命令行工具不会在Finder中进行多次单击,swift,cocoa,click,mouse,Swift,Cocoa,Click,Mouse,我在命令行工具应用程序中使用下面的代码来执行鼠标单击。我使用一个shell脚本来执行代码,并使用一个口述命令来触发它。除了Finder,这段代码几乎可以在所有程序中使用,我不知道为什么。单次鼠标点击效果很好,但不是两次或三次。我不知道是什么问题 // Get location var ml = NSEvent.mouseLocation ml.y = NSHeight(NSScreen.screens[0].frame) - ml.y let location = CGPoint(x: ml.x
// Get location
var ml = NSEvent.mouseLocation
ml.y = NSHeight(NSScreen.screens[0].frame) - ml.y
let location = CGPoint(x: ml.x, y: ml.y)
// Clicks
let e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: location, mouseButton: .left)!
e.setIntegerValueField(.mouseEventClickState, value: 2) // double click
e.post(tap: .cghidEventTap)
CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: location, mouseButton: .left)!.post(tap: .cghidEventTap)
给你
/// Clicks left mouse button twice
static func doubleClick() {
var e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: Mouse.location, mouseButton: .left)!
e.post(tap: .cghidEventTap)
e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: Mouse.location, mouseButton: .left)!
e.post(tap: .cghidEventTap)
e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseDown, mouseCursorPosition: Mouse.location, mouseButton: .left)!
e.setIntegerValueField(.mouseEventClickState, value: 2)
e.post(tap: .cghidEventTap)
e = CGEvent(mouseEventSource: nil, mouseType: .leftMouseUp, mouseCursorPosition: Mouse.location, mouseButton: .left)!
e.setIntegerValueField(.mouseEventClickState, value: 2)
e.post(tap: .cghidEventTap)
}
双击是mouseDown、mouseUp、mouseDown、mouseUp正常单击,然后单击mouseEventClickState 2。您是否尝试过发布四个事件,而不是像上一个问题中那样发布一个事件?