简体   繁体   中英

How to get button pressing event in UWP, windows 10 store app

I want to implement zoomin and zoomout button and I want to genereate event for pressing so that I can continuously call event untill button is hold, but there is no direct event for pointer pressing or button pressing. Right now I am doing it through code below.

 this.btnDockUp.AddHandler(PointerReleasedEvent, new PointerEventHandler(BtnDockUp_PointerReleased), true);
 this.btnDockUp.AddHandler(PointerPressedEvent, new PointerEventHandler(BtnDockUp_PointerPressed), true);

    private void Image_PointerPressed(object sender, PointerRoutedEventArgs args)
    {
        PointerPoint point = args.GetCurrentPoint(sender as UIElement);
        if (args.Pointer.PointerDeviceType == PointerDeviceType.Mouse && point.Properties.IsLeftButtonPressed)
        {
            do
            {
                Debug.WriteLine("Pressed");
            } while (point.Properties.IsCanceled);
            Debug.WriteLine("Released");
        }
    }

    private volatile bool _upPressed = false;
    private void BtnDockUp_PointerReleased(object sender, PointerRoutedEventArgs e)
    {
        _upPressed = false;
    }

but its involving task and threads, I want some batter way to achieve this.

Just for the record,

You know when the button is pressed, you also know when the button is released. Therefore you know that all the time in between, the button is being held. The trick is to implement your own logic that keeps track of the input state in a way that's useful to your app/game.

So you might have a MouseSnapshot class with the following fields:

    public Vector2 Position;
    public Vector2 PositionDelta;
    public int WheelDelta;
    public Buttons ButtonPressed;
    public Buttons ButtonHeld;
    public Buttons ButtonReleased;
    public bool PointerExited;
    public bool PointerEntered;
    public bool PointerInRegion;
    // whatever else you need to track

Now you just update your MouseSnapshot as the events come in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM