Created
October 21, 2015 17:29
-
-
Save maximbilan/3f0eb14d9b5064dfdd14 to your computer and use it in GitHub Desktop.
Mac OS X Event Monitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
public class EventMonitor { | |
private var monitor: AnyObject? | |
private let mask: NSEventMask | |
private let handler: NSEvent? -> () | |
public init(mask: NSEventMask, handler: NSEvent? -> ()) { | |
self.mask = mask | |
self.handler = handler | |
} | |
deinit { | |
stop() | |
} | |
public func start() { | |
monitor = NSEvent.addGlobalMonitorForEventsMatchingMask(mask, handler: handler) | |
} | |
public func stop() { | |
if monitor != nil { | |
NSEvent.removeMonitor(monitor!) | |
monitor = nil | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment