HidEvent.cs
changeset 44 63a5f4c05179
parent 43 5c7f34c627b9
child 46 cbddac7b53e7
     1.1 --- a/HidEvent.cs	Wed Dec 24 14:31:16 2014 +0100
     1.2 +++ b/HidEvent.cs	Thu Dec 25 15:12:21 2014 +0100
     1.3 @@ -24,7 +24,8 @@
     1.4          public bool IsGeneric { get; private set; }
     1.5          public bool IsButtonDown { get { return Usages.Count == 1 && Usages[0] != 0; } }
     1.6          public bool IsButtonUp { get { return Usages.Count == 1 && Usages[0] == 0; } }
     1.7 -        public bool IsRepeat { get; private set; }
     1.8 +        public bool IsRepeat { get { return RepeatCount != 0; } }
     1.9 +        public uint RepeatCount { get; private set; }
    1.10  
    1.11          public HidDevice Device { get; private set; }
    1.12  
    1.13 @@ -36,6 +37,8 @@
    1.14          public event HidEventRepeatDelegate OnHidEventRepeat;
    1.15  
    1.16          private System.Timers.Timer Timer { get; set; }
    1.17 +        public DateTime Time { get; private set; }
    1.18 +        public DateTime OriginalTime { get; private set; }
    1.19  
    1.20          //Compute repeat delay and speed based on system settings
    1.21          //Those computations were taken from the Petzold here: ftp://ftp.charlespetzold.com/ProgWinForms/4%20Custom%20Controls/NumericScan/NumericScan/ClickmaticButton.cs
    1.22 @@ -64,12 +67,16 @@
    1.23          /// <param name="hRawInputDevice">Device Handle as provided by RAWINPUTHEADER.hDevice, typically accessed as rawinput.header.hDevice</param>
    1.24          public HidEvent(Message aMessage, HidEventRepeatDelegate aRepeatDelegate)
    1.25          {
    1.26 -            IsRepeat = false;
    1.27 +            RepeatCount = 0;
    1.28              IsValid = false;
    1.29              IsKeyboard = false;
    1.30              IsGeneric = false;
    1.31 +            
    1.32  
    1.33 +            Time = DateTime.Now;
    1.34 +            OriginalTime = DateTime.Now;
    1.35              Timer = new System.Timers.Timer();
    1.36 +            Timer.Elapsed += (sender, e) => OnRepeatTimerElapsed(sender, e, this);
    1.37              Usages = new List<ushort>();
    1.38              OnHidEventRepeat += aRepeatDelegate;
    1.39  
    1.40 @@ -242,22 +249,31 @@
    1.41                  return;
    1.42              }
    1.43              Timer.Enabled = false;
    1.44 -            Timer.AutoReset = false;
    1.45 -            Timer.Interval = aInterval;
    1.46 -            Timer.Elapsed += (sender, e) => OnRepeatTimerElapsed(sender, e, this);            
    1.47 -            Timer.Enabled = true;
    1.48 +            //Initial delay do not use auto reset
    1.49 +            //After our initial delay however we do setup our timer one more time using auto reset
    1.50 +            Timer.AutoReset = (RepeatCount!=0);
    1.51 +            Timer.Interval = aInterval;         
    1.52 +            Timer.Enabled = true;            
    1.53          }
    1.54  
    1.55 -        private void OnRepeatTimerElapsed(object sender, ElapsedEventArgs e, HidEvent aHidEvent)
    1.56 +        static private void OnRepeatTimerElapsed(object sender, ElapsedEventArgs e, HidEvent aHidEvent)
    1.57          {
    1.58              if (aHidEvent.IsStray)
    1.59              {
    1.60                  //Skip events if canceled
    1.61                  return;
    1.62              }
    1.63 -            aHidEvent.IsRepeat = true;
    1.64 -            StartRepeatTimer(iRepeatSpeed);            
    1.65 -            OnHidEventRepeat(aHidEvent);
    1.66 +
    1.67 +            aHidEvent.RepeatCount++;
    1.68 +            aHidEvent.Time = DateTime.Now;
    1.69 +            if (aHidEvent.RepeatCount==1)
    1.70 +            {
    1.71 +                //Re-Start our timer only after the initial delay 
    1.72 +                aHidEvent.StartRepeatTimer(aHidEvent.iRepeatSpeed);
    1.73 +            }
    1.74 +
    1.75 +            //Broadcast our repeat event
    1.76 +            aHidEvent.OnHidEventRepeat(aHidEvent);
    1.77          }
    1.78  
    1.79          public ListViewItem ToListViewItem()
    1.80 @@ -277,7 +293,7 @@
    1.81  
    1.82              }
    1.83  
    1.84 -            ListViewItem item = new ListViewItem(new[] { usage, UsagePage.ToString("X2"), UsageCollection.ToString("X2"), IsRepeat.ToString() });
    1.85 +            ListViewItem item = new ListViewItem(new[] { usage, UsagePage.ToString("X2"), UsageCollection.ToString("X2"), RepeatCount.ToString(), Time.ToString("HH:mm:ss:fff") });
    1.86              return item;
    1.87          }
    1.88