Here are some precision to Roman answer
BaseInputConnection mInputConnection = new BaseInputConnection( findViewById(R.id.main_content), true);
KeyEvent kd = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU);
KeyEvent ku = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MENU);
mInputConnection.sendKeyEvent(kd);
mInputConnection.sendKeyEvent(ku);
BaseInputConnection mInputConnection = new BaseInputConnection(targetView, true);
mInputConnection.sendKeyEvent(new KeyEvent(...));
KeyEvent: Each key press is described by a sequence of key events, Key events are generally accompanied by a key code.
KeyCode: is a constant that define the event, and is defined inside KeyEvent Class.
For example if you want to detect the KeyEvent "ENTER" :
@Override
public boolean onKeyDown( int keyCode, KeyEvent event ) {
if( keyCode == KeyEvent.KEYCODE_ENTER) {
//Do something...
return true;
}
return super.onKeyDown( keyCode, event );
}
Read the docs
Object used to report key and button events. Each key press is described by a sequence of key events. A key press starts with a key event with ACTION_DOWN. If the key is held sufficiently long that it repeats, then the initial down is followed additional key events with ACTION_DOWN and a non-zero value for getRepeatCount(). The last key event is a ACTION_UP for the key up. If the key press is canceled, the key up event will have the FLAG_CANCELED flag set.
Answer to this question should be twofold. It is determined by the way how the key was generated. If it was press on the hardware key, then both approaches described below are valid. If it was press on the software key, then it depends on actual context.
1.) If key was result of the pressing on the soft keyboard that was obtained by long press on the Menu key:
You need to carefully override the following function:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
//your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
2.) If your activity contains EditText, and softkeyboard was obtained from it, then first approach does not work because key event was already consumed by EditText. You need to use text changed Listener:
mMyEditText.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
/*This method is called to notify you that, within s, the count characters beginning at start are about to be replaced by new text with length after. It is an error to attempt to make changes to s from this callback.*/
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
);
Zelimir code works if your ssoo version is up to 2.3 If you have this code running in 2.3 it will not works at all, the way to control the event keys in all ssoo version is with dispatchKeyEvent()
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.d("hello", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
With this, you can control the key pressed in a webview or wherever you are. The only bug is that you can not control the "action button" ... don't know why.