Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there a way to detect double click events? I did not find anything on the doc page of EventHandler.

Use case: I want to re-implement the Crop Image... functionality available form the context menu when clicking images. The cropping GUI appears much too slowly with even moderately large images, such as screenshots of the full screen. Although I could use a button to crop, I'd prefer using double click, just like in the original implementation.

share|improve this question

2 Answers 2

up vote 10 down vote accepted

You can use a combination of MouseDown and MouseClickCount as in the following examples:

example 1: double-click increments the value of j:

j = 1; EventHandler[Panel[Dynamic[j]], 
"MouseDown" :> If[CurrentValue["MouseClickCount"] == 2, ++j]]

example 2: double-click toggles the text color:

DynamicModule[{col = Green}, 
  EventHandler[
    Style["text", FontColor -> Dynamic[col]], 
   {"MouseDown" :> 
      If[CurrentValue["MouseClickCount"] == 2, 
         (col = col /. {Red -> Green, Green -> Red})]}
  ]]
share|improve this answer
    
This seems to be the "right" solution (the one that uses the system's double click delay). @MrWizard is it available in v7 as well (I'd like to make the application work in 7)? –  Szabolcs Mar 13 '12 at 10:04
    
@Szabolcs yes it is! +1 kguler –  Mr.Wizard Mar 13 '12 at 10:07
    
@Szabolcs one point regarding this method: if I double-click repeatedly is does not register (the text color does not change repeatedly). This is different from typical Windows behavior. –  Mr.Wizard Mar 13 '12 at 10:25
    
@Mr.Wizard That's easy to fix with Mod[..., 2] == 0. Actually it does register repeated double clicks (with a pause inbetween), but not a quadruple click. Some programs (e.g. browsers and word processors) register triple or even quadruple clicks as well. Try many clicks in Firefox. First it selects the word, then paragraph, then word again. A separate click clear the selection. This method makes it easy to implement those behaviours (not that I want to). –  Szabolcs Mar 13 '12 at 10:37

You could do something like this:

DynamicModule[{col = Green, time = AbsoluteTime[]}, 
  EventHandler[
    Style["text", FontColor -> Dynamic[col]],
    "MouseClicked" :> 
      If[AbsoluteTime[] - time > 0.25, 
         time = AbsoluteTime[],
         col = col /. {Red -> Green, Green -> Red}]
  ]]

Implementing limited tolerance for mouse position will be more complicated.

share|improve this answer
    
It is definitely possible to do something like this, but if it's the only solution, I'd prefer not to use double clicks at all. I was hoping there is a solution that integrates with the system's double click detection and obeys the system-wide settings for it (e.g. maximum time and distance between clicks). BTW I didn't do much GUI programming, so I am now sure how big a problem it is to differentiate between double clicks and single clicks without causing a delay in the interpretation of single clicks. I'm curious if this requires special attention when using ... –  Szabolcs Mar 13 '12 at 7:03
    
... common GUI toolkits. I was trying to figure out how the built-in image cropping GUI does it, but it's not trivial to get it (converting to InputForm strips most of the GUI code). I'm not sure if that functionality is available in version 7. –  Szabolcs Mar 13 '12 at 7:04
    
@Szabolcs "I am now sure how big a problem it is to differentiate between double clicks and single clicks without causing a delay in the interpretation of single clicks." -- perhaps I am missing something, but isn't a delay of at least the double-click detention window necessary for single-click action? At least I cannot recall an exception to this in Windows. –  Mr.Wizard Mar 13 '12 at 7:08
    
I really don't know how these work :) I thought that maybe the delay is not there when no special double click actions are available for some GUI object. I have no idea how noticeable such a short delay would be, for example when clicking a button and waiting for an immediate response. –  Szabolcs Mar 13 '12 at 7:10
    
Some spelunking results: It seems that the image cropper is based on a special box type, Image`InteractiveImageCropBoxes, and is not implemented in terms of standard and documented things. –  Szabolcs Mar 13 '12 at 7:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.