0

my OSX application targets to switch spaces with mouse gestures. I wanted to switch spaces with moving mouse to right or left while middle mouse button is pressed. I already caught mouse gestures that i wanted but i could not switch spaces programmatically. I tried to simulate pressing ctrl+right arrow or ctrl+left arrow but it doesn't work. My whole class is like below.

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
    }




}


Can ÖZGEN
  • 238
  • 3
  • 8

1 Answers1

1

First, if you want to use the virtual key to trigger ctrl+left, you should use:

spcd?.flags = CGEventFlags.maskControl;

maskCommand means Command key. And 0x37 is command key, 0x3B is ctrl key.

I have the same question when making my app.

I found that the virtual key can work, but the system can't catch it. So I have to change my shortcut key to Ctrl+1(move to left space),and Ctrl+2(move to right space). Then the virtual key can work.

And here is my code:

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)
}

I strongly suggest you change your system shortcut to solve this problem temporarily.

wayZ
  • 26
  • 3
  • This is a great idea. Solved my problem! Thanks. – Can ÖZGEN May 26 '20 at 14:36
  • And I found another problem. When I set the shortcut: ctrl+1(move to left space), ctrl+2(move to right space), ctrl+3(show mission control), I should set ctrl+left(swtich to space1), ctrl+right(switch to space2), ctrl+up(switch to space3).It shouldn't leave swithc to space123 empty. If let them empty, after restart the system, ctrl+1(move to left space ) and ctrl+2(move to right space) wouldn't work. – wayZ May 27 '20 at 21:34