detect simultaneous right and left mouse clicks

541 Views Asked by At

I'm writing a game in UWP and C#, and I want the player to be able to indicate an action by pressing both left and right mousebuttons at (or almost at) the same time.

It's easy enough to have an event for one button, but within the event, I want to see if the other button has been pressed.

It's been suggested that this should work: Window.Current.CoreWindow.GetKeyState(Windows.System.VirtualKey.RightButton).HasFlag(CoreVirtualKeyStates.Down) but it doesn't - it always returns false for the button that wasn't detected first.

Any suggestions?

1

There are 1 best solutions below

4
On

For PointerMoved event, The multiple, simultaneous mouse button inputs can be processed here and by testing, when pressing both left and right mousebuttons at (or almost) the same time, the event will be triggered. So you can try to use this event to judge.

private void StackPanel_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    if (e.Pointer.PointerDeviceType == PointerDeviceType.Mouse && e.Pointer.IsInContact)
    {
         var p = e.GetCurrentPoint((UIElement)sender);
         if (p.Properties.IsLeftButtonPressed && p.Properties.IsRightButtonPressed)
         {
             // do something               
         }

    }
    e.Handled = true;
}