10
$\begingroup$

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.

$\endgroup$
0

2 Answers 2

14
$\begingroup$

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})]}
  ]]
$\endgroup$
4
  • $\begingroup$ 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)? $\endgroup$
    – Szabolcs
    Mar 13, 2012 at 10:04
  • $\begingroup$ @Szabolcs yes it is! +1 kguler $\endgroup$
    – Mr.Wizard
    Mar 13, 2012 at 10:07
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Mar 13, 2012 at 10:25
  • $\begingroup$ @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). $\endgroup$
    – Szabolcs
    Mar 13, 2012 at 10:37
3
$\begingroup$

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.

$\endgroup$
8
  • $\begingroup$ 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 ... $\endgroup$
    – Szabolcs
    Mar 13, 2012 at 7:03
  • $\begingroup$ ... 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. $\endgroup$
    – Szabolcs
    Mar 13, 2012 at 7:04
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Mar 13, 2012 at 7:08
  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Mar 13, 2012 at 7:10
  • $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Mar 13, 2012 at 7:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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