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
}
}
First, if you want to use the virtual key to trigger ctrl+left, you should use:
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:
I strongly suggest you change your system shortcut to solve this problem temporarily.