Java Code Examples for android.view.KeyEvent#getRepeatCount()

The following examples show how to use android.view.KeyEvent#getRepeatCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileSelectorActivity.java    From JotaTextEditor with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    // BACK key short press => move to parent directory. if root finish the activity.
    // BACK key long  press => finish the activity.
    if ( keyCode == KeyEvent.KEYCODE_BACK ){
        if (event.getRepeatCount() == 0 && !m_strDirPath.equals("/") ) {
            if( m_strDirPath.lastIndexOf("/") <= 0 ) {
                m_strDirPath = m_strDirPath.substring(0, m_strDirPath.lastIndexOf("/") + 1 );
            } else {
                m_strDirPath = m_strDirPath.substring(0, m_strDirPath.lastIndexOf("/"));
            }
            fillList();
        }else{
            finish();
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 2
Source File: MaterialTapTargetPrompt.java    From styT with Apache License 2.0 6 votes vote down vote up
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
    if (mPromptOptions.getBackButtonDismissEnabled()
            && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
        KeyEvent.DispatcherState state = getKeyDispatcherState();
        if (state != null) {
            if (event.getAction() == KeyEvent.ACTION_DOWN
                    && event.getRepeatCount() == 0) {
                state.startTracking(event, this);
                return true;
            } else if (event.getAction() == KeyEvent.ACTION_UP
                    && !event.isCanceled() && state.isTracking(event)) {
                if (mPromptTouchedListener != null) {
                    mPromptTouchedListener.onNonFocalPressed();
                }
                return mPromptOptions.getAutoDismiss()
                        || super.dispatchKeyEventPreIme(event);
            }
        }
    }

    return super.dispatchKeyEventPreIme(event);
}
 
Example 3
Source File: AppMenu.java    From delion with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (mPopup == null || mPopup.getListView() == null) return false;

    if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
            event.startTracking();
            v.getKeyDispatcherState().startTracking(event, this);
            return true;
        } else if (event.getAction() == KeyEvent.ACTION_UP) {
            v.getKeyDispatcherState().handleUpEvent(event);
            if (event.isTracking() && !event.isCanceled()) {
                dismiss();
                return true;
            }
        }
    }
    return false;
}
 
Example 4
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void handleVoiceKeyEventLocked(String packageName, int pid, int uid,
        boolean asSystemService, KeyEvent keyEvent, boolean needWakeLock) {
    int action = keyEvent.getAction();
    boolean isLongPress = (keyEvent.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0;
    if (action == KeyEvent.ACTION_DOWN) {
        if (keyEvent.getRepeatCount() == 0) {
            mVoiceButtonDown = true;
            mVoiceButtonHandled = false;
        } else if (mVoiceButtonDown && !mVoiceButtonHandled && isLongPress) {
            mVoiceButtonHandled = true;
            startVoiceInput(needWakeLock);
        }
    } else if (action == KeyEvent.ACTION_UP) {
        if (mVoiceButtonDown) {
            mVoiceButtonDown = false;
            if (!mVoiceButtonHandled && !keyEvent.isCanceled()) {
                // Resend the down then send this event through
                KeyEvent downEvent = KeyEvent.changeAction(keyEvent, KeyEvent.ACTION_DOWN);
                dispatchMediaKeyEventLocked(packageName, pid, uid, asSystemService,
                        downEvent, needWakeLock);
                dispatchMediaKeyEventLocked(packageName, pid, uid, asSystemService,
                        keyEvent, needWakeLock);
            }
        }
    }
}
 
Example 5
Source File: FindToolbar.java    From delion with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    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()) {
                mFindToolbar.deactivate();
                return true;
            }
        }
    }
    return false;
}
 
Example 6
Source File: ArrowKeyMovementMethod.java    From JotaTextEditor with Apache License 2.0 5 votes vote down vote up
public boolean onKeyOther(TextView view, Spannable text, KeyEvent event) {
    int code = event.getKeyCode();
    if (code != KeyEvent.KEYCODE_UNKNOWN
            && event.getAction() == KeyEvent.ACTION_MULTIPLE) {
        int repeat = event.getRepeatCount();
        boolean handled = false;
        while ((--repeat) > 0) {
            handled |= executeDown(view, text, code,event);
        }
        return handled;
    }
    return false;
}
 
Example 7
Source File: FragmentActivity.java    From V.FlyoutTest with MIT License 5 votes vote down vote up
/**
 * Take care of calling onBackPressed() for pre-Eclair platforms.
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (android.os.Build.VERSION.SDK_INT < 5 /* ECLAIR */
            && keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        // Take care of calling this method on earlier versions of
        // the platform where it doesn't exist.
        onBackPressed();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
 
Example 8
Source File: PhotoModule.java    From Camera2 with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    switch (keyCode)
    {
        case KeyEvent.KEYCODE_VOLUME_UP:
        case KeyEvent.KEYCODE_VOLUME_DOWN:
        case KeyEvent.KEYCODE_FOCUS:
            if (/* TODO: mActivity.isInCameraApp() && */mFirstTimeInitialized && !mActivity.getCameraAppUI()
                    .isInIntentReview())
            {
                if (event.getRepeatCount() == 0)
                {
                    onShutterButtonFocus(true);
                }
                return true;
            }
            return false;
        case KeyEvent.KEYCODE_CAMERA:
            if (mFirstTimeInitialized && event.getRepeatCount() == 0)
            {
                onShutterButtonClick();
            }
            return true;
        case KeyEvent.KEYCODE_DPAD_CENTER:
            // If we get a dpad center event without any focused view, move
            // the focus to the shutter button and press it.
            if (mFirstTimeInitialized && event.getRepeatCount() == 0)
            {
                // Start auto-focus immediately to reduce shutter lag. After
                // the shutter button gets the focus, onShutterButtonFocus()
                // will be called again but it is fine.
                onShutterButtonFocus(true);
            }
            return true;
    }
    return false;
}
 
Example 9
Source File: ImageViewTouchBase.java    From yiim_v2 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
	if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
		event.startTracking();
		return true;
	}
	return super.onKeyDown(keyCode, event);
}
 
Example 10
Source File: FabSpeedDial.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
    if (isMenuOpen()
            && event.getKeyCode() == KeyEvent.KEYCODE_BACK
            && event.getAction() == KeyEvent.ACTION_UP
            && event.getRepeatCount() == 0) {
        closeMenu();
        return true;
    }

    return super.dispatchKeyEventPreIme(event);
}
 
Example 11
Source File: MainActivity.java    From Pixiv-Shaft with MIT License 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (baseBind.drawerLayout.isDrawerOpen(GravityCompat.START)) {
        baseBind.drawerLayout.closeDrawer(GravityCompat.START);
        return true;
    } else {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            exit();
            return true;
        }
        return false;
    }
}
 
Example 12
Source File: Instrumentation.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Send a key event to the currently focused window/view and wait for it to
 * be processed.  Finished at some point after the recipient has returned
 * from its event processing, though it may <em>not</em> have completely
 * finished reacting from the event -- for example, if it needs to update
 * its display as a result, it may still be in the process of doing that.
 * 
 * @param event The event to send to the current focus.
 */
public void sendKeySync(KeyEvent event) {
    validateNotAppThread();

    long downTime = event.getDownTime();
    long eventTime = event.getEventTime();
    int action = event.getAction();
    int code = event.getKeyCode();
    int repeatCount = event.getRepeatCount();
    int metaState = event.getMetaState();
    int deviceId = event.getDeviceId();
    int scancode = event.getScanCode();
    int source = event.getSource();
    int flags = event.getFlags();
    if (source == InputDevice.SOURCE_UNKNOWN) {
        source = InputDevice.SOURCE_KEYBOARD;
    }
    if (eventTime == 0) {
        eventTime = SystemClock.uptimeMillis();
    }
    if (downTime == 0) {
        downTime = eventTime;
    }
    KeyEvent newEvent = new KeyEvent(downTime, eventTime, action, code, repeatCount, metaState,
            deviceId, scancode, flags | KeyEvent.FLAG_FROM_SYSTEM, source);
    InputManager.getInstance().injectInputEvent(newEvent,
            InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH);
}
 
Example 13
Source File: UserDataActivity.java    From ToDoList with Apache License 2.0 5 votes vote down vote up
/**
 * 重写返回按钮监听刷新主界面头像
 */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        Intent intent = new Intent(UserDataActivity.this, LoginActivity.class);
        setResult(2, intent);
        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 14
Source File: FragmentActivity.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
public boolean onKeyDown(int i1, KeyEvent keyevent)
{
    if (android.os.Build.VERSION.SDK_INT < 5 && i1 == 4 && keyevent.getRepeatCount() == 0)
    {
        onBackPressed();
        return true;
    } else
    {
        return super.onKeyDown(i1, keyevent);
    }
}
 
Example 15
Source File: BubbleDialog.java    From HappyBubble with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
    if (isThroughEvent && keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
    {
        dismiss();
        mActivity.onBackPressed();
        mActivity = null;
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 16
Source File: BrightActivity.java    From AirFree-Client with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        finish();
        overridePendingTransition(R.anim.slide_up_in, R.anim.slide_down_out);
        return false;
    }
    return false;
}
 
Example 17
Source File: MediaSessionService.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
private void dispatchVolumeKeyEventLocked(String packageName, int pid, int uid,
        boolean asSystemService, KeyEvent keyEvent, int stream, boolean musicOnly) {
    boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN;
    boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP;
    int direction = 0;
    boolean isMute = false;
    switch (keyEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            direction = AudioManager.ADJUST_RAISE;
            break;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            direction = AudioManager.ADJUST_LOWER;
            break;
        case KeyEvent.KEYCODE_VOLUME_MUTE:
            isMute = true;
            break;
    }
    if (down || up) {
        int flags = AudioManager.FLAG_FROM_KEY;
        if (musicOnly) {
            // This flag is used when the screen is off to only affect active media.
            flags |= AudioManager.FLAG_ACTIVE_MEDIA_ONLY;
        } else {
            // These flags are consistent with the home screen
            if (up) {
                flags |= AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE;
            } else {
                flags |= AudioManager.FLAG_SHOW_UI | AudioManager.FLAG_VIBRATE;
            }
        }
        if (direction != 0) {
            // If this is action up we want to send a beep for non-music events
            if (up) {
                direction = 0;
            }
            dispatchAdjustVolumeLocked(packageName, pid, uid, asSystemService, stream,
                    direction, flags);
        } else if (isMute) {
            if (down && keyEvent.getRepeatCount() == 0) {
                dispatchAdjustVolumeLocked(packageName, pid, uid, asSystemService, stream,
                        AudioManager.ADJUST_TOGGLE_MUTE, flags);
            }
        }
    }
}
 
Example 18
Source File: YoukuBasePlayerActivity.java    From SimplifyReader with Apache License 2.0 4 votes vote down vote up
public boolean onKeyDown(int keyCode, KeyEvent event) {
    try {
        switch (keyCode) {
            case KeyEvent.KEYCODE_MENU:
                // 取消长按menu键弹出键盘事�?
                if (event.getRepeatCount() > 0) {
                    return true;
                }
                return mediaPlayerDelegate.isFullScreen;
            case KeyEvent.KEYCODE_BACK:
                // 点击过快的取消操�?
                if (event.getRepeatCount() > 0) {
                    return true;
                }
                if (!mediaPlayerDelegate.isDLNA) {
                    if (mediaPlayerDelegate.isFullScreen
                            && !isFromLocal()
                            && (mediaPlayerDelegate.videoInfo != null && !mediaPlayerDelegate.videoInfo.isHLS)) {
                        goSmall();
                        return true;
                    } else {
                        onkeyback();
                        return true;
                    }
                } else {
                    return true;
                }
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                return volumeDown();
            case KeyEvent.KEYCODE_VOLUME_UP:
                return volumeUp();
            case KeyEvent.KEYCODE_SEARCH:
                return mediaPlayerDelegate.isFullScreen;
            case 125:
                /** 有些手机载入中弹出popupwindow */
                return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return super.onKeyDown(keyCode, event);
}
 
Example 19
Source File: MediaButtonIntentReceiver.java    From RetroMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
public static boolean handleIntent(final Context context, final Intent intent) {
    final String intentAction = intent.getAction();
    if (Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
        final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return false;
        }

        final int keycode = event.getKeyCode();
        final int action = event.getAction();
        final long eventTime = event.getEventTime();

        String command = null;
        switch (keycode) {
            case KeyEvent.KEYCODE_MEDIA_STOP:
                command = ACTION_STOP;
                break;
            case KeyEvent.KEYCODE_HEADSETHOOK:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                command = ACTION_TOGGLE_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_NEXT:
                command = ACTION_SKIP;
                break;
            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                command = ACTION_REWIND;
                break;
            case KeyEvent.KEYCODE_MEDIA_PAUSE:
                command = ACTION_PAUSE;
                break;
            case KeyEvent.KEYCODE_MEDIA_PLAY:
                command = ACTION_PLAY;
                break;
        }
        if (command != null) {
            if (action == KeyEvent.ACTION_DOWN) {
                if (event.getRepeatCount() == 0) {
                    // Only consider the first event in a sequence, not the repeat events,
                    // so that we don't trigger in cases where the first event went to
                    // a different app (e.g. when the user ends a phone call by
                    // long pressing the headset button)

                    // The service may or may not be running, but we need to send it
                    // a command.
                    if (keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                        if (eventTime - mLastClickTime >= DOUBLE_CLICK) {
                            mClickCounter = 0;
                        }

                        mClickCounter++;
                        if (DEBUG) Log.v(TAG, "Got headset click, count = " + mClickCounter);
                        mHandler.removeMessages(MSG_HEADSET_DOUBLE_CLICK_TIMEOUT);

                        Message msg = mHandler.obtainMessage(
                                MSG_HEADSET_DOUBLE_CLICK_TIMEOUT, mClickCounter, 0, context);

                        long delay = mClickCounter < 3 ? DOUBLE_CLICK : 0;
                        if (mClickCounter >= 3) {
                            mClickCounter = 0;
                        }
                        mLastClickTime = eventTime;
                        acquireWakeLockAndSendMessage(context, msg, delay);
                    } else {
                        startService(context, command);
                    }
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 20
Source File: LocationBarLayout.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@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;
}