UWP C++/WinRT - 如何检测是否按下了鼠标右键

UWP C++/WinRT - How to detect whether right mouse button is pressed

我打算在我的应用程序中实现鼠标 "dragging" 手势,遵循 PointerPressed() / PointerMoved() / PointerReleased() 模式。这是一个 UWP 应用程序,使用最近发布的 Windows.

的 C++/WinRT 语言投影

问题是,我需要使用鼠标右键而不是鼠标左键。

Pointer****() 事件目前不提供了解按下哪个按钮的方法。如果您尝试在 Pointer****() 事件中调用 GetKeyState(),::RightButton 虚拟键将始终为 return 0 (None)。此外,RightTapped() 事件在释放按钮之前不会触发。

但是,如果您将 PointerPressed() 事件附加到按钮(除了 Click() 事件),您会注意到左键单击会触发 Click 事件,而右键单击实际上会触发 PointerPressed( ) 事件。因此,理论上, 可以区分两个鼠标按钮(因为 API 已经做到了)。

有人知道在 UWP C++/WinRT 应用程序中使用鼠标右键执行上述手势的方法吗?任何建议都可以,我目前无能为力。

您可以检查 PointerPointPropertiesIsRightButtonPressed 值,该值可以通过 PointerRoutedEventArgs 访问。假设您将 PointerRoutedEventArgs e 作为事件处理程序中的参数,然后

PointerPoint currentPoint = e.GetCurrentPoint(NULL);
PointerPointProperties props = currentPoint.Properties();
if (props.IsRightButtonPressed()) {
    // right button pressed!
}

你可以检查这个sample for more details which is in C#. Check GetCurrentPoint documentation关于哪个参数作为参数传递