c++uwpmousewrl

WRL/C++ Detect multiple mouse buttons pressed simultaneously


I am coding in C++ targeting Windows WRL for UWP, and I need to process mouse input, at times consisting of left/right buttons being pressed simultaneously.

My code only receives an event for the first mouse button pressed and the last mouse button released. So, if I press the right button followed by the left button, PointerPressed is not fired for the left button. Similarly, if I release the right button and then release the left button, PointerReleased is not fired for the right button. I subscribed to the events as follows:

void App::SetWindow(CoreWindow^ window)
{
  window->PointerPressed += ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &App::OnPointerPressed);
  window->PointerReleased += ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &App::OnPointerReleased);
}

void App::OnPointerPressed(CoreWindow ^sender, PointerEventArgs ^args)
{
}

void App::OnPointerReleased(CoreWindow ^sender, PointerEventArgs ^args)
{
}

Please let me know if my code needs to reset something in between button presses/releases, or if I should subscribe to some other events.

Thanks


Solution

  • The CoreWindow.PointerPressed event is fired for the first mouse button pressing detected in the interaction session, and the event is not fired for the second mouse button pressing before the interaction session ends up. Similarly, the CoreWindow.PointerReleased event is fired for only when the last mouse button is released, that is, the event is not fired when your first button is released and the second button is not released.

    Therefore, you can not use CoreWindow.PointerPressed and CoreWindow.PointerReleased event to detect left and right buttons being pressed simultaneously.

    You could use CoreWindow.PointerMoved event to detect your simultaneous two mouse action.

    For example,

    Void App::SetWindow(CoreWindow^ window)
    {
        window->PointerMoved+=ref new TypedEventHandler<CoreWindow^, PointerEventArgs^>(this, &App::OnPointerMoved);
    }
    
    Void App::OnPointerMoved(CoreWindow^ sender, PointerEventArgs^ args)
    {
        if (args->CurrentPoint->Properties->IsLeftButtonPressed)
        {
            pressed++;  // pressed is an int variable whose initial value is 0,  pressed is defind in App.xaml.h
        }
        if (args->CurrentPoint->Properties->IsRightButtonPressed)
        {
            released++;  // released is an int variable whose initial value is 0,  released is defind in App.xaml.h
        }
        if (pressed&&released)
        {
            pressed = 0;
            released = 0;
           //Here we know the right and left button of mouse is pressed
        }
    }