[FIXED] Xamarin.Forms – Global tapped event on application level


How can I intercept a tapped/clicked event on application level in an Xamarin.Forms app?

I tried on the MainPage to use something like

        <TapGestureRecognizer Command="{Binding TappedCommand}" Tapped="TapGestureRecognizer_OnTapped"></TapGestureRecognizer>

but it works only if I tap empty areas, touching a button doesn’t fire the events.

Additionally I often navigate by changing the MainPage, so I would have to add such recognizer even if it worked.

I want to handle any touch/tap/click event in my app globally, is it possible in Xamarin.Forms?


This can be easily setup within the platform-specific apps and then use a Xamarin.Forms dependency service to subscribe/unsubscribe to the events.

What you capture within those events is up to your needs, in this example I am just capturing and eventing the x/y values.

A generic EventArgs Class and DP Interface:

public class TouchEventArgs<T> : EventArgs
    public T EventData { get; private set; }

    public TouchEventArgs(T EventData)
        this.EventData = EventData;

public interface IGlobalTouch
    void Subscribe(EventHandler handler);
    void Unsubscribe(EventHandler handler);

Add the following to the MainActivity of your Xamarin.Android application:

public EventHandler globalTouchHandler;
public override bool DispatchTouchEvent(MotionEvent ev)
    globalTouchHandler?.Invoke(null, new TouchEventArgs<Point>(new Point(ev.GetX(), ev.GetY())));
    return base.DispatchTouchEvent(ev);

Android Dependency Implementation:

public class GlobalTouch : IGlobalTouch
    public GlobalTouch() {}

    public void Subscribe(EventHandler handler)
        (Forms.Context as MainActivity).globalTouchHandler += handler;

    public void Unsubscribe(EventHandler handler)
        (Forms.Context as MainActivity).globalTouchHandler -= handler;

Usage in your Xamarin.Forms project:

DependencyService.Get<IGlobalTouch>().Subscribe((sender, e) =>
    var point = (e as TouchEventArgs<Point>).EventData;

Note: You should be assigning a delegate so can unsubscribe…



The same technique can be used for iOS, there are TouchesBegan, TouchesEnded, TouchesMoved and TouchesCancelled that you can attach to depending upon your needs….

Answered By – SushiHangover

Answer Checked By – Marie Seifert (FixeMe Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *