Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PointerPressed: left or right button?

How can I get the type of pressed pointer (left mouse down or right mouse down) in a Metro style C# app? I didn't find a MouseLeftButtonDown event handler in any Metro style UI element. I should use PointerPressed event instead, but I don't know how can i get which button was pressed.

like image 831
kartal Avatar asked Dec 16 '12 18:12

kartal


3 Answers

PointerPressed is enough to handle mouse buttons:

void MainPage_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    // Check for input device
    if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
    {
        var properties = e.GetCurrentPoint(this).Properties;
        if (properties.IsLeftButtonPressed)
        {
            // Left button pressed
        }
        else if (properties.IsRightButtonPressed)
        {
            // Right button pressed
        }
    }
}
like image 174
andrewpey Avatar answered Oct 24 '22 15:10

andrewpey


You can use the following event to determine what pointer is used and what button is pressed.

private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;
    Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);

    if (ptrPt.Properties.IsLeftButtonPressed)
    {
        //Do stuff
    }
    if (ptrPt.Properties.IsRightButtonPressed)
    {
        //Do stuff
    }
}
like image 3
Mattias Josefsson Avatar answered Oct 24 '22 14:10

Mattias Josefsson


Working on a UWP project and previous answers like Properties.IsLeftButtonPressed/IsRightButtonPressed did not work for me. Those values are always false. I realized during the Debugging that Properties.PointerUpdateKind was changing according to mouse button. Here is the result which worked for me:

var properties = e.GetCurrentPoint(this).Properties;
if (properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.LeftButtonReleased)
{

}
else if (properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.RightButtonReleased)
{

}
else if (properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.MiddleButtonReleased)
{

}

There are more options in PointerUpdateKind like ButtonPressed varities of the ones in the example and XButton varities e.g. XButton1Pressed, XButton2Released etc.

like image 1
AntiqTech Avatar answered Oct 24 '22 14:10

AntiqTech