Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF MouseButtonEventArgs Timestamp value is negative?

I have some code for a WPF trigger that checks for double-clicks:

private void HandleButtonUp(object sender, MouseButtonEventArgs mouseEventArgs)
    {
        if (mouseEventArgs.ChangedButton == MouseButton.Left &&
            (mouseEventArgs.Timestamp - _lastClick) < SystemInfo.DoubleClickTime)
        {
            this.InvokeActions(mouseEventArgs);
            _lastClick = 0; // Require 2 clicks again
        }
        else 
            _lastClick = mouseEventArgs.Timestamp;
    }

This has worked well until now. But today, suddenly single-clicks are invoking the action. When I checked the code, I have found that the timestamp value is negative which results in it always being less than the SystemInfo.DoubleClickTime (500 is what mine is set to).

Is this normal? Why has this suddenly changed?

like image 702
Gerald Avatar asked Jul 24 '13 12:07

Gerald


1 Answers

The InputEventArgs.Timestamp property behaves like Environment.TickCount, where you can find the following Remarks:

The value of this property is derived from the system timer and is stored as a 32-bit signed integer. Consequently, if the system runs continuously, TickCount will increment from zero to Int32.MaxValue for approximately 24.9 days, then jump to Int32.MinValue, which is a negative number, then increment back to zero during the next 24.9 days.

TickCount is different from the Ticks property, which is the number of 100-nanosecond intervals that have elapsed since 1/1/0001, 12:00am.

Use the DateTime.Now property to obtain the current local date and time on this computer.

Ignoring the rare case when the jump occurs (after 24.9 days, then every 49.7 days), you may check like this:

Math.Abs(mouseEventArgs.Timestamp - _lastClick) < SystemInfo.DoubleClickTime
like image 100
Clemens Avatar answered Oct 11 '22 13:10

Clemens