c# – Capture keystroke Android

Question:

I am making an Android application and I need to capture the keys that are pressed on the keyboard. I am new to programming in Xamarin and it would help me a lot if someone could guide me.

Answer:

The common thing is to do it with the onKeyDown() method,

for example if we want to detect when the "Back" key is clicked:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        //Implemenetar accción,Se pulso la tecla back!
    }
    return super.onKeyDown(keyCode, event);
}

https://developer.xamarin.com/api/member/Android.App.Activity.OnKeyDown/p/Android.Views.Keycode/Android.Views.KeyEvent/

  • Here you can find a list of all the constants, to detect any key:

http://developer.android.com/intl/en/reference/android/view/KeyEvent.html

  • You can also use the onKeyUp() method , but the difference is that it is executed when the key is released.
Scroll to Top