I have a Xamarin Android form (content page) and I want when the user presses the a certain keyboard key to take a certain action, like calling a method. I can not find anywhere any solution to that. If there is anyone knowing how to do this I would really appreciate his/her help.
Thank you
Try overriding DispatchKeyEvent
in your MainActivity
public override bool DispatchKeyEvent(KeyEvent e)
{
// if the Keycode is A .. there's lots of these
//you'll have to checkout the Keycode class for more options
//this example will fire the following code when 'A' is pressed
if (e.KeyCode == Keycode.A)
{
//invoke your method here
}
//return base if needed, otherwise keystrokes won't get automatically passed into textboxes etc and stuff will act wacky
return base.DispatchKeyEvent(e);
}
if this doesn't work you can also override OnKeyDown
and OnKeyUp
and OnKey
AFAIK
Difference between onKey(), OnKeyDown() and dispatchKeyEvent() methods provided by Android?