locked
PointerPressed event handling

    Question

  • Hi

    I'm struggling to get the PointerPressed event handler fire when using left button. It does fire on right button press and release, but not on left button press and release. After MUCH reading (a bit everywhere for last 4 days) I think I figured the basebutton is redirecting this event to become click event.  I saw I could change the clickmethod to press for instance, but I do need to trigger an action on press and an action on release when using the left button.

    Is there any possibility ?

    Thanks much for help.


    • Edited by S. Scherrer Wednesday, April 09, 2014 5:52 PM Wrong Title
    Wednesday, April 09, 2014 5:51 PM

Answers

  • Hi S. Scherrer,

    I believe the PointerPressed event has been redirected as a Tapped event as you mentioned, Tapped is a higher level gesture and I would recommend you to use it if it can fits your needs.

    Let's say if we have a button, the PointerPressed and PointerReleased event will be fired when mouse right/middle button clicked, but Tapped event will be fired with mouse left button. I believe XAML will not fire this for you because it consider Tapped event is good enough.

    A quick solution is:

            public MainPage()
            {
                this.InitializeComponent();
                //bt.PointerPressed += bEnteredExited_PointerPressed;
                bt.AddHandler(PointerPressedEvent, new PointerEventHandler(Button_PointerPressed), true);
                bt.AddHandler(PointerReleasedEvent, new PointerEventHandler(Button_PointerReleased), true);
            }
    
            private void Button_PointerPressed(object sender, PointerRoutedEventArgs e)
            {
                   // ...
            }
    
            private void Button_PointerReleased(object sender, PointerRoutedEventArgs e)
            {
                   // ...
            }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, April 10, 2014 2:38 AM
    Moderator

All replies

  • Hiya,

     I am help your understand of behind the scenes type stuff. On a mouse, a Click is button down then

    button release.  Applies to what ever button event you want to handle. Whatever event occurs, or a trigger,

    you can catch, or handle, that event.  I use MS Paint as example, you click on Line draw tool. Then you left

    button press somewhere in drawing area, move mouse around and then release left-button. There was at

    least 4 mouse events that the program handled.  Click, button-down or button-press, button-release, etc.

    In your question, it is possible that the event you wanted to handle did occur but you did not have the

    proper event-handler.  Without using specific code language, I hope this is helpful. Thanks.

    Wednesday, April 09, 2014 6:14 PM
  • Well I tried all event handler and caught them all. As well I do catche the right button press event proper with same event handler. So I'm pretty sure it's caught somewhere and marked handled true preventing routing. At least I read this somewhere. Just strange behaviour I find . In fact all Pointer event work (enter, exit...) just PointerPress and PointerRelease are not fired when using left button. This thread has similar topci, but they did not asnwer as I do need the 2 event fired (I know how to set clickmethod on press or on release  and it works fine )

    Wednesday, April 09, 2014 6:56 PM
  • Hi S. Scherrer,

    I believe the PointerPressed event has been redirected as a Tapped event as you mentioned, Tapped is a higher level gesture and I would recommend you to use it if it can fits your needs.

    Let's say if we have a button, the PointerPressed and PointerReleased event will be fired when mouse right/middle button clicked, but Tapped event will be fired with mouse left button. I believe XAML will not fire this for you because it consider Tapped event is good enough.

    A quick solution is:

            public MainPage()
            {
                this.InitializeComponent();
                //bt.PointerPressed += bEnteredExited_PointerPressed;
                bt.AddHandler(PointerPressedEvent, new PointerEventHandler(Button_PointerPressed), true);
                bt.AddHandler(PointerReleasedEvent, new PointerEventHandler(Button_PointerReleased), true);
            }
    
            private void Button_PointerPressed(object sender, PointerRoutedEventArgs e)
            {
                   // ...
            }
    
            private void Button_PointerReleased(object sender, PointerRoutedEventArgs e)
            {
                   // ...
            }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Thursday, April 10, 2014 2:38 AM
    Moderator
  • Hi

    Much thanks. I agree with your suggestion. However the funny part is that the button I want to use is part of a DataTemplate for a Gridview... So... I cannot access easily from code the named button inside this datatemplate. I am presently searching this new problem resolution to validate your response but I am almost certain now this is the right answer. This will allow the handler to be routed in all cases and not be absorbed by some other.

    Thursday, April 10, 2014 5:06 AM
  • Yes, access the template control from code is not a easy thing, you have to use something like VisualTreeHelper and you can ref to this blog: http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html

    But anyway I think for your original question, PointerPressed question has been solved :).

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, April 10, 2014 8:24 AM
    Moderator