Is there any way to detect a right click event from a mouse when using Xamarin for a UWP app? There does not seem to be any way to access mouse events except as they simulate touch events.
asked Oct 6, 2019 at 22:28
John Gaby
1,5323 gold badges20 silver badges36 bronze badges
1 Answer 1
If this is just a pure UWP app there is a UIElement.PointerPressed Event you can subscribe to.
void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;
if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
{
Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);
if (ptrPt.Properties.IsRightButtonPressed)
{
// Do something
}
// Prevent most handlers along the event route from handling the same event again.
e.Handled = true;
}
}
Check out the docs for more info.
answered Oct 6, 2019 at 23:09
Nick Peppers
3,25127 silver badges29 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
John Gaby
This looks quite promising, thanks for the response. Now let's say that I have a Xamarin control (e.g. Xamarin.Forms.Grid), how do I get the underlying UIElement for that control in the case where it is a UWP app. Note, I am not looking for a cross platform solution, only one that will works with UWP apps.
Nick Peppers
The easiest way might be to use the native control directly in Xaml/C#, especially if this is intended only as a UWP Forms app. It's that or you'll have to create your own custom renderer for that specific control(s).
John Gaby
The app is cross platform, but when the user is using the Windows version, I want to add some mouse functionality that would not be available on the other platforms. I will look into the custom renderer. Thanks again.
John Gaby
Well that was a lot of hoops to jump through just to be able to tell if the user clicked with the right mouse button but it works. Thanks for your help.