Below is my simple AppDelegate.swift class.
// AppDelegate.swift
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(aNotification: NSNotification) {
NSEvent.addGlobalMonitorForEventsMatchingMask(NSEventMask.KeyDownMask, handler: keyDown);
}
func keyDown(event:NSEvent!) {
print("key down is \(event.keyCode)");
}
}
How come key down events are not being printed? What am I missing? I've tried searching around but can't figure it out.
In order to use
NSEvent
globally as you currently have it you would have to allow your application to be trusted through accessibility settings. If you want to useNSEvent
locally you could do:According to
NSEvent.h
onaddGlobalMonitorForEventsMatchingMask:handler:
: