Do you need help on a specific subject? Use the contact form (Request a blog entry) on the right hand side.


Mouse up, mouse down and popup menu's

A quick note today: I found out the hard way that after a popup menu is dismissed by clicking outside of the menu, but inside the associated view, the mouseUp function is called without a preceding call to mouseDown.

My solution is to protect the mouseUp handler with a test for the mouseDown position. If the mouseDown position is 'nil', then the mouseUp will simply exit. The mouseDown position is of course initialised in mouseDown and removed in mouseUp.

Like this:
    var mouseDownPosition: CGPoint?

    override func mouseDown(theEvent: NSEvent) {
        if mouseDownPosition != nil {
            log.atLevelInfo(id: 0, source: "mouseDown", message: "Old mouseDownPosition still available, overwriting now")
        mouseDownPosition = convertPoint(theEvent.locationInWindow, fromView: nil)        

    override func mouseUp(theEvent: NSEvent) {
        if mouseDownPosition == nil {
            log.atLevelDebug(id: 0, source: "mouseUp", message: "Exit, no mouseDownPosition available")
        ... Handle the mouseUp here
        // The mouseDownPosition has been consumed, remove it
        mouseDownPosition = nil

Happy coding...

Did this help?, then please help out a small independent.
If you decide that you want to make a small donation, you can do so by clicking this
link: a cup of coffee ($2) or use the popup on the right hand side for different amounts.
Payments will be processed by PayPal, receiver will be sales at balancingrock dot nl
Bitcoins will be gladly accepted at: 1GacSREBxPy1yskLMc9de2nofNv2SNdwqH

We don't get the world we wish for... we get the world we pay for.

No comments:

Post a Comment