您好我正在尝试检测UWP中是否同时按下了Left和Right按钮。这样做的最佳方式是什么?
答案 0 :(得分:0)
在UWP中,没有直接事件表明LMB和RMB都被按下了。 PointerUpdateKind指定应用程序支持的指针更新类型。
您只能使用间接方法进行检测。请注意,当按下鼠标右键同时按下鼠标左键时,它将调用 PointerMoved 事件而不是PointerPressed。
private bool leftButtonPressed = false;
private bool rightButtonPressed = false;
//Scenario1OutputRoot is the ui element.
void Scenario1OutputRoot_PointerReleased(object sender, PointerRoutedEventArgs e)
{
Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(Scenario1OutputRoot);
if (pt.Properties.PointerUpdateKind != Windows.UI.Input.PointerUpdateKind.Other)
{
this.buttonPress.Text += (pt.Properties.PointerUpdateKind.ToString() + Environment.NewLine);
if (pt.Properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.LeftButtonPressed)
{
leftButtonPressed = false;
}
if (pt.Properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.RightButtonPressed)
{
rightButtonPressed = false;
}
}
}
void Scenario1OutputRoot_PointerPressed(object sender, PointerRoutedEventArgs e)
{
Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(Scenario1OutputRoot);
if (pt.Properties.PointerUpdateKind != Windows.UI.Input.PointerUpdateKind.Other)
{
this.buttonPress.Text += (pt.Properties.PointerUpdateKind.ToString() + Environment.NewLine);
if (pt.Properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.LeftButtonPressed)
{
leftButtonPressed = true;
}
if (pt.Properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.RightButtonPressed)
{
rightButtonPressed = true;
}
}
if (leftButtonPressed && rightButtonPressed)
{
this.buttonPress.Text = "both the Left and Right buttons are being pressed ";
}
}
void Scenario1OutputRoot_PointerMoved(object sender, PointerRoutedEventArgs e)
{
Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(Scenario1OutputRoot);
if(pt.Properties.PointerUpdateKind != Windows.UI.Input.PointerUpdateKind.Other)
{
this.buttonPress.Text += (pt.Properties.PointerUpdateKind.ToString() + Environment.NewLine);
if (pt.Properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.LeftButtonPressed)
{
leftButtonPressed = true;
}
if (pt.Properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.RightButtonPressed)
{
rightButtonPressed = true;
}
if (leftButtonPressed && rightButtonPressed)
{
this.buttonPress.Text = "both the Left and Right buttons are being pressed ";
}
}
e.Handled = true;
}