Capture key pressed Android

5

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

    
asked by Maray 27.04.2016 в 17:39
source

2 answers

4

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

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);
}

link

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

link

  • You can also use the method onKeyUp () , but the difference is that it is executed when you release the key.
answered by 27.04.2016 в 17:50
0
  

For keyboard events on Xamarin Android you must implement the interface KeyEvent.ICallback .

The interface has these methods:

  • OnKeyDown (int, KeyEvent)
  • OnKeyLongPress (int, KeyEvent)
  • OnKeyMultiple (int, int, KeyEvent)
  • OnKeyUp (int, KeyEvent)

Xamarin's KeyEvent Android documentation: Link

If you are in an Activity, you would have to do it in the following way:

public class MiActivity : Activity , KeyEvent.ICallback
{ 
    public override bool OnKeyUp(Keycode keyCode, KeyEvent e)
    {
        //codigo asociado a la key presionada
        return base.OnKeyUp(keyCode, e);
    }
}
    
answered by 27.04.2016 в 17:52