When WPF was designed, there wasn't too much touch or stylus capable devices around. To keep old apps to responsive to new input devices such as touch screen or stylus, their events are transformed as follows (if it is not marked as handled):
- Touch device: Stylus > Touch > Mouse
- Stylus device: Stylus > Mouse
The problem starts when you would like to handle each input device event only when it comes from its corresponding real device, e.g. handle stylus events only if it comes from a real stylus device and not from a touch device, etc.
In case of Touch events, nothing special required, as it is fired only by real touch devices.
In case of Mouse events, the StylusDevice property can be used:
protected override void OnMouseDown(MouseButtonEventArgs e)
{
if (e.StylusDevice != null) return;
// This is a real mouse event
base.OnMouseDown(e);
}
In case of Stylus events, the StylusDevice.TabletDevice.Type property can be used:
protected override void OnStylusButtonDown(StylusButtonEventArgs e)
{
if (e.StylusDevice.TabletDevice.Type != TabletDeviceType.Stylus) return;
// This is a real stylus event
base.OnStylusButtonDown(e);
}