@Override public boolean onKeyDown(int keyCode, KeyEvent event) { // Galaxy Note sends key events for the stylus that are outside of the // valid keyCode range (see bug 758427) if (keyCode > KeyEvent.getMaxKeyCode()) return true; // This method is called only if the key event was not handled // by any of the views, which usually means the edit box lost focus if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_MENU || keyCode == KeyEvent.KEYCODE_DPAD_UP || keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT || keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_DEL || keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { return super.onKeyDown(keyCode, event); } else if (keyCode == KeyEvent.KEYCODE_SEARCH) { mText.setText(""); mText.requestFocus(); InputMethodManager imm = (InputMethodManager) mText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(mText, InputMethodManager.SHOW_IMPLICIT); return true; } else { int selStart = -1; int selEnd = -1; if (mText.hasSelection()) { selStart = mText.getSelectionStart(); selEnd = mText.getSelectionEnd(); } if (selStart >= 0) { // Restore the selection, which gets lost due to the focus switch mText.setSelection(selStart, selEnd); } // Manually dispatch the key event to the AwesomeBar before restoring (default) input // focus. dispatchKeyEvent() will update AwesomeBar's cursor position. mText.dispatchKeyEvent(event); int newCursorPos = mText.getSelectionEnd(); // requestFocusFromTouch() will select all AwesomeBar text, so we must restore cursor // position so subsequent typing does not overwrite all text. mText.requestFocusFromTouch(); mText.setSelection(newCursorPos); return true; } }