EsAMe
EsAMe

Reputation: 305

UWP - Middle Mouse Wheel Button Click Event Handler

I have a button control that when clicked with mouse wheel, should do something.

I tried this from WPF but I think it's different in UWP.

private void Button5_Click(object sender, RoutedEventArgs e)
{
    if (e.ChangedButton == MouseButton.Middle)
    {
        button6.Content = "Hi";
    }
}

Upvotes: 0

Views: 770

Answers (2)

Anran Zhang
Anran Zhang

Reputation: 7727

You can add PointerPressed event handler on Button.

Usage:

private void Button5_PointerPressed(object sender, PointerRoutedEventArgs e)
{
    var pointer = e.GetCurrentPoint(sender as Button);
    if (pointer.Properties.IsMiddleButtonPressed)
    {
        button6.Content = "Hello";
    }
}

Update

Add PointerReleased event handler on UIElement, but not Button, I think Button's Click event overrides the middle mouse up event.

Usage:

private void Grid_PointerReleased(object sender, PointerRoutedEventArgs e)
{
    var pointer = e.GetCurrentPoint(sender as UIElement);
    if (pointer.Properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.MiddleButtonReleased)
    {
        // Todo
    }
}

Best regards.

Upvotes: 1

OMANSAK
OMANSAK

Reputation: 1332

You can use any Pointer event and handle mouse device events

        BUtton_1.PointerPressed += delegate(object sender, PointerRoutedEventArgs args) 
        {
            Pointer ptr = args.Pointer;
            if (ptr.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse) // Wheel click has just on mouse
            {
                Windows.UI.Input.PointerPoint ptrPt = args.GetCurrentPoint((Button)sender);
                if (ptrPt.Properties.IsMiddleButtonPressed)
                {
                    TextBox_1.Text += "Hi";
                }
            }
            args.Handled = true;
        };

Upvotes: 0

Related Questions