0

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?

cyberspy
  • 1,023
  • 11
  • 13

1 Answers1

0

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;
}
Faywang - MSFT
  • 5,798
  • 1
  • 5
  • 8
  • Thanks for the suggestion, but I'm already using IsLeftButtonPressed and IsRightButtonPressed to detect which single button has been pressed, but I can't get both to be true. I always get something like this when I output those values: `Right Clicked properties.IsLeftButtonPressed: False properties.IsRightButtonPressed: True` Even if I introduce a short delay after the Event is triggered, to give some time for the other button to be pressed, I can only get one of IsLeft or IsRight to be true – cyberspy Oct 24 '19 at 22:09
  • The PointerPressed event can only be triggered one after the other, that is, the button of the previous trigger will block the trigger of the next button before releasing. It is recommended to use PointerMove so that it can be detected at the same time without being blocked. And when you used PointerMove, will the results both return true? – Faywang - MSFT Oct 25 '19 at 09:55
  • Understood - I'll give it a go. What happens though if the mouse stops moving before the buttons are clicked? – cyberspy Oct 25 '19 at 19:53
  • Even if your mouse doesn't move, it still monitors your mouse clicks. – Faywang - MSFT Oct 28 '19 at 09:51