I am working on an MAUI 10 app, where I'd like to listen to a phyiscal button event as described here:
https://developer.android.com/training/wearables/user-input/physical-buttons

My issue is, that everytime I hit the phyiscal button, the default "Recent apps" on my Pixel Watch.

I already added some debugging infos, but it looks like the events are never fired:

[Activity(Theme = "@style/Maui.MainTheme", MainLauncher = true, LaunchMode = LaunchMode.SingleTop, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] public class MainActivity : MauiAppCompatActivity { public override bool DispatchKeyEvent(KeyEvent? e) { if (e != null) { Android.Util.Log.Debug("InTime", $"KeyEvent received: Action={e.Action}, KeyCode={e.KeyCode}"); if (e.KeyCode == Keycode.Stem1 || e.KeyCode == Keycode.Stem2 || e.KeyCode == Keycode.Stem3) { if (e.Action == KeyEventActions.Down) { e.StartTracking(); return true; } if (e.Action == KeyEventActions.Up && !e.IsCanceled && !e.IsLongPress) { var viewModel = IPlatformApplication.Current?.Services.GetService<MainViewModel>(); if (e.KeyCode == Keycode.Stem1) { viewModel?.ToggleTimer(); } else // Stem2 or Stem3 { viewModel?.ResetTimer(); } return true; } } } return base.DispatchKeyEvent(e); } public override bool OnKeyLongPress(Keycode keyCode, KeyEvent? e) { Android.Util.Log.Debug("InTime", $"Long Press received: KeyCode={keyCode}"); if (keyCode == Keycode.Stem1 || keyCode == Keycode.Stem2 || keyCode == Keycode.Stem3) { var viewModel = IPlatformApplication.Current?.Services.GetService<MainViewModel>(); viewModel?.ResetTimer(); return true; } return base.OnKeyLongPress(keyCode, e); } }

Any ideas how to correct it or what I need to change?

DominikAmon's user avatar

1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.