post icon

Silverlight Tip of the Day #17 – Double Click

Silverlight currently has full mouse support for single click. However, double click is a another story. In this tip I will show you how to implement double click. You can apply this technique for an individual control or even your entire page.

The key to accomplishing this is to check for two things:

  1. Measure the TimeSpan between two mouse clicks. Verify it is less than around 300 milliseconds.
  2. Make certain the mouse has not moved more than a few pixels.

To demonstrate this I have a DateTime member that tracks the time the first mouse left click occurred. I also keep track of whether this is the first click or not.

public DateTime _lastClick = DateTime.Now; 
private bool _firstClickDone = false; 

Next, I add an event to monitor for left mouse clicks:

this.MouseLeftButtonDown += new 
      MouseButtonEventHandler(MainPage_MouseLeftButtonDown); 

Finally, in the event to monitor for mouse left clicks I do the following:

  • If more than 300 milliseconds has passed or a first click has not occurred yet get a snapshot of the time for the first click.
  • If we have clicked already and less than 300 milliseconds has passed verify the mouse has not moved more than a few pixels. If not, we have a double click.

The following code demonstrates this technique:

private void MainPage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    UIElement element = sender as UIElement; 
    DateTime clickTime = DateTime.Now; 
 
    TimeSpan span = clickTime - _lastClick; 
 
    FirstClickTB.Text = "MS between clicks = " + span.TotalMilliseconds; 
 
    if (span.TotalMilliseconds > 300 || _firstClickDone == false) 
    { 
        DoubleClickTB.Text = "First click..."; 
        _clickPosition = e.GetPosition(element); 
        _firstClickDone = true; 
        _lastClick = DateTime.Now; 
    } 
    else 
    { 
        Point position = e.GetPosition(element); 
        if (Math.Abs(_clickPosition.X - position.X) < 4 && 
            Math.Abs(_clickPosition.Y - position.Y) < 4) 
            DoubleClickTB.Text = "Double Click!"; 
        else 
            DoubleClickTB.Text = "Double Click failed due to mouse move!"; 
        _firstClickDone = false; 
    }            
} 

[silverlight: Tip16_DoubleClick.xap]

Thank you, --Mike

15 Comments

Leave a comment
  1. Tom
    10. May, 2010 at 9:15 pm #

    Hmmmm.. in Windows at least, the default double-click time is 500ms and mouse movement is tolerated within a 4×4 rectangle around the first click. You’re quite the harsh double-clicking taskmaster, Mike! :)

    http://blogs.msdn.com/oldnewthing/archive/2008/04/23/8417521.aspx
    http://msdn.microsoft.com/en-us/library/ms724385%28VS.85%29.aspx

    Personally, I’m on a mission to abolish double-clicking altogether. I’ve seen a lot of people have trouble with it, but one recent instance really takes the cake. I helped a customer whose laptop touchpad was so screwed up that he was completely unable to double-click, even when plugging an external mouse into the system. He ended up having to go into Control Panel, Mouse properties (which was overridden by the touchpad’s proprietary UI!), and change the action for the left mouse button from “click” to “double-click”. Then he was able to click and double-click normally. Yes, that’s right – from click to double-click. What sense does that make?! Ah computers… Of course, he swore he had never gone into that UI before.

  2. Patrick
    10. May, 2010 at 9:48 pm #

    Hello Mike,
    there is another solution to implement the double-click: save the date/time of the previous click and calculate the time difference to detect a double-click.
    This solution has the advantage to not use a timer (less resource used) and you can leave the MouseLeftButtonDown event handler.
    Patrick

  3. Kevkong
    11. May, 2010 at 12:22 am #

    Its simple, but what maybe It would be much reasonable to check wheter the second click is on the same element instead of checking, if the mouse moved more than 1 pixel. Some people do have high mousesensitivity :x

  4. MojoFilter
    11. May, 2010 at 7:06 am #

    I was JUST discussing this with a coworker (seriously, we had the conversation and I sat down to this post staring directly at me in Google Reader). It seems pretty incredible to me that there’s no built-in support for double-clicking in Silverlight 4. No support for right-clicking was understandable but even that is supported now.

    It hurts my soul a little bit every time I have to do the double-click detection manually.

  5. Snowman
    11. May, 2010 at 7:43 am #

    MojoFilter- Thanks for your feedback, I will pass it along to the Sl time! I am in full agreement :)

    Kevkong – An element can be pretty big, the full page in fact. I would prefer that the double click is checked within a region.

    Patrick – DateTime, hmm, yea maybe that’s better :) I’ll try it out.

    Tom- Perhaps you are right :) I can update it for more tollerence.

  6. Chad
    11. May, 2010 at 8:13 am #

    Is there anyway to pull the mouse settings from the OS?

  7. ozz
    11. May, 2010 at 10:10 am #

    thanks for tip. why are you using flash on your site and not silverlight?

  8. Snowman
    11. May, 2010 at 11:44 am #

    Chad – Not that I know of

    ozz- What do you mean? All my demos are in Silverlight.

    The Google ads are in Flash, but I cannot control that. Also, the WordPress plugins are flash based because they haven’t been made in SL yet.

  9. Anvaka
    14. May, 2010 at 12:07 am #

    I use DateTime approach instead. As Patric mentioned, it’s less resource intensive and is easier to implement… Timer approach sounds like using a steam-hammer to crack nuts.

    PS: Sorry for criticism. I really like your tips. Keep it up!

  10. Snowman
    14. May, 2010 at 8:42 am #

    Thanks everyone for your feedback. You are right about DateTime being better than a timer.

    I have updated this tip to reflect that technique.

    Let me know if you can think of even better ways to optimize it.

    –Mike

  11. darkkevind
    27. Jan, 2011 at 6:06 am #

    I’ve written a class that could help with this in a more easier way.

    http://kevindark.co.uk/SilverlightDoubleClick.aspx

    Any feedback is apreciated.

    Kevin.

  12. DmitryK1
    18. Jul, 2011 at 3:22 am #

    2Tom
    >in Windows at least, the default double-click time is
    user changable from the config panel.

    This is incradible microsoft innovation in silverlight.

    the parameter see in HKEY_CURRENT_USER/Control Panel/Mouse
    DoubleClickSpeed (500 is default, but user configurable)

  13. Kevkong
    02. Aug, 2011 at 5:23 am #

    Is it just me, or did you forget to declare _clickPosition? ;)

Trackbacks/Pingbacks

  1. Dew Drop – May 11, 2010 | Alvin Ashcraft's Morning Dew - 11. May, 2010

    [...] Silverlight Tip of the Day #17 – Double Click (Mike Snow) [...]

  2. Handling Mouse Double Click event in Silverlight with Caliburn.Micro « Valeriu Caraulean - 18. Nov, 2010

    [...] events and then deciding, it was a double click or not (using TimeSpan and distance between clicks, sample implementation). The interesting bit however was to link this event to an Action to be executed by Caliburn.Micro [...]