下面列出了android.view.KeyEvent#isLongPress ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (!mFilmstripVisible)
{
if (mCurrentModule.onKeyDown(keyCode, event))
{
return true;
}
// Prevent software keyboard or voice search from showing up.
if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_MENU)
{
if (event.isLongPress())
{
return true;
}
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
final int uniChar = event.getUnicodeChar();
final boolean handled = super.onKeyDown(keyCode, event);
final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
if (!handled && acceptFilter() && isKeyNotWhitespace) {
boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
keyCode, event);
if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
// something usable has been typed - start a search
// the typed text will be retrieved and cleared by
// showSearchDialog()
// If there are multiple keystrokes before the search dialog takes focus,
// onSearchRequested() will be called for every keystroke,
// but it is idempotent, so it's fine.
return onSearchRequested();
}
}
// Eat the long press event so the keyboard doesn't come up.
if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
return true;
}
return handled;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
final int uniChar = event.getUnicodeChar();
final boolean handled = super.onKeyDown(keyCode, event);
final boolean isKeyNotWhitespace = uniChar > 0
&& !Character.isWhitespace(uniChar);
if (!handled && acceptFilter() && isKeyNotWhitespace) {
boolean gotKey = TextKeyListener.getInstance().onKeyDown(
mWorkspace, mDefaultKeySsb, keyCode, event);
if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
return onSearchRequested();
}
}
if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
return true;
}
return handled;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH && !event.isLongPress()) {
openSearch();
return true;
}
int c = event.getUnicodeChar();
if (c > 32) {
if (mSearchEditText != null && !mSearchEditText.isFocused()) {
openSearch();
mSearchEditText.append(Character.toString((char) c));
return true;
}
}
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH && !event.isLongPress()) {
openSearch();
return true;
}
int c = event.getUnicodeChar();
if (c > 32) {
if (mSearchEditText != null && !mSearchEditText.isFocused()) {
openSearch();
mSearchEditText.append(Character.toString((char) c));
return true;
}
}
return super.onKeyUp(keyCode, event);
}
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
KeyEvent keyEvent = mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_HEADSETHOOK) {
if (keyEvent.getAction() == KeyEvent.ACTION_UP && !keyEvent.isLongPress()) {
onRemoteClick();
}
return true;
} else {
return super.onMediaButtonEvent(mediaButtonEvent);
}
}
@Override
public boolean onBackKey(KeyEvent event, boolean allowPop) {
if (event.getAction() == KeyEvent.ACTION_UP) {
if (!event.isLongPress() && allowPop) {
popInternal();
}
return true;
} else if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (event.isLongPress()) {
clear();
}
return true;
}
return false;
}
/**
* Handles the dispatching of the volume button events to one of the
* registered listeners. If there's a volume key long-press listener and
* there's no active global priority session, long-pressess will be sent to the
* long-press listener instead of adjusting volume.
*
* @param packageName The caller package.
* @param asSystemService {@code true} if the event sent to the session as if it was come
* from the system service instead of the app process. This helps sessions to
* distinguish between the key injection by the app and key events from the
* hardware devices. Should be used only when the volume key events aren't handled
* by foreground activity. {@code false} otherwise to tell session about the real
* caller.
* @param keyEvent a non-null KeyEvent whose key code is one of the
* {@link KeyEvent#KEYCODE_VOLUME_UP},
* {@link KeyEvent#KEYCODE_VOLUME_DOWN},
* or {@link KeyEvent#KEYCODE_VOLUME_MUTE}.
* @param stream stream type to adjust volume.
* @param musicOnly true if both UI nor haptic feedback aren't needed when adjust volume.
*/
@Override
public void dispatchVolumeKeyEvent(String packageName, boolean asSystemService,
KeyEvent keyEvent, int stream, boolean musicOnly) {
if (keyEvent == null ||
(keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_UP
&& keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_DOWN
&& keyEvent.getKeyCode() != KeyEvent.KEYCODE_VOLUME_MUTE)) {
Log.w(TAG, "Attempted to dispatch null or non-volume key event.");
return;
}
final int pid = Binder.getCallingPid();
final int uid = Binder.getCallingUid();
final long token = Binder.clearCallingIdentity();
if (DEBUG_KEY_EVENT) {
Log.d(TAG, "dispatchVolumeKeyEvent, pkg=" + packageName + ", pid=" + pid + ", uid="
+ uid + ", asSystem=" + asSystemService + ", event=" + keyEvent);
}
try {
synchronized (mLock) {
if (isGlobalPriorityActiveLocked()
|| mCurrentFullUserRecord.mOnVolumeKeyLongPressListener == null) {
dispatchVolumeKeyEventLocked(packageName, pid, uid, asSystemService,
keyEvent, stream, musicOnly);
} else {
// TODO: Consider the case when both volume up and down keys are pressed
// at the same time.
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
if (keyEvent.getRepeatCount() == 0) {
// Keeps the copy of the KeyEvent because it can be reused.
mCurrentFullUserRecord.mInitialDownVolumeKeyEvent =
KeyEvent.obtain(keyEvent);
mCurrentFullUserRecord.mInitialDownVolumeStream = stream;
mCurrentFullUserRecord.mInitialDownMusicOnly = musicOnly;
mHandler.sendMessageDelayed(
mHandler.obtainMessage(
MessageHandler.MSG_VOLUME_INITIAL_DOWN,
mCurrentFullUserRecord.mFullUserId, 0),
mLongPressTimeout);
}
if (keyEvent.getRepeatCount() > 0 || keyEvent.isLongPress()) {
mHandler.removeMessages(MessageHandler.MSG_VOLUME_INITIAL_DOWN);
if (mCurrentFullUserRecord.mInitialDownVolumeKeyEvent != null) {
dispatchVolumeKeyLongPressLocked(
mCurrentFullUserRecord.mInitialDownVolumeKeyEvent);
// Mark that the key is already handled.
mCurrentFullUserRecord.mInitialDownVolumeKeyEvent = null;
}
dispatchVolumeKeyLongPressLocked(keyEvent);
}
} else { // if up
mHandler.removeMessages(MessageHandler.MSG_VOLUME_INITIAL_DOWN);
if (mCurrentFullUserRecord.mInitialDownVolumeKeyEvent != null
&& mCurrentFullUserRecord.mInitialDownVolumeKeyEvent
.getDownTime() == keyEvent.getDownTime()) {
// Short-press. Should change volume.
dispatchVolumeKeyEventLocked(packageName, pid, uid, asSystemService,
mCurrentFullUserRecord.mInitialDownVolumeKeyEvent,
mCurrentFullUserRecord.mInitialDownVolumeStream,
mCurrentFullUserRecord.mInitialDownMusicOnly);
dispatchVolumeKeyEventLocked(packageName, pid, uid, asSystemService,
keyEvent, stream, musicOnly);
} else {
dispatchVolumeKeyLongPressLocked(keyEvent);
}
}
}
}
} finally {
Binder.restoreCallingIdentity(token);
}
}
@Override
public boolean isLongPressEvent(KeyEvent evt) {
return evt.isLongPress();
}
@Override
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
if (!isEnabled()) {
return super.onKeyDown(keyCode, event);
}
// If there's only one thumb, we can select it right away.
if (values.size() == 1) {
activeThumbIdx = 0;
}
// If there is no active thumb, key events will be used to pick the thumb to change.
if (activeThumbIdx == -1) {
Boolean handled = onKeyDownNoActiveThumb(keyCode, event);
return handled != null ? handled : super.onKeyDown(keyCode, event);
}
isLongPress |= event.isLongPress();
Float increment = calculateIncrementForKey(keyCode);
if (increment != null) {
if (snapActiveThumbToValue(values.get(activeThumbIdx) + increment)) {
updateHaloHotspot();
postInvalidate();
}
return true;
}
switch (keyCode) {
case KeyEvent.KEYCODE_TAB:
if (event.hasNoModifiers()) {
return moveFocus(1);
}
if (event.isShiftPressed()) {
return moveFocus(-1);
}
return false;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
activeThumbIdx = -1;
for (TooltipDrawable label : labels) {
ViewUtils.getContentViewOverlay(this).remove(label);
}
postInvalidate();
return true;
default:
// Nothing to do in this case.
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (DEBUG) Log.d(TAG, "[dispatchKeyEvent] event: " + event);
final int keyCode = event.getKeyCode();
// Not handled by the view hierarchy, does the action bar want it
// to cancel out of something special?
if (keyCode == KeyEvent.KEYCODE_BACK) {
final int action = event.getAction();
// Back cancels action modes first.
if (mActionMode != null) {
if (action == KeyEvent.ACTION_UP) {
mActionMode.finish();
}
if (DEBUG) Log.d(TAG, "[dispatchKeyEvent] returning true");
return true;
}
// Next collapse any expanded action views.
if (wActionBar != null && wActionBar.hasExpandedActionView()) {
if (action == KeyEvent.ACTION_UP) {
wActionBar.collapseActionView();
}
if (DEBUG) Log.d(TAG, "[dispatchKeyEvent] returning true");
return true;
}
}
boolean result = false;
if (keyCode == KeyEvent.KEYCODE_MENU && isReservingOverflow()) {
if (event.getAction() == KeyEvent.ACTION_DOWN && event.isLongPress()) {
mMenuKeyIsLongPress = true;
} else if (event.getAction() == KeyEvent.ACTION_UP) {
if (!mMenuKeyIsLongPress) {
if (mActionMode == null && wActionBar != null) {
if (wActionBar.isOverflowMenuShowing()) {
wActionBar.hideOverflowMenu();
} else {
wActionBar.showOverflowMenu();
}
}
result = true;
}
mMenuKeyIsLongPress = false;
}
}
if (DEBUG) Log.d(TAG, "[dispatchKeyEvent] returning " + result);
return result;
}