Search code examples
android.netnativemaui

Raw/Native Events in .Net Maui


is there a way to distinguish / access raw and/or native events in .Net Maui?

E.g. I would like to be able to know what input I got, like mouse vs finger. Using touch on mobile I have pan and pinch with 2 fingers for moving/scrolling and zooming. But with a mouse I would like to be able to decide whether the wheel will be used for zooming or scrolling, or use the right click for pan. Or distinguish between those two and stylus/joystick/gamepad etc as well as inject new events into the application or converting events from one to another.

Something like this would also be needed for keyboard shortcuts and detecting stuff like mouseclick vs shift+mouseclick. And if you can send those events to either the os or the global application it could also serve for unit/integration testing (sending a click at a specific location and checking that the correct code is executed).


Solution

  • is there a way to distinguish / access raw and/or native events in .Net Maui?

    Maui is a cross platform frame. The event in the maui is just call the event of the different platform view.

    Such as when you declare an entry and use the Entry's TextChanged event. It just invokes the EditText's TextChanged event on the android and the MauiPaswordTextBox's TextChanged event on the windows. So you needn't to distinguish the events.

    I would like to be able to decide whether the wheel will be used for zooming or scrolling, or use the right click for pan

    This sounds like you just want to do something when the wheel scrolled or the mouse clicked. You can just add a event delegate to the control. Such as:

    In the xaml:

    <Entry x:Name="entry" BackgroundColor="Pink"/>
    

    And in the Page.cs:

    protected override void OnHandlerChanged()
          {
                
                base.OnHandlerChanged();
    #if WINDOWS
                Microsoft.Maui.Platform.MauiPasswordTextBox mauiPasswordTextBox = (Microsoft.Maui.Platform.MauiPasswordTextBox)entry.Handler.PlatformView;
                mauiPasswordTextBox.PointerWheelChanged += (s, arg) =>
                {
                      if (arg.GetCurrentPoint(mauiPasswordTextBox).Properties.MouseWheelDelta > 0)
                      {
                        // the value > 0 means the wheel scroll forward
                      }
                      if (arg.GetCurrentPoint(mauiPasswordTextBox).PointerDeviceType == Microsoft.UI.Input.PointerDeviceType.Mouse)
                      {
                        // this can distinguish the devicetype, contains mouse, pen and touch
                      }
                };
    #endif
          }
    

    Generally speaking, if you want to distinguish the input type need to use the platform native api on different platforms. So you can google the issue with the different platforms keyword such as Android, iOS and so on.