KEYCODE_ESCAPE源码实例Demo

android.view.KeyEvent#KEYCODE_ESCAPE源码实例Demo

下面列出了android.view.KeyEvent#KEYCODE_ESCAPE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: M3UPlayer   文件: Player.java

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_MENU) {
		if (mMenu.getVisibility() != View.VISIBLE) {
			mMenu.setVisibility(View.VISIBLE);
			mMenu.requestLayout();
			mMenu.requestFocus();
		} else {
			choose(mPosition);
		}
		return true;
	} else if (keyCode == KeyEvent.KEYCODE_BACK
			|| keyCode == KeyEvent.KEYCODE_ESCAPE) {
		if (mMenu.getVisibility() == View.VISIBLE) {
			mMenu.setVisibility(View.INVISIBLE);
			return true;
		}
	}
	return super.onKeyDown(keyCode, event);
}
 
源代码2 项目: android_9.0.0_r45   文件: Dialog.java

/**
 * A key was released.
 * <p>
 * Default implementation consumes {@link KeyEvent#KEYCODE_BACK KEYCODE_BACK}
 * and, as of {@link android.os.Build.VERSION_CODES#P P}, {@link KeyEvent#KEYCODE_ESCAPE
 * KEYCODE_ESCAPE} to close the dialog.
 *
 * @see #onKeyDown
 * @see android.view.KeyEvent
 */
@Override
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE)
            && event.isTracking()
            && !event.isCanceled()) {
        onBackPressed();
        return true;
    }
    return false;
}
 

@Override
public void handleKeyboardEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN && mTab.getActivity() != null) {
        if (mTab.getActivity().onKeyDown(event.getKeyCode(), event)) return;

        // Handle the Escape key here (instead of in KeyboardShortcuts.java), so it doesn't
        // interfere with other parts of the activity (e.g. the URL bar).
        if (event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE && event.hasNoModifiers()) {
            WebContents wc = mTab.getWebContents();
            if (wc != null) wc.stop();
            return;
        }
    }
    handleMediaKey(event);
}
 
源代码4 项目: TVRemoteIME   文件: IMEService.java

private boolean handleKeyboardFocusEvent(int keyCode){
	if(mInputView != null) {
		if (Environment.needDebug) {
			Environment.debug(TAG, "handleKeyboardFocusEvent:" + keyCode);
		}
		switch (keyCode) {
			case KeyEvent.KEYCODE_DPAD_UP:
			case KeyEvent.KEYCODE_DPAD_DOWN:
			case KeyEvent.KEYCODE_DPAD_LEFT:
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				if(mInputView.isShown()) {
					requestNextButtonFocus(keyCode);
					return true;
				}
				break;
			case KeyEvent.KEYCODE_ENTER:
			case KeyEvent.KEYCODE_DPAD_CENTER:
				if (mInputView.isShown() && focusedView != null) {
					clickButtonByKey(focusedView);
					return true;
				}
				break;
			case KeyEvent.KEYCODE_CAPS_LOCK:
				toggleCapsState(true);
				return true;
			case KeyEvent.KEYCODE_ESCAPE:
			case KeyEvent.KEYCODE_BACK:
				if (mInputView.isShown()){
					if(helpDialog != null && helpDialog.isShown()){
						helpDialog.setVisibility(View.GONE);
					}else {
						this.finishInput();
					}
					return true;
				}
				break;
		}
	}
	return false;
}
 

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (mChildrenFocused && event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE
            && event.getAction() == KeyEvent.ACTION_UP) {
        mChildrenFocused = false;
        requestFocus();
        return true;
    }
    return super.dispatchKeyEvent(event);
}
 

@Override
public void handleKeyboardEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN && mTab.getActivity() != null) {
        if (mTab.getActivity().onKeyDown(event.getKeyCode(), event)) return;

        // Handle the Escape key here (instead of in KeyboardShortcuts.java), so it doesn't
        // interfere with other parts of the activity (e.g. the URL bar).
        if (event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE && event.hasNoModifiers()) {
            WebContents wc = mTab.getWebContents();
            if (wc != null) wc.stop();
            return;
        }
    }
    handleMediaKey(event);
}
 

private boolean processKeyEvent(KeyEvent event) {
  if (event == null) {
    return false;
  }

  if (event.getRepeatCount() > 1) {
    return false;
  }

  //noinspection SimplifiableIfStatement
  if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
      || event.getKeyCode() == KeyEvent.KEYCODE_HOME
      || event.getKeyCode() == KeyEvent.KEYCODE_CALL
      || event.getKeyCode() == KeyEvent.KEYCODE_ENDCALL) {
    return false;
  }

  // Enter and Esc are used to accept/dismiss dialogs. However, the default shortcuts
  // involve Enter and Esc (with modifiers), so we should only trap Enter and Esc without
  // modifiers.
  boolean isDialogNavigation =
      event.getKeyCode() == KeyEvent.KEYCODE_ENTER
          || event.getKeyCode() == KeyEvent.KEYCODE_ESCAPE;
  if (isDialogNavigation && event.hasNoModifiers()) {
    return false;
  }

  return event.getAction() == KeyEvent.ACTION_DOWN;
}
 

/**
 * 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) {
    if (isCancelable()) {
      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);
    }
    if (mCallback != null) {
      mCallback.onTimeSet(mTimePicker,
        mTimePicker.getHours(), mTimePicker.getMinutes(), mTimePickerEnd.getHours(), mTimePickerEnd.getMinutes());
    }
    dismiss();
    return true;
  } else if (keyCode == KeyEvent.KEYCODE_DEL) {
    if (mInKbMode) {
      if (!mTypedTimes.isEmpty()) {
        int deleted = deleteLastTypedKey();
        String deletedKeyStr;
        if (deleted == getAmOrPmKeyCode(AM)) {
          deletedKeyStr = mAmText;
        } else if (deleted == getAmOrPmKeyCode(PM)) {
          deletedKeyStr = mPmText;
        } else {
          deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
        }
        if (tabHost.getCurrentTab() == 1) {
          Utils.tryAccessibilityAnnounce(mTimePicker,
            String.format(mDeletedKeyFormat, deletedKeyStr));
        } else {
          Utils.tryAccessibilityAnnounce(mTimePickerEnd,
            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(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
    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;
}
 
源代码9 项目: 365browser   文件: KeyboardShortcuts.java

/**
 * This should be called from the Activity's dispatchKeyEvent() to handle keyboard shortcuts.
 *
 * Note: dispatchKeyEvent() is called before the active view or web page gets a chance to handle
 * the key event. So the keys handled here cannot be overridden by any view or web page.
 *
 * @param event The KeyEvent to handle.
 * @param activity The ChromeActivity in which the key was pressed.
 * @param uiInitialized Whether the UI has been initialized. If this is false, most keys will
 *                      not be handled.
 * @return True if the event was handled. False if the event was ignored. Null if the event
 *         should be handled by the activity's parent class.
 */
@SuppressFBWarnings("NP_BOOLEAN_RETURN_NULL")
public static Boolean dispatchKeyEvent(KeyEvent event, ChromeActivity activity,
        boolean uiInitialized) {
    int keyCode = event.getKeyCode();
    if (!uiInitialized) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_MENU) return true;
        return null;
    }

    switch (keyCode) {
        case KeyEvent.KEYCODE_SEARCH:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                activity.onMenuOrKeyboardAction(R.id.focus_url_bar, false);
            }
            // Always consume the SEARCH key events to prevent android from showing
            // the default app search UI, which locks up Chrome.
            return true;
        case KeyEvent.KEYCODE_MENU:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                activity.onMenuOrKeyboardAction(R.id.show_menu, false);
            }
            return true;
        case KeyEvent.KEYCODE_ESCAPE:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                if (activity.exitFullscreenIfShowing()) return true;
            }
            break;
        case KeyEvent.KEYCODE_TV:
        case KeyEvent.KEYCODE_GUIDE:
        case KeyEvent.KEYCODE_DVR:
        case KeyEvent.KEYCODE_AVR_INPUT:
        case KeyEvent.KEYCODE_AVR_POWER:
        case KeyEvent.KEYCODE_STB_INPUT:
        case KeyEvent.KEYCODE_STB_POWER:
        case KeyEvent.KEYCODE_TV_INPUT:
        case KeyEvent.KEYCODE_TV_POWER:
        case KeyEvent.KEYCODE_WINDOW:
            // Do not consume the AV device-related keys so that the system will take
            // an appropriate action, such as switching to TV mode.
            return false;
    }

    return null;
}
 
源代码10 项目: DateTimepicker   文件: TimePickerDialog.java

/**
 * 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);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker,
                    mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    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(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        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;
}
 
源代码11 项目: narrate-android   文件: TimePickerDialog.java

/**
 * 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);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker,
                    mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    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(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        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;
}
 
源代码12 项目: AndroidChromium   文件: LocationBarLayout.java

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (KeyNavigationUtil.isGoDown(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()) {
        int suggestionCount = mSuggestionListAdapter.getCount();
        if (mSuggestionList.getSelectedItemPosition() < suggestionCount - 1) {
            if (suggestionCount > 0) mIgnoreOmniboxItemSelection = false;
        } else {
            // Do not pass down events when the last item is already selected as it will
            // dismiss the suggestion list.
            return true;
        }

        if (mSuggestionList.getSelectedItemPosition()
                == ListView.INVALID_POSITION) {
            // When clearing the selection after a text change, state is not reset
            // correctly so hitting down again will cause it to start from the previous
            // selection point. We still have to send the key down event to let the list
            // view items take focus, but then we select the first item explicitly.
            boolean result = mSuggestionList.onKeyDown(keyCode, event);
            mSuggestionList.setSelection(0);
            return result;
        } else {
            return mSuggestionList.onKeyDown(keyCode, event);
        }
    } else if (KeyNavigationUtil.isGoUp(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()) {
        if (mSuggestionList.getSelectedItemPosition() != 0
                && mSuggestionListAdapter.getCount() > 0) {
            mIgnoreOmniboxItemSelection = false;
        }
        return mSuggestionList.onKeyDown(keyCode, event);
    } else if (KeyNavigationUtil.isGoRight(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()
            && mSuggestionList.getSelectedItemPosition()
                    != ListView.INVALID_POSITION) {
        OmniboxResultItem selectedItem =
                (OmniboxResultItem) mSuggestionListAdapter.getItem(
                        mSuggestionList.getSelectedItemPosition());
        // Set the UrlBar text to empty, so that it will trigger a text change when we
        // set the text to the suggestion again.
        setUrlBarText("", null);
        mUrlBar.setText(selectedItem.getSuggestion().getFillIntoEdit());
        mSuggestionList.setSelection(0);
        mUrlBar.setSelection(mUrlBar.getText().length());
        return true;
    } else if (KeyNavigationUtil.isEnter(event)
            && LocationBarLayout.this.getVisibility() == VISIBLE) {
        UiUtils.hideKeyboard(mUrlBar);
        mSuggestionSelectionInProgress = true;
        final String urlText = mUrlBar.getQueryText();
        if (mNativeInitialized) {
            findMatchAndLoadUrl(urlText);
        } else {
            mDeferredNativeRunnables.add(new Runnable() {
                @Override
                public void run() {
                    findMatchAndLoadUrl(urlText);
                }
            });
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            // Tell the framework to start tracking this event.
            getKeyDispatcherState().startTracking(event, this);
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            getKeyDispatcherState().handleUpEvent(event);
            if (event.isTracking() && !event.isCanceled()) {
                backKeyPressed();
                return true;
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_ESCAPE) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            revertChanges();
            return true;
        }
    }
    return false;
}
 
源代码13 项目: AndroidChromium   文件: KeyboardShortcuts.java

/**
 * This should be called from the Activity's dispatchKeyEvent() to handle keyboard shortcuts.
 *
 * Note: dispatchKeyEvent() is called before the active view or web page gets a chance to handle
 * the key event. So the keys handled here cannot be overridden by any view or web page.
 *
 * @param event The KeyEvent to handle.
 * @param activity The ChromeActivity in which the key was pressed.
 * @param uiInitialized Whether the UI has been initialized. If this is false, most keys will
 *                      not be handled.
 * @return True if the event was handled. False if the event was ignored. Null if the event
 *         should be handled by the activity's parent class.
 */
@SuppressFBWarnings("NP_BOOLEAN_RETURN_NULL")
public static Boolean dispatchKeyEvent(KeyEvent event, ChromeActivity activity,
        boolean uiInitialized) {
    int keyCode = event.getKeyCode();
    if (!uiInitialized) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_MENU) return true;
        return null;
    }

    switch (keyCode) {
        case KeyEvent.KEYCODE_SEARCH:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                activity.onMenuOrKeyboardAction(R.id.focus_url_bar, false);
            }
            // Always consume the SEARCH key events to prevent android from showing
            // the default app search UI, which locks up Chrome.
            return true;
        case KeyEvent.KEYCODE_MENU:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                activity.onMenuOrKeyboardAction(R.id.show_menu, false);
            }
            return true;
        case KeyEvent.KEYCODE_ESCAPE:
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                if (activity.exitFullscreenIfShowing()) return true;
            }
            break;
        case KeyEvent.KEYCODE_TV:
        case KeyEvent.KEYCODE_GUIDE:
        case KeyEvent.KEYCODE_DVR:
        case KeyEvent.KEYCODE_AVR_INPUT:
        case KeyEvent.KEYCODE_AVR_POWER:
        case KeyEvent.KEYCODE_STB_INPUT:
        case KeyEvent.KEYCODE_STB_POWER:
        case KeyEvent.KEYCODE_TV_INPUT:
        case KeyEvent.KEYCODE_TV_POWER:
        case KeyEvent.KEYCODE_WINDOW:
            // Do not consume the AV device-related keys so that the system will take
            // an appropriate action, such as switching to TV mode.
            return false;
    }

    return null;
}
 

/**
 * 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;
}
 
源代码15 项目: Blackbulb   文件: TimePickerDialog.java

/**
 * 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) {
        if(isCancelable()) 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);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(this,
                    mTimePicker.getHours(), mTimePicker.getMinutes(), mTimePicker.getSeconds());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    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(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        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;
}
 

/**
 * 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);
        }
        if (onTimeSelectedListener != null) {
            onTimeSelectedListener.onTimeSelected(mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        //dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format(Locale.getDefault(), "%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(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        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;
}
 

/**
 * 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) {
        if(isCancelable()) 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);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker,
                    mTimePicker.getHours(), mTimePicker.getMinutes(),mTimePickerEnd.getHours(), mTimePickerEnd.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    deletedKeyStr = mPmText;
                } else {
                    deletedKeyStr = String.format("%d", getValFromKeyCode(deleted));
                }
                if(tabHost.getCurrentTab()==0){
                    Utils.tryAccessibilityAnnounce(mTimePicker,
                            String.format(mDeletedKeyFormat, deletedKeyStr));
                }else{
                    Utils.tryAccessibilityAnnounce(mTimePickerEnd,
                            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(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        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;
}
 
源代码18 项目: Conquer   文件: TimePickerDialog.java

/**
 * 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);
        }
        if (mCallback != null) {
            mCallback.onTimeSet(mTimePicker,
                    mTimePicker.getHours(), mTimePicker.getMinutes());
        }
        dismiss();
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_DEL) {
        if (mInKbMode) {
            if (!mTypedTimes.isEmpty()) {
                int deleted = deleteLastTypedKey();
                String deletedKeyStr;
                if (deleted == getAmOrPmKeyCode(AM)) {
                    deletedKeyStr = mAmText;
                } else if (deleted == getAmOrPmKeyCode(PM)) {
                    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(AM) || keyCode == getAmOrPmKeyCode(PM)))) {
        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;
}
 
源代码19 项目: 365browser   文件: LocationBarLayout.java

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (KeyNavigationUtil.isGoDown(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()) {
        int suggestionCount = mSuggestionListAdapter.getCount();
        if (mSuggestionList.getSelectedItemPosition() < suggestionCount - 1) {
            if (suggestionCount > 0) mIgnoreOmniboxItemSelection = false;
        } else {
            // Do not pass down events when the last item is already selected as it will
            // dismiss the suggestion list.
            return true;
        }

        if (mSuggestionList.getSelectedItemPosition()
                == ListView.INVALID_POSITION) {
            // When clearing the selection after a text change, state is not reset
            // correctly so hitting down again will cause it to start from the previous
            // selection point. We still have to send the key down event to let the list
            // view items take focus, but then we select the first item explicitly.
            boolean result = mSuggestionList.onKeyDown(keyCode, event);
            mSuggestionList.setSelection(0);
            return result;
        } else {
            return mSuggestionList.onKeyDown(keyCode, event);
        }
    } else if (KeyNavigationUtil.isGoUp(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()) {
        if (mSuggestionList.getSelectedItemPosition() != 0
                && mSuggestionListAdapter.getCount() > 0) {
            mIgnoreOmniboxItemSelection = false;
        }
        return mSuggestionList.onKeyDown(keyCode, event);
    } else if (KeyNavigationUtil.isGoRight(event)
            && mSuggestionList != null
            && mSuggestionList.isShown()
            && mSuggestionList.getSelectedItemPosition()
                    != ListView.INVALID_POSITION) {
        OmniboxResultItem selectedItem =
                (OmniboxResultItem) mSuggestionListAdapter.getItem(
                        mSuggestionList.getSelectedItemPosition());
        // Set the UrlBar text to empty, so that it will trigger a text change when we
        // set the text to the suggestion again.
        setUrlBarText("", null);
        mUrlBar.setText(selectedItem.getSuggestion().getFillIntoEdit());
        mSuggestionList.setSelection(0);
        mUrlBar.setSelection(mUrlBar.getText().length());
        return true;
    } else if (KeyNavigationUtil.isEnter(event)
            && LocationBarLayout.this.getVisibility() == VISIBLE) {
        UiUtils.hideKeyboard(mUrlBar);
        mSuggestionSelectionInProgress = true;
        final String urlText = mUrlBar.getTextWithAutocomplete();
        if (mNativeInitialized) {
            findMatchAndLoadUrl(urlText);
        } else {
            mDeferredNativeRunnables.add(new Runnable() {
                @Override
                public void run() {
                    findMatchAndLoadUrl(urlText);
                }
            });
        }
        return true;
    } else if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            // Tell the framework to start tracking this event.
            getKeyDispatcherState().startTracking(event, this);
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            getKeyDispatcherState().handleUpEvent(event);
            if (event.isTracking() && !event.isCanceled()) {
                backKeyPressed();
                return true;
            }
        }
    } else if (keyCode == KeyEvent.KEYCODE_ESCAPE) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            revertChanges();
            return true;
        }
    }
    return false;
}
 
源代码20 项目: android_9.0.0_r45   文件: Dialog.java

/**
 * A key was pressed down.
 * <p>
 * If the focused view didn't want this event, this method is called.
 * <p>
 * Default implementation consumes {@link KeyEvent#KEYCODE_BACK KEYCODE_BACK}
 * and, as of {@link android.os.Build.VERSION_CODES#P P}, {@link KeyEvent#KEYCODE_ESCAPE
 * KEYCODE_ESCAPE} to later handle them in {@link #onKeyUp}.
 * 
 * @see #onKeyUp
 * @see android.view.KeyEvent
 */
@Override
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE) {
        event.startTracking();
        return true;
    }

    return false;
}
 
源代码评论
动弹
沙发等你来抢
 方法所在类
 同类方法