PointerPressed: left or right button?

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

+8
source share
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 } } } 
+9
source

You can use the following event to determine which pointer is used and which 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 } } 
+3
source

Working on a UWP project and previous answers like Properties.IsLeftButtonPressed / IsRightButtonPressed did not help me. These values ​​are always false. During debugging, I realized that Properties.PointerUpdateKind changes depending on the mouse button. Here is the result that 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, such as the ButtonPressed options shown in the example, and XButton options, such as XButton1Pressed, XButton2Released, etc.

0
source

All Articles