Keyboard shortcuts - Xamarin
Xamarin does not have support for hardware keyboard events. However, you can intercept key events inside the Android and iOS application.
- Android: hook into
onKeyUp
insideMainActivity.cs
- iOS: hook into
KeyCommands
using a custom renderer.
The code example below shows a sample implementation for iOS.
[assembly: ExportRenderer(typeof(ContentPage), typeof(KeyboardPageRenderer))]
namespace KeyCommandsInXamarinForms.iOS
{
public class KeyboardPageRenderer : PageRenderer
{
protected override void OnElementChanged(VisualElementChangedEventArgs e)
{
base.OnElementChanged(e);
if (e.OldElement != null || Element == null)
{
return;
}
// Create key command for Command + F
UIKeyCommand command1 = UIKeyCommand.Create(
new NSString("F"),
UIKeyModifierFlags.Command,
new ObjCRuntime.Selector("OnKeyPressed:")
);
this.AddKeyCommand(command1);
}
[Export("OnKeyPressed:")]
private void Excute(UIKeyCommand keyCommand)
{
// Find
}
public override bool CanBecomeFirstResponder
{
get
{
return true; // Key commands require first responder
}
}
}
}
The code example below shows a sample implementation for Android.
public override bool OnKeyUp([GeneratedEnum] Keycode keyCode, KeyEvent e)
{
if (keyCode == Keycode.KEYCODE_F && e.isCtrlPressed)
{
// Search
return true
}
return base.OnKeyUp(keyCode, e);
}