         61: .RALT,
         62: .RCONTROL,
     ]
-
+    
     private func transformButtonNumber(buttonNumber: NSEventButtonNumberType) -> RawInputButtonCode {
         return buttonMap[buttonNumber] ?? .UNKNOWN
     }

    }
    private var ignoreFirstDelta = false
    private var currentDownModifierKeyCodes = Set<NSEventKeyCodeType>()
    private var atLeastOneEventForModifierKeyCodes = Set<NSEventKeyCodeType>()
    private let startingEventModifierFlags = NSEvent.modifierFlags()
    private var currentDownEventModifierFlags = NSEvent.modifierFlags().intersect([.DeviceIndependentModifierFlagsMask])
    public weak var engine: Engine!
    private let buttonMap: [NSEventButtonNumberType : RawInputButtonCode] = [

    public override func flagsChanged(theEvent: NSEvent) {
        let previousDownEventModifierFlags = currentDownEventModifierFlags
        currentDownEventModifierFlags = theEvent.modifierFlags.intersect([.DeviceIndependentModifierFlagsMask])
        let transitionedToUpModifierFlags = previousDownEventModifierFlags.subtract(currentDownEventModifierFlags)
        let transitionedToDownModifierFlags = currentDownEventModifierFlags.subtract(previousDownEventModifierFlags)
        let transitionedToUp = !transitionedToUpModifierFlags.isEmpty
        let transitionedToDown = !transitionedToDownModifierFlags.isEmpty
        let keyCode = theEvent.keyCode
        
        if transitionedToUp {
            let upModifierFlagsWereDownOnStart = !startingEventModifierFlags.intersect(transitionedToUpModifierFlags).isEmpty
            
            if upModifierFlagsWereDownOnStart {
                // Do not start handling a modifier until it has been cleared out from startingEventModifierFlags
                startingEventModifierFlags.subtractInPlace(transitionedToUpModifierFlags)
//                NSLog("IGNORE: \(keyCode)")
            }
            else {
                currentDownModifierKeyCodes.remove(keyCode)
                postKeyUpCode(keyCode)
//                NSLog("FLAG UP: \(keyCode)")
            }
        }
        else if transitionedToDown {
            currentDownModifierKeyCodes.insert(keyCode)
            postKeyDownCode(keyCode)
//            NSLog("FLAG DOWN: \(keyCode)")
        }
        else if isAleadyDownKeyCode(keyCode) {
            currentDownModifierKeyCodes.remove(keyCode)
            postKeyUpCode(keyCode)
//            NSLog("FLAG UP 2: \(keyCode)")
        }
        else if modifierFlagWasNotDownOnStart {
            currentDownModifierKeyCodes.insert(keyCode)
            postKeyDownCode(keyCode)
//            NSLog("FLAG DOWN 2: \(keyCode)")
        }
        else {
//            NSLog("IGNORE 2: \(keyCode)")
        }
        //NSLog("FLAGS: \(theEvent)")
    }

    private func isAleadyDownKeyCode(keyCode: NSEventKeyCodeType) -> Bool {
        return currentDownModifierKeyCodes.contains(keyCode)
    }
    
    private var modifierFlagWasNotDownOnStart: Bool {
        return !currentDownEventModifierFlags.subtract(startingEventModifierFlags).isEmpty
    }
    
    public override func mouseDown(theEvent: NSEvent) {
        postButtonDownEvent(theEvent)
    }
