0

I have an InkCanvas and a touch screen on a specific touch display. This display is able to capture stylus events. Sadly it also captures touch events. On some occassions this is completly ok. But when an InkCanvas is shown, it should ONLY capture the stylus events and not the touch or the mouse events. Following normal Microsoft logic I used the following approach:

private void InkCanvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    TabletDeviceType? stylus = e.StylusDevice?.TabletDevice?.Type;
    if (stylus == null || stylus != TabletDeviceType.Stylus)
        e.Handled = true;
}

private void InkCanvas_PreviewTouchDown(object sender, TouchEventArgs e)
{
    e.Handled = true;
}

private void InkCanvas_PreviewStylusDown(object sender, StylusDownEventArgs e)
{
    TabletDeviceType? type = e.StylusDevice?.TabletDevice?.Type;
    if (type == null || type == TabletDeviceType.Touch)
        e.Handled = true;
}

Now the mouse is ignored and the stylus can draw freely on the InkCanvas. BUT even when I "cancelled" the PreviewTouchDown, the line is drawn. Ok, after releasing the touch, it is NOT applied to the StrokesCollection and it gets deleted from the InkCanvas, but it was there. For all to see. When I do the same with the mouse, the complete event is ignored, not only rolled back after MouseUp.

When I googled some time around there were a mass of solutions for the UWP-InkCanvas (mostly it would work out all by itself, because it, by standard, only allows stylus events). But I have to use .Net-Framework 4.8, so I cannot use Islands or the .net core ones.

I would even consider using a complete different user control. Does someone know of a solution or workaround?

Marcel Grüger
  • 885
  • 1
  • 9
  • 25
  • I cant' really test rn but what about cancelling `PreviewMouseMove` as well? – Corentin Pane Sep 06 '21 at 13:49
  • I already tried to set an `e.Handled = true;` to `GotMouseCapture`, `GotTouchCapture`, `TouchDown`, `MouseDown`, `PreviewTouchMove`, `PreviewMouseMove`, `PreviewTouchDown`, `PreviewMouseDown`, `MouseMove` and `TouchMove`. But no behavior changes... – Marcel Grüger Sep 07 '21 at 05:22
  • Something else to keep in mind is that unhandled stylus/touch events eventually end up triggering mouse event listeners so you might want to always mark events as handled when you're done with them. Also, marking an event as handled doesn't prevent a control from still reacting to it using the handledEventsToo mechanism. So you can try to overlay a transparent item over your InkCanvas using a Grid to intercept the touch event maybe? – Corentin Pane Sep 07 '21 at 07:37
  • That sounds interesting, but I would not know how to filter the touch and mouse events with this grid and route the stylus event 1 level down the zindex... – Marcel Grüger Sep 07 '21 at 11:09
  • My bad, you're right it's a bit trickier than I thought. – Corentin Pane Sep 07 '21 at 11:33
  • Did anybody find a working answer? – Michael Sohnen May 25 '22 at 18:42
  • Microsoft stopped working on the .Net Framework and fixed this in the .Net Core. So there will be no solution unless you upgrade to .Net 6 or the upcoming .Net 7 maybe. – Marcel Grüger May 27 '22 at 05:23

0 Answers0