在Raspberry Pi和Windows 10 IoT Core上同时检测RMB和LMB印刷机

时间:2017-09-08 04:45:07

标签: c# user-interface uwp raspberry-pi3

您好我正在尝试检测UWP中是否同时按下了Left和Right按钮。这样做的最佳方式是什么?

1 个答案:

答案 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;
}