WRL/C++ Detect multiple mouse buttons pressed simultaneously

Programmer71 21 Reputation points
2020-11-05T20:23:38.477+00:00

Hello, I am coding in C++ targeting Windows WRL for UWP, without XAML. I need to process mouse input, at times consisting of left/right mouse 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

Universal Windows Platform (UWP)
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,481 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yan Gu - MSFT 2,676 Reputation points
    2020-11-06T05:55:57.99+00:00

    Hello,

    Welcome to Microsoft Q&A,

    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  
        }  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful