下面列出了android.view.KeyEvent#KEYCODE_TAB 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* You can call this function yourself to have the scroll view perform
* scrolling from a key event, just as if the event had been dispatched to
* it by the view hierarchy.
*
* @param event The key event to execute.
* @return Return true if the event was handled, else false.
*/
public boolean executeKeyEvent(KeyEvent event) {
boolean handled = false;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_LEFT:
handled = arrowScroll(FOCUS_LEFT);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
handled = arrowScroll(FOCUS_RIGHT);
break;
case KeyEvent.KEYCODE_TAB:
if (Build.VERSION.SDK_INT >= 11) {
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// before Android 3.0. Ignore the tab key on those devices.
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
}
break;
}
}
return handled;
}
/**
* You can call this function yourself to have the scroll view perform
* scrolling from a key event, just as if the event had been dispatched to
* it by the view hierarchy.
*
* @param event The key event to execute.
* @return Return true if the event was handled, else false.
*/
public boolean executeKeyEvent(KeyEvent event) {
boolean handled = false;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_LEFT:
handled = arrowScroll(FOCUS_LEFT);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
handled = arrowScroll(FOCUS_RIGHT);
break;
case KeyEvent.KEYCODE_TAB:
if (Build.VERSION.SDK_INT >= 11) {
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// before Android 3.0. Ignore the tab key on those devices.
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
}
break;
}
}
return handled;
}
/**
* You can call this function yourself to have the scroll view perform
* scrolling from a key event, just as if the event had been dispatched to
* it by the view hierarchy.
*
* @param event The key event to execute.
* @return Return true if the event was handled, else false.
*/
public boolean executeKeyEvent(KeyEvent event) {
boolean handled = false;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_LEFT:
handled = arrowScroll(FOCUS_LEFT);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
handled = arrowScroll(FOCUS_RIGHT);
break;
case KeyEvent.KEYCODE_TAB:
if (Build.VERSION.SDK_INT >= 11) {
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// before Android 3.0. Ignore the tab key on those devices.
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
}
break;
}
}
return handled;
}
/**
* Monitor key presses in this view to see if the user types
* any commit keys, which consist of ENTER, TAB, or DPAD_CENTER.
* If the user has entered text that has contact matches and types
* a commit key, create a chip from the topmost matching contact.
* If the user has entered text that has no contact matches and types
* a commit key, then create a chip from the text they have entered.
*/
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_TAB:
if (event.hasNoModifiers()) {
if (mSelectedChip != null) {
clearSelectedChip();
} else {
commitDefault();
}
}
break;
}
return super.onKeyUp(keyCode, event);
}
/**
* You can call this function yourself to have the scroll view perform
* scrolling from a key event, just as if the event had been dispatched to
* it by the view hierarchy.
*
* @param event The key event to execute.
* @return Return true if the event was handled, else false.
*/
public boolean executeKeyEvent(KeyEvent event) {
boolean handled = false;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_LEFT:
handled = arrowScroll(FOCUS_LEFT);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
handled = arrowScroll(FOCUS_RIGHT);
break;
case KeyEvent.KEYCODE_TAB:
if (Build.VERSION.SDK_INT >= 11) {
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// before Android 3.0. Ignore the tab key on those devices.
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
}
break;
}
}
return handled;
}
/**
* You can call this function yourself to have the scroll view perform
* scrolling from a key event, just as if the event had been dispatched to
* it by the view hierarchy.
*
* @param event The key event to execute.
* @return Return true if the event was handled, else false.
*/
public boolean executeKeyEvent(KeyEvent event) {
boolean handled = false;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_LEFT:
handled = arrowScroll(FOCUS_LEFT);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
handled = arrowScroll(FOCUS_RIGHT);
break;
case KeyEvent.KEYCODE_TAB:
if (Build.VERSION.SDK_INT >= 11) {
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// before Android 3.0. Ignore the tab key on those devices.
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
}
break;
}
}
return handled;
}
/**
* You can call this function yourself to have the scroll view perform
* scrolling from a key event, just as if the event had been dispatched to
* it by the view hierarchy.
*
* @param event The key event to execute.
* @return Return true if the event was handled, else false.
*/
public boolean executeKeyEvent(KeyEvent event) {
boolean handled = false;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_LEFT:
handled = arrowScroll(FOCUS_LEFT);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
handled = arrowScroll(FOCUS_RIGHT);
break;
case KeyEvent.KEYCODE_TAB:
if (Build.VERSION.SDK_INT >= 11) {
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// before Android 3.0. Ignore the tab key on those devices.
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
}
break;
}
}
return handled;
}
@Override
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
boolean handled = false;
switch (keyCode) {
case KeyEvent.KEYCODE_TAB:
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_DPAD_CENTER:
if (event.hasNoModifiers()) {
shouldFocusNext = true;
handled = true;
}
break;
case KeyEvent.KEYCODE_DEL:
handled = !canDeleteSelection(1) || deleteSelectedObject();
break;
}
return handled || super.onKeyDown(keyCode, event);
}
/**
* You can call this function yourself to have the scroll view perform
* scrolling from a key event, just as if the event had been dispatched to
* it by the view hierarchy.
*
* @param event The key event to execute.
* @return Return true if the event was handled, else false.
*/
public boolean executeKeyEvent(KeyEvent event) {
boolean handled = false;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_LEFT:
handled = arrowScroll(FOCUS_LEFT);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
handled = arrowScroll(FOCUS_RIGHT);
break;
case KeyEvent.KEYCODE_TAB:
if (Build.VERSION.SDK_INT >= 11) {
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// before Android 3.0. Ignore the tab key on those devices.
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
}
break;
}
}
return handled;
}
/**
* Monitor key presses in this view to see if the user types any commit keys, which consist of ENTER, TAB, or
* DPAD_CENTER. If the user has entered text that has contact matches and types a commit key, create a chip from the
* topmost matching contact. If the user has entered text that has no contact matches and types a commit key, then
* create a chip from the text they have entered.
*/
@Override
public boolean onKeyUp(final int keyCode,final KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_TAB:
if(event.hasNoModifiers())
if(mSelectedChip!=null)
clearSelectedChip();
else commitDefault();
break;
}
return super.onKeyUp(keyCode,event);
}
/**
* You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy.
*
* @param event The key event to execute.
*
* @return Return true if the event was handled, else false.
*/
public boolean executeKeyEvent(KeyEvent event)
{
boolean handled = false;
if ( event.getAction() == KeyEvent.ACTION_DOWN )
{
switch ( event.getKeyCode() )
{
case KeyEvent.KEYCODE_DPAD_LEFT:
handled = arrowScroll( FOCUS_LEFT );
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
handled = arrowScroll( FOCUS_RIGHT );
break;
case KeyEvent.KEYCODE_TAB:
if ( Build.VERSION.SDK_INT >= 11 )
{
// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
// before Android 3.0. Ignore the tab key on those devices.
if ( event.hasNoModifiers() )
{
handled = arrowScroll( FOCUS_FORWARD );
}
else if ( event.hasModifiers( KeyEvent.META_SHIFT_ON ) )
{
handled = arrowScroll( FOCUS_BACKWARD );
}
}
break;
}
}
return handled;
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_TAB && event.getAction() == KeyEvent.ACTION_DOWN) {
if (event.hasNoModifiers()) {
View forward = getNextFocusForward();
if (forward != null) return forward.requestFocus();
} else if (event.isShiftPressed()) {
View backward = getNextFocusBackward();
if (backward != null) return backward.requestFocus();
}
} else if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
return handleEnterKeyPress();
}
return false;
}
@Test
public void testActivateLastThumb_tab_unhandled() {
slider.requestFocus();
slider.setFocusedThumbIndex(countTestValues() - 1);
activateFocusedThumb();
KeyEventBuilder tab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB);
boolean handledDown = slider.dispatchKeyEvent(tab.buildDown());
boolean handledUp = slider.dispatchKeyEvent(tab.buildUp());
assertThat(handledDown).isFalse();
assertThat(handledUp).isFalse();
}
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_TAB && event.getAction() == KeyEvent.ACTION_DOWN) {
if (event.hasNoModifiers()) {
View forward = getNextFocusForward();
if (forward != null) return forward.requestFocus();
} else if (event.isShiftPressed()) {
View backward = getNextFocusBackward();
if (backward != null) return backward.requestFocus();
}
} else if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
return handleEnterKeyPress();
}
return false;
}
@Test
public void testFocusLastThumb_tab_unhandled() {
slider.requestFocus();
slider.setFocusedThumbIndex(countTestValues() - 1);
KeyEventBuilder tab = new KeyEventBuilder(KeyEvent.KEYCODE_TAB);
boolean handledDown = slider.dispatchKeyEvent(tab.buildDown());
boolean handledUp = slider.dispatchKeyEvent(tab.buildUp());
assertThat(handledDown).isFalse();
assertThat(handledUp).isFalse();
}
/**
* You can call this function yourself to have the scroll view perform
* scrolling from a key event, just as if the event had been dispatched to
* it by the view hierarchy.
*
* @param event The key event to execute.
* @return Return true if the event was handled, else false.
*/
public boolean executeKeyEvent(@NonNull KeyEvent event) {
boolean handled = false;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_LEFT:
if (event.hasModifiers(KeyEvent.META_ALT_ON)) {
handled = pageLeft();
} else {
handled = arrowScroll(FOCUS_LEFT);
}
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
if (event.hasModifiers(KeyEvent.META_ALT_ON)) {
handled = pageRight();
} else {
handled = arrowScroll(FOCUS_RIGHT);
}
break;
case KeyEvent.KEYCODE_TAB:
if (event.hasNoModifiers()) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
break;
}
}
return handled;
}
public boolean shouldOverrideKeyEvent(KeyEvent event) {
int keyCode = event.getKeyCode();
// We need to send almost every key to WebKit. However:
// 1. We don't want to block the device on the renderer for
// some keys like menu, home, call.
// 2. There are no WebKit equivalents for some of these keys
// (see app/keyboard_codes_win.h)
// Note that these are not the same set as KeyEvent.isSystemKey:
// for instance, AKEYCODE_MEDIA_* will be dispatched to webkit.
if (keyCode == KeyEvent.KEYCODE_MENU ||
keyCode == KeyEvent.KEYCODE_HOME ||
keyCode == KeyEvent.KEYCODE_BACK ||
keyCode == KeyEvent.KEYCODE_CALL ||
keyCode == KeyEvent.KEYCODE_ENDCALL ||
keyCode == KeyEvent.KEYCODE_POWER ||
keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
keyCode == KeyEvent.KEYCODE_CAMERA ||
keyCode == KeyEvent.KEYCODE_FOCUS ||
keyCode == KeyEvent.KEYCODE_VOLUME_DOWN ||
keyCode == KeyEvent.KEYCODE_VOLUME_MUTE ||
keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
return true;
}
// We also have to intercept some shortcuts before we send them to the ContentView.
if (event.isCtrlPressed() && (
keyCode == KeyEvent.KEYCODE_TAB ||
keyCode == KeyEvent.KEYCODE_W ||
keyCode == KeyEvent.KEYCODE_F4)) {
return true;
}
return false;
}
/**
* React to the user typing while in the suggestions list. First, check for
* action keys. If not handled, try refocusing regular characters into the
* EditText.
*/
private boolean onSuggestionsKey(View v, int keyCode, KeyEvent event) {
// guard against possible race conditions (late arrival after dismiss)
if (mSearchable == null) {
return false;
}
if (mSuggestionsAdapter == null) {
return false;
}
if (event.getAction() == KeyEvent.ACTION_DOWN && KeyEventCompat.hasNoModifiers(event)) {
// First, check for enter or search (both of which we'll treat as a
// "click")
if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_SEARCH
|| keyCode == KeyEvent.KEYCODE_TAB) {
int position = mQueryTextView.getListSelection();
return onItemClicked(position, KeyEvent.KEYCODE_UNKNOWN, null);
}
// Next, check for left/right moves, which we use to "return" the
// user to the edit view
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
// give "focus" to text editor, with cursor at the beginning if
// left key, at end if right key
// TODO: Reverse left/right for right-to-left languages, e.g.
// Arabic
int selPoint = (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) ? 0 : mQueryTextView
.length();
mQueryTextView.setSelection(selPoint);
mQueryTextView.setListSelection(0);
mQueryTextView.clearListSelection();
ensureImeVisible(mQueryTextView, true);
return true;
}
// Next, check for an "up and out" move
if (keyCode == KeyEvent.KEYCODE_DPAD_UP && 0 == mQueryTextView.getListSelection()) {
// TODO: restoreUserQuery();
// let ACTV complete the move
return false;
}
// Next, check for an "action key"
// TODO SearchableInfo.ActionKeyInfo actionKey = mSearchable.findActionKey(keyCode);
// TODO if ((actionKey != null)
// TODO && ((actionKey.getSuggestActionMsg() != null) || (actionKey
// TODO .getSuggestActionMsgColumn() != null))) {
// TODO // launch suggestion using action key column
// TODO int position = mQueryTextView.getListSelection();
// TODO if (position != ListView.INVALID_POSITION) {
// TODO Cursor c = mSuggestionsAdapter.getCursor();
// TODO if (c.moveToPosition(position)) {
// TODO final String actionMsg = getActionKeyMessage(c, actionKey);
// TODO if (actionMsg != null && (actionMsg.length() > 0)) {
// TODO return onItemClicked(position, keyCode, actionMsg);
// TODO }
// TODO }
// TODO }
// TODO }
}
return false;
}
/**
* CTRL + C copy
* CTRL + V paste
* CTRL + B: compile
* CTRL + R generate
* CTRL + X cut
* CTRL + Z undo
* CTRL + Y redo
* CTRL + Q quit
* CTRL + S save
* CTRL + O open
* CTRL + F find
* CTRL + H find and replace
* CTRL + L format code
* CTRL + G: goto line
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (DLog.DEBUG) Log.w(TAG, "onKeyDown: " + keyCode + " " + event);
if (handleControlKey(keyCode, event, false)) {
return true;
}
if (event.isCtrlPressed() || mKeyListener.mControlKey.isActive()) {
// Log.i(TAG, "onKeyDown: process");
switch (keyCode) {
case KeyEvent.KEYCODE_A:
selectAll();
return true;
case KeyEvent.KEYCODE_X:
cut();
return true;
case KeyEvent.KEYCODE_C:
copy();
return true;
case KeyEvent.KEYCODE_V:
paste();
return true;
case KeyEvent.KEYCODE_G: //go to line
if (editorControl != null)
editorControl.goToLine();
return true;
case KeyEvent.KEYCODE_L: //format
if (editorControl != null)
editorControl.formatCode();
return true;
case KeyEvent.KEYCODE_Z:
if (canUndo()) {
undo();
}
return true;
case KeyEvent.KEYCODE_Y:
if (canRedo()) {
redo();
}
return true;
case KeyEvent.KEYCODE_S:
if (editorControl != null)
editorControl.saveCurrentFile();
return true;
case KeyEvent.KEYCODE_N:
if (editorControl != null)
editorControl.saveAs();
return true;
default:
return super.onKeyDown(keyCode, event);
}
} else {
switch (keyCode) {
case KeyEvent.KEYCODE_TAB:
String textToInsert = mSettings.getTabStr();
int start, end;
start = Math.max(getSelectionStart(), 0);
end = Math.max(getSelectionEnd(), 0);
getText().replace(Math.min(start, end), Math.max(start, end),
textToInsert, 0, textToInsert.length());
return true;
default:
try {
return super.onKeyDown(keyCode, event);
} catch (Exception e) {
}
return false;
}
}
}
/**
* For keyboard mode, processes key events.
* @param keyCode the pressed key.
* @return true if the key was successfully processed, false otherwise.
*/
private boolean processKeyUp(int keyCode) {
if (keyCode == KeyEvent.KEYCODE_ESCAPE || keyCode == KeyEvent.KEYCODE_BACK) {
dismiss();
return true;
} else if (keyCode == KeyEvent.KEYCODE_TAB) {
if(mInKbMode) {
if (isTypedTimeFullyLegal()) {
finishKbMode(true);
}
return true;
}
} else if (keyCode == KeyEvent.KEYCODE_ENTER) {
if (mInKbMode) {
if (!isTypedTimeFullyLegal()) {
return true;
}
finishKbMode(false);
}
onTimeSet(mTimePicker, mTimePicker.getHours(), mTimePicker.getMinutes());
return true;
} else if (keyCode == KeyEvent.KEYCODE_DEL) {
if (mInKbMode) {
if (!mTypedTimes.isEmpty()) {
int deleted = deleteLastTypedKey();
String deletedKeyStr;
if (deleted == getAmOrPmKeyCode(HALF_DAY_1)) {
deletedKeyStr = mAmText;
} else if (deleted == getAmOrPmKeyCode(HALF_DAY_2)) {
deletedKeyStr = mPmText;
} else {
deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
}
Utils.tryAccessibilityAnnounce(mTimePicker,
String.format(mDeletedKeyFormat, deletedKeyStr));
updateDisplay(true);
}
}
} else if (keyCode == KeyEvent.KEYCODE_0 || keyCode == KeyEvent.KEYCODE_1
|| keyCode == KeyEvent.KEYCODE_2 || keyCode == KeyEvent.KEYCODE_3
|| keyCode == KeyEvent.KEYCODE_4 || keyCode == KeyEvent.KEYCODE_5
|| keyCode == KeyEvent.KEYCODE_6 || keyCode == KeyEvent.KEYCODE_7
|| keyCode == KeyEvent.KEYCODE_8 || keyCode == KeyEvent.KEYCODE_9
|| (!mIs24HourMode &&
(keyCode == getAmOrPmKeyCode(HALF_DAY_1) || keyCode == getAmOrPmKeyCode(HALF_DAY_2)))) {
if (!mInKbMode) {
if (mTimePicker == null) {
// Something's wrong, because time picker should definitely not be null.
Log.e(TAG, "Unable to initiate keyboard mode, TimePicker was null.");
return true;
}
mTypedTimes.clear();
tryStartingKbMode(keyCode);
return true;
}
// We're already in keyboard mode.
if (addKeyIfLegal(keyCode)) {
updateDisplay(false);
}
return true;
}
return false;
}