Is there an event on a page/grid like "anyKeyPressed"?
Unfortunately, no. And for mobile platform (Android/iOS), there's no such event as "anyKeyPressed" especially for physical keyboard.
If not considered frontend only, you can achieve it on UWP:
- Create an Inteface called IKeyboardListener in your Shared Project:
namespace Forms2
{
public interface IKeyboardListener
{
}
}
- Add this to your UWP's App.xaml.cs:
namespace Forms2.UWP
{
sealed partial class App : Application, IKeyboardListener
{
....
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
....
Windows.UI.Core.CoreWindow.GetForCurrentThread().KeyDown += HandleKeyDown;
}
public void HandleKeyDown(Windows.UI.Core.CoreWindow window, Windows.UI.Core.KeyEventArgs e)
{
MessagingCenter.Send<IKeyboardListener, string>(this, "KeyboardListener", e.VirtualKey.ToString());
}
}
}
- Use this on the page you want to execute the return command:
namespace Forms2
{
public partial class Page1 : ContentPage
{
public Page1()
{
InitializeComponent();
MessagingCenter.Subscribe<IKeyboardListener, string>(this, "KeyboardListener", (sender, args) => {
Debug.WriteLine(args);
if (args!=null)
{
Navigation.PopAsync();
}
});
}
}
}
If any key is pressed, you can return previous page.