Benches Uncategorized

4

How to handle a button double click

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I know how to define and IBAction and link it to a control by (Click-Drag)ing.

But that does not make me chose what action (event for x-Microsoft) in the control am I linking to.

By other words, when I click-drag, I dont mention, anywhere that I want this action to be linked to the "Click" event of a button. So what if I want this action method to be called when a button is double clicked for example, or when the mouse moves over it.

Thanks

4 replies

Sign in or join Beachball Help to reply.

  1. 0

    laurent demaret

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Tony Sameh wrote:

    By other words, when I click-drag, I dont mention, anywhere that I want this action to be linked to the "Click" event of a button. So what if I want this action method to be called when a button is double clicked for example, or when the mouse moves over it.

    Well, you don't get that with NSButton but rather with NSEvent. To know wether it's a double click you have [[[NSApp currentEvent] clickCount]] that you can test in your handler. To know about entering or leaving a view (a button is also a view) you can refer to the documentation at
    http://developer.apple.com/mac/library/documentation/cocoa/Conceptual/EventOverv iew/TrackingAreaObjects/TrackingAreaObjects.html

    hth

  2. 0

    RayNewbie

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi Tony, and welcome to the Dev Forum!

    The good news is Apple provides thorough documentation on tracking, interpreting and handling Cocoa mouse events. The bad news is the info is carved into pieces that are scattered all over the library. The challenge is to identify the docs that contain one or more pieces of the puzzle, and then to fit everything together.

    As to double clicks, there's no NSControl equivalent to the double-click notification that many Windows controls send (e.g. BN_DOUBLECLICKED). Also, as you've seen, when connecting an event to an action method in IB, there's only one event to choose from (Cocoa Touch is different in this regard, btw. You'll have a choice of several events when connecting a UIControl object to an action method in IB, though "double-touch" isn't among them).

    So how do Cocoa programmers detect a double click in, for example, an NSButton? The default action message for this class is generated by a NSLeftMouseUp event. I.e. when the left button is released while the control is tracking the cursor.

    One crude but effective solution is to have the listener (the action method connected to the button) set an ivar and start a one-shot timer on the first message. If a second message is received before the timer method resets the ivar, we have a double click. Otherwise we have two single clicks.

    A more general solution is based on two methods you might easily overlook. Firstly, [setContinuous:|http://developer.apple.com/mac/library/documentation/Cocoa/Refe rence/ApplicationKit/Classes/NSCell
    Class/Reference/NSCell.html#//appleref/doc/uid/20000074-BBCCCEAA] allows you to configure the associated NSActionCell object to send a stream of messages to the button's target instead of just one. The stream starts with the mouse down event that initiated tracking and ends with the mouse up that ends tracking (also see [trackMouse:inRect:ofView:untilMouseUp:|http://developer.apple.com/mac/library/ documentation/Cocoa/Reference/ApplicationKit/Classes/NSCell
    Class/Reference/NSCell.html#//appleref/doc/uid/20000074-BBCCJAFJ]).

    The next piece of the puzzle is the [currentEvent|http://developer.apple.com/mac/library/documentation/Cocoa/Refere nce/ApplicationKit/Classes/NSApplication
    Class/Reference/Reference.html#//appleref/occ/instm/NSApplication/currentEvent] method of NSApplication. This returns the NSEvent object the started the current event cycle, i.e., the event which caused NSButton to send the current action message.

    Thus the listener has access to the stream of event objects which represents the mouse activity from the start of tracking to the end. Using the position and time stamp data from this stream, the listener can distinguish between clicks and drags (hovering is detected differently; I won't try to cover that topic in this post).

    A couple reference links that might help with the rest of the puzzle:

    [Handling Mouse Events|http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Ev entOverview/HandlingMouseEvents/HandlingMouseEvents.html#//apple_ref/doc/uid/100 00060i-CH6-SW1]

    [Control and Cell Programming Topics for Cocoa|http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/Con trolCell/ControlCell.html#//apple_ref/doc/uid/10000015i]

    Hope that helps!

    - Ray

  3. 0

    RayNewbie

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Ooops.. Sorry Laurent. I didn't mean to repeat any of your points. Was still editing my response when you posted. - Ray

  4. 0

    laurent demaret

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    No problemo Ray, it's obvious for me that we crossed our posts.

    I had another idea about "rolling over" the control: as you can bind it for the "Tool Tip" value, maybe it's possible to add an @dynamic one and do what you want in the "getter" ?

    Just a turnaround.. But I'm getting used to bind target/arguments of ordinary buttons instead of IBActions, very usefull for multiples and precise parameters and just regrets it's not implemented in more controls for more "actions".