1

I have MAUI app and I want to record touch events by users on Android and Windows platform globally in order to record a macro and later playback the macro.

  • You'll need to write custom code for each platform. Read Maui docs to adapt Xamarin answers to Maui handlers or services. For android, maybe [Xamarin.Forms - Global tapped event](https://stackoverflow.com/a/41216401/199364). Or google `android global touch listener`. That gives java answers; the equivalent Xamarin c# functions are usually same name, but with first letter capitalized. For Windows (net6-windows), I think its similar to [this UWP method](https://stackoverflow.com/a/56836104/199364). – ToolmakerSteve Apr 18 '22 at 15:06

2 Answers2

0

In Android, the View class defines an overridable method named OnTouchEvent to process all the touch activity. The type of the touch activity is defined by enumeration members Down, PointerDown, Move, Up, and PointerUp.The Android View also defines an event named Touch that allows an event handler to be attached to any View object.

In the Universal Windows Platform (UWP), the UIElement class defines events named PointerPressed, PointerMoved, and PointerReleased. These are described in the article Handle Pointer Input article on MSDN and the API documentation for the UIElement class.

You can check this article which shows how to implement low-level multi-touch finger tracking, and how to generate events that signal touch activity:

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/effects/touch-tracking

Adrain
  • 1,946
  • 1
  • 3
  • 7
0

You have two options to capture touch events of the user.

1. Easy - use GestureRecognizers

Use View.GestureRecognizers on your control on which you want to capture user inputs. The advantage is that this works on all platforms out of the box. You can add different gesture recognizer such as tap, swipe, pan, ... in code behind or in xaml. I guess the tap gesture is the one you are looking for

See: https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/gestures/tap

1.1 Sample

            var tapGestureRecognizer = new TapGestureRecognizer();
            tapGestureRecognizer.Tapped += TapGestureRecognizer_Tapped;
            elementView.GestureRecognizers.Add(tapGestureRecognizer);

2. Use platform dependent events

If you want to use more specific platform events like, "mouse over" and so on you can use the dot net maui handlers.

See here: https://learn.microsoft.com/en-us/dotnet/maui/user-interface/handlers/customize

2.1 Sample

In this example we want to set a property called IsMouseOver depending whether the mouse is over the control CardView or not. This can look for example like the following:

//in App.xamls.cs
Microsoft.Maui.Handlers.ElementHandler.ElementMapper.AppendToMapping("IsMouseOver", (handler, view) =>
        {
#if WINDOWS
            if (view is CardView cardView && handler.PlatformView is ContentPanel contentPanel)
            {
                contentPanel.PointerEntered += (sender, e) =>
                {
                    view.Dispatcher.Dispatch(()=>cardView.IsMouseOver = true);
                };
                contentPanel.PointerExited += (sender, e) =>
                {
                    view.Dispatcher.Dispatch(()=>cardView.IsMouseOver = false);         
                };
            }
#endif
         });

The full example can be found here. Note you may need to use the Dispatcher of the control when dealing with events.

Update: On android GestureRecognizers will fire only on the control where you added the gesture. If you want to capture the events of the child controls you need to add the GestureRecognizers as well for the childs. For the windows platform you don't need to add the GestureRecognizers on the child control. It will be inherited. Hopefully they will fix this later on android.

Briefkasten
  • 1,964
  • 2
  • 25
  • 53