Keyboard shortcuts - .NET MAUI
In MAUI, there is no cross platform way to achieve setting a keyboard shortcut. But, you can listen for keypresses by overriding the default MainActivity.KeyUp
method for Android, and for iOS by setting up a UIKeyCommand
inside the default AppDelegate
.
Android implementation:
[Activity(
Theme = "@style/Maui.SplashTheme",
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 OnKeyUp([GeneratedEnum] Keycode keyCode, KeyEvent? e)
{
if (keyCode == Keycode.F && e.IsCtrlPressed)
{
//Apply any logic
return true;
}
return base.OnKeyUp(keyCode, e);
}
}
iOS implementation:
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
public UIKeyCommand FKeyCommand = UIKeyCommand.Create(
new NSString("f"),
UIKeyModifierFlags.Control,
new ObjCRuntime.Selector("Action:")
);
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
public override UIKeyCommand[] KeyCommands
=> new[] { FKeyCommand };
[Export("Action:")]
private void Excute(UIKeyCommand keyCommand)
{
//Apply any logic
}
}