Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于鼠标单击的Swift命令行工具不会在Finder中进行多次单击_Swift_Cocoa_Click_Mouse - Fatal编程技术网

用于鼠标单击的Swift命令行工具不会在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

我在命令行工具应用程序中使用下面的代码来执行鼠标单击。我使用一个shell脚本来执行代码,并使用一个口述命令来触发它。除了Finder,这段代码几乎可以在所有程序中使用,我不知道为什么。单次鼠标点击效果很好,但不是两次或三次。我不知道是什么问题

// 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。您是否尝试过发布四个事件,而不是像上一个问题中那样发布一个事件?