Touch Screen Double Touch Capture

I posted another question on how to "manually" capture a double tap while tracking the time span between touches of the TouchDown event, but it's pretty buggy. Does anyone know of Microsoft's standard way / event for double-tap capture on a touch screen?

Thank you very much,

Dan

+4
source share
4 answers

I check the combination of tap location and stopwatch and it works great!

private readonly Stopwatch _doubleTapStopwatch = new Stopwatch(); private Point _lastTapLocation; public event EventHandler DoubleTouchDown; protected virtual void OnDoubleTouchDown() { if (DoubleTouchDown != null) DoubleTouchDown(this, EventArgs.Empty); } private bool IsDoubleTap(TouchEventArgs e) { Point currentTapPosition = e.GetTouchPoint(this).Position; bool tapsAreCloseInDistance = currentTapPosition.GetDistanceTo(_lastTapLocation) < 40; _lastTapLocation = currentTapPosition; TimeSpan elapsed = _doubleTapStopwatch.Elapsed; _doubleTapStopwatch.Restart(); bool tapsAreCloseInTime = (elapsed != TimeSpan.Zero && elapsed < TimeSpan.FromSeconds(0.7)); return tapsAreCloseInDistance && tapsAreCloseInTime; } private void OnPreviewTouchDown(object sender, TouchEventArgs e) { if (IsDoubleTap(e)) OnDoubleTouchDown(); } 

It checks in PreviewTouchDown whether it is DoubleTap.

+10
source

Jowans' answer helped me a lot (if my reputation allowed me, I would promote it;)) BUT I had to adapt it, so it only worked with double tags. The source code considers that any crane above 2 should be a double crane. Changing the _lastTapLocation object to NULL and resetting it when this double-click helped.

 private Point? _lastTapLocation; private bool IsDoubleTap(TouchEventArgs e) { Point currentTapPosition = e.GetTouchPoint(this).Position; bool tapsAreCloseInDistance = false; if (_lastTapLocation != null) { tapsAreCloseInDistance = GetDistanceBetweenPoints(currentTapPosition, (Point)_lastTapLocation) < 70; } _lastTapLocation = currentTapPosition; TimeSpan elapsed = _doubleTapStopwatch.Elapsed; _doubleTapStopwatch.Restart(); bool tapsAreCloseInTime = (elapsed != TimeSpan.Zero && elapsed < TimeSpan.FromSeconds(0.7)); if (tapsAreCloseInTime && tapsAreCloseInDistance) { _lastTapLocation = null; } return tapsAreCloseInDistance && tapsAreCloseInTime; } 
+3
source

I think using the StylusSystemGesture event is more appropriate. Here is my code.

  public static class ext { private static Point? _lastTapLocation; private static readonly Stopwatch _DoubleTapStopwatch = new Stopwatch(); public static bool IsDoubleTap(this StylusSystemGestureEventArgs e, IInputElement iInputElement) { Point currentTapPosition = e.GetPosition(iInputElement); bool tapsAreCloseInDistance = false; if (_lastTapLocation != null) { tapsAreCloseInDistance = GetDistanceBetweenPoints(currentTapPosition, (Point)_lastTapLocation) < 70; } _lastTapLocation = currentTapPosition; TimeSpan elapsed = _DoubleTapStopwatch.Elapsed; _DoubleTapStopwatch.Restart(); bool tapsAreCloseInTime = (elapsed != TimeSpan.Zero && elapsed < TimeSpan.FromSeconds(0.7)); if (tapsAreCloseInTime && tapsAreCloseInDistance) { _lastTapLocation = null; } return tapsAreCloseInDistance && tapsAreCloseInTime; } } 

Using:

  private void UIElement_OnStylusSystemGesture(object sender, StylusSystemGestureEventArgs e) { if (e.SystemGesture == SystemGesture.Tap) { if (e.IsDoubleTap(sender as IInputElement)) { // Do your stuff here } } } 
+1
source

By adding Andreas answer, you can also remove the code associated with the stopwatch.

StylusSystemGestureEventArgs also comes with a Timestamp property, which I believe is counted in milliseconds.

Therefore, instead of using the stopwatch to calculate from TimeSpan, add the int _lastTimestamp field and subtract the timestamps.

 public static class ext { private static Point? _lastTapLocation; // Stopwatch changed to int private int _lastTimestamp = 0; public static bool IsDoubleTap(this StylusSystemGestureEventArgs e, IInputElement iInputElement) { Point currentTapPosition = e.GetPosition(iInputElement); bool tapsAreCloseInDistance = false; if (_lastTapLocation != null) { tapsAreCloseInDistance = GetDistanceBetweenPoints(currentTapPosition, (Point)_lastTapLocation) < 70; } _lastTapLocation = currentTapPosition; // This replaces the previous TimeSpan calculation bool tapsAreCloseInTime = ((e.Timestamp - _lastTimestamp) < 700); if (tapsAreCloseInTime && tapsAreCloseInDistance) { _lastTapLocation = null; } return tapsAreCloseInDistance && tapsAreCloseInTime; } } 
+1
source

Source: https://habr.com/ru/post/1392893/


All Articles