0

我的 OSX 应用程序的目标是用鼠标手势切换空间。我想在按下鼠标中键时通过向右或向左移动鼠标来切换空格。我已经捕捉到了我想要的鼠标手势,但我无法以编程方式切换空间。我试图模拟按 ctrl+右箭头或 ctrl+左箭头,但它不起作用。我的整个班级如下所示。

import Cocoa
import SwiftUI

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    var window: NSWindow!
    var mouseLocation: NSPoint { NSEvent.mouseLocation }
    var downX:CGFloat?

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        NSEvent.addGlobalMonitorForEvents(matching: [.otherMouseDown]) { event in
            self.downX = self.mouseLocation.x;
        }

        NSEvent.addGlobalMonitorForEvents(matching: [.otherMouseUp]) { event in
            if(self.mouseLocation.x - self.downX! > 0) {
                print("swipe right")

        let src = CGEventSource(stateID: CGEventSourceStateID.hidSystemState)

        let cmdd = CGEvent(keyboardEventSource: src, virtualKey: 0x37, keyDown: true)
        let cmdu = CGEvent(keyboardEventSource: src, virtualKey: 0x37, keyDown: false)
        let spcd = CGEvent(keyboardEventSource: src, virtualKey: 0x7C, keyDown: true)
        let spcu = CGEvent(keyboardEventSource: src, virtualKey: 0x7C, keyDown: false)


        let loc = CGEventTapLocation.cghidEventTap

        cmdd?.post(tap: loc)
        spcd?.post(tap: loc)
        spcu?.post(tap: loc)
        cmdu?.post(tap: loc)

            } else {
                print("swipe left")


                      let src = CGEventSource(stateID: CGEventSourceStateID.hidSystemState)

                        let cmdd = CGEvent(keyboardEventSource: src, virtualKey: 0x37, keyDown: true)
                        let spcd = CGEvent(keyboardEventSource: src, virtualKey: 0x7B, keyDown: true)
                        let spcu = CGEvent(keyboardEventSource: src, virtualKey: 0x7B, keyDown: false)
                        let cmdu = CGEvent(keyboardEventSource: src, virtualKey: 0x37, keyDown: false)

                        spcd?.flags = CGEventFlags.maskCommand;

                        let loc = CGEventTapLocation.cghidEventTap

                        cmdd?.post(tap: loc)
                        spcd?.post(tap: loc)
                        spcu?.post(tap: loc)
                        cmdu?.post(tap: loc)
            }
        }

    }

    func applicationWillTerminate(_ aNotification: Notification) {
        // Insert code here to tear down your application
    }




}


4

1 回答 1

1

首先,如果你想使用虚拟键触发ctrl+left,你应该使用:

spcd?.flags = CGEventFlags.maskControl;

maskCommand 表示命令键。0x37 是命令键,0x3B 是 ctrl 键。

制作我的应用程序时,我有同样的问题。

我发现虚拟钥匙可以工作,但系统抓不到。所以我得把我的快捷键改成Ctrl+1(移到左边空格),和Ctrl+2(移到右边空格)。然后虚拟钥匙就可以工作了。

这是我的代码:

func CtrlWithKey(keyCode:UInt) {
    // 0x3b is Ctrl key
    let controlKeyDownEvent = CGEvent(keyboardEventSource: nil, virtualKey: CGKeyCode(0x3B), keyDown: true)
    controlKeyDownEvent?.flags = CGEventFlags.maskControl
    controlKeyDownEvent?.post(tap: CGEventTapLocation.cghidEventTap)

    let letterKeyDownEvent = CGEvent(keyboardEventSource: nil, virtualKey: CGKeyCode(keyCode), keyDown: true)
    letterKeyDownEvent?.flags = CGEventFlags.maskControl
    letterKeyDownEvent?.post(tap: CGEventTapLocation.cghidEventTap)

    let letterKeyUpEvent = CGEvent(keyboardEventSource: nil, virtualKey: CGKeyCode(keyCode), keyDown: false)
    letterKeyUpEvent?.flags = CGEventFlags.maskControl
    letterKeyUpEvent?.post(tap: CGEventTapLocation.cghidEventTap)

    let controlKeyUpEvent = CGEvent(keyboardEventSource: nil, virtualKey: CGKeyCode(0x3B), keyDown: false)
    controlKeyUpEvent?.flags = CGEventFlags.maskControl
    controlKeyUpEvent?.post(tap: CGEventTapLocation.cghidEventTap)
}

我强烈建议您更改系统快捷方式以暂时解决此问题。

于 2020-05-25T13:47:03.070 回答