//
//  EventMonitor.swift
//  ipbc-Client
//
//  Created by Gerrit Linnemann on 09.09.15.
//  Copyright (c) 2015 Adawim UG (haftungsbeschränkt). All rights reserved.
//
//  More infos to EventMonitor: http://www.raywenderlich.com/98178/os-x-tutorial-menus-popovers-menu-bar-apps
//

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