Example usage for android.view KeyEvent KEYCODE_DPAD_DOWN

List of usage examples for android.view KeyEvent KEYCODE_DPAD_DOWN

Introduction

In this page you can find the example usage for android.view KeyEvent KEYCODE_DPAD_DOWN.

Prototype

int KEYCODE_DPAD_DOWN

To view the source code for android.view KeyEvent KEYCODE_DPAD_DOWN.

Click Source Link

Document

Key code constant: Directional Pad Down key.

Usage

From source file:android.support.v7.widget.AbstractXpListPopupWindow.java

/**
 * Filter key down events. By forwarding key down events to this function,
 * views using non-modal ListPopupWindow can have it handle key selection of items.
 *
 * @param keyCode keyCode param passed to the host view's onKeyDown
 * @param event event param passed to the host view's onKeyDown
 * @return true if the event was handled, false if it was ignored.
 * @see #setModal(boolean)/*w  w  w  .ja v  a2s. c  om*/
 */
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // when the drop down is shown, we drive it directly
    if (isShowing()) {
        // the key events are forwarded to the list in the drop down view
        // note that ListView handles space but we don't want that to happen
        // also if selection is not currently in the drop down, then don't
        // let center or enter presses go there since that would cause it
        // to select one of its items
        if (keyCode != KeyEvent.KEYCODE_SPACE
                && (mDropDownList.getSelectedItemPosition() >= 0 || !isConfirmKey(keyCode))) {
            int curIndex = mDropDownList.getSelectedItemPosition();
            boolean consumed;

            final boolean below = !mPopup.isAboveAnchor();

            final ListAdapter adapter = mAdapter;

            boolean allEnabled;
            int firstItem = Integer.MAX_VALUE;
            int lastItem = Integer.MIN_VALUE;

            if (adapter != null) {
                allEnabled = adapter.areAllItemsEnabled();
                firstItem = allEnabled ? 0 : mDropDownList.lookForSelectablePosition(0, true);
                lastItem = allEnabled ? adapter.getCount() - 1
                        : mDropDownList.lookForSelectablePosition(adapter.getCount() - 1, false);
            }

            if ((below && keyCode == KeyEvent.KEYCODE_DPAD_UP && curIndex <= firstItem)
                    || (!below && keyCode == KeyEvent.KEYCODE_DPAD_DOWN && curIndex >= lastItem)) {
                // When the selection is at the top, we block the key
                // event to prevent focus from moving.
                clearListSelection();
                mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
                show();
                return true;
            } else {
                // WARNING: Please read the comment where mListSelectionHidden
                //          is declared
                mDropDownList.setListSelectionHidden(false);
            }

            consumed = mDropDownList.onKeyDown(keyCode, event);
            if (DEBUG)
                Log.v(TAG, "Key down: code=" + keyCode + " list consumed=" + consumed);

            if (consumed) {
                // If it handled the key event, then the user is
                // navigating in the list, so we should put it in front.
                mPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
                // Here's a little trick we need to do to make sure that
                // the list view is actually showing its focus indicator,
                // by ensuring it has focus and getting its window out
                // of touch mode.
                mDropDownList.requestFocusFromTouch();
                show();

                switch (keyCode) {
                // avoid passing the focus from the text view to the
                // next component
                case KeyEvent.KEYCODE_ENTER:
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_DPAD_DOWN:
                case KeyEvent.KEYCODE_DPAD_UP:
                    return true;
                }
            } else {
                if (below && keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    // when the selection is at the bottom, we block the
                    // event to avoid going to the next focusable widget
                    if (curIndex == lastItem) {
                        return true;
                    }
                } else if (!below && keyCode == KeyEvent.KEYCODE_DPAD_UP && curIndex == firstItem) {
                    return true;
                }
            }
        }
    }

    return false;
}

From source file:com.yek.keyboard.anysoftkeyboard.AnySoftKeyboard.java

private void onFunctionKey(final int primaryCode, final Keyboard.Key key, final int multiTapIndex,
        final int[] nearByKeyCodes, final boolean fromUI) {
    if (BuildConfig.DEBUG)
        Logger.d(TAG, "onFunctionKey %d", primaryCode);

    final InputConnection ic = getCurrentInputConnection();

    switch (primaryCode) {
    case KeyCodes.DELETE:
        if (ic == null)// if we don't want to do anything, lets check null first.
            break;
        // we do backword if the shift is pressed while pressing
        // backspace (like in a PC)
        if (mAskPrefs.useBackword() && mShiftKeyState.isPressed() && !mShiftKeyState.isLocked()) {
            handleBackWord(ic);// w ww .j a va  2 s . co  m
        } else {
            handleDeleteLastCharacter(false);
        }
        break;
    case KeyCodes.SHIFT:
        if (fromUI) {
            handleShift();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.SHIFT_LOCK:
        mShiftKeyState.toggleLocked();
        handleShift();
        break;
    case KeyCodes.DELETE_WORD:
        if (ic == null)// if we don't want to do anything, lets check
            // null first.
            break;
        handleBackWord(ic);
        break;
    case KeyCodes.CLEAR_INPUT:
        if (ic != null) {
            ic.beginBatchEdit();
            abortCorrectionAndResetPredictionState(false);
            ic.deleteSurroundingText(Integer.MAX_VALUE, Integer.MAX_VALUE);
            ic.endBatchEdit();
        }
        break;
    case KeyCodes.CTRL:
        if (fromUI) {
            handleControl();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.CTRL_LOCK:
        mControlKeyState.toggleLocked();
        handleControl();
        break;
    case KeyCodes.ARROW_LEFT:
    case KeyCodes.ARROW_RIGHT:
        final int keyEventKeyCode = primaryCode == KeyCodes.ARROW_LEFT ? KeyEvent.KEYCODE_DPAD_LEFT
                : KeyEvent.KEYCODE_DPAD_RIGHT;
        if (!handleSelectionExpending(keyEventKeyCode, ic, mGlobalSelectionStartPosition,
                mGlobalCursorPosition)) {
            sendDownUpKeyEvents(keyEventKeyCode);
        }
        break;
    case KeyCodes.ARROW_UP:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_UP);
        break;
    case KeyCodes.ARROW_DOWN:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_DOWN);
        break;
    case KeyCodes.MOVE_HOME:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            sendDownUpKeyEvents(0x0000007a/*API 11:KeyEvent.KEYCODE_MOVE_HOME*/);
        } else {
            if (ic != null) {
                CharSequence textBefore = ic.getTextBeforeCursor(1024, 0);
                if (!TextUtils.isEmpty(textBefore)) {
                    int newPosition = textBefore.length() - 1;
                    while (newPosition > 0) {
                        char chatAt = textBefore.charAt(newPosition - 1);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition--;
                    }
                    if (newPosition < 0)
                        newPosition = 0;
                    ic.setSelection(newPosition, newPosition);
                }
            }
        }
        break;
    case KeyCodes.MOVE_END:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            //API 11: KeyEvent.KEYCODE_MOVE_END
            sendDownUpKeyEvents(0x0000007b);
        } else {
            if (ic != null) {
                CharSequence textAfter = ic.getTextAfterCursor(1024, 0);
                if (!TextUtils.isEmpty(textAfter)) {
                    int newPosition = 1;
                    while (newPosition < textAfter.length()) {
                        char chatAt = textAfter.charAt(newPosition);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition++;
                    }
                    if (newPosition > textAfter.length())
                        newPosition = textAfter.length();
                    try {
                        CharSequence textBefore = ic.getTextBeforeCursor(Integer.MAX_VALUE, 0);
                        if (!TextUtils.isEmpty(textBefore)) {
                            newPosition = newPosition + textBefore.length();
                        }
                        ic.setSelection(newPosition, newPosition);
                    } catch (Throwable e/*I'm using Integer.MAX_VALUE, it's scary.*/) {
                        Logger.w(TAG, "Failed to getTextBeforeCursor.", e);
                    }
                }
            }
        }
        break;
    case KeyCodes.VOICE_INPUT:
        if (mVoiceRecognitionTrigger.isInstalled()) {
            mVoiceRecognitionTrigger
                    .startVoiceRecognition(getCurrentAlphabetKeyboard().getDefaultDictionaryLocale());
        } else {
            Intent voiceInputNotInstalledIntent = new Intent(getApplicationContext(),
                    VoiceInputNotInstalledActivity.class);
            voiceInputNotInstalledIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(voiceInputNotInstalledIntent);
        }
        break;
    case KeyCodes.CANCEL:
        handleClose();
        break;
    case KeyCodes.SETTINGS:
        showOptionsMenu();
        break;
    case KeyCodes.SPLIT_LAYOUT:
    case KeyCodes.MERGE_LAYOUT:
    case KeyCodes.COMPACT_LAYOUT_TO_RIGHT:
    case KeyCodes.COMPACT_LAYOUT_TO_LEFT:
        if (getInputView() != null) {
            mKeyboardInCondensedMode = CondenseType.fromKeyCode(primaryCode);
            setKeyboardForView(getCurrentKeyboard());
        }
        break;
    case KeyCodes.DOMAIN:
        onText(key, mAskPrefs.getDomainText());
        break;
    case KeyCodes.QUICK_TEXT:
        onQuickTextRequested(key);
        break;
    case KeyCodes.QUICK_TEXT_POPUP:
        onQuickTextKeyboardRequested(key);
        break;
    case KeyCodes.MODE_SYMOBLS:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.Symbols);
        break;
    case KeyCodes.MODE_ALPHABET:
        if (getKeyboardSwitcher().shouldPopupForLanguageSwitch()) {
            showLanguageSelectionDialog();
        } else {
            nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.Alphabet);
        }
        break;
    case KeyCodes.UTILITY_KEYBOARD:
        getInputView().openUtilityKeyboard();
        break;
    case KeyCodes.MODE_ALPHABET_POPUP:
        showLanguageSelectionDialog();
        break;
    case KeyCodes.ALT:
        nextAlterKeyboard(getCurrentInputEditorInfo());
        break;
    case KeyCodes.KEYBOARD_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.Any);
        break;
    case KeyCodes.KEYBOARD_REVERSE_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.PreviousAny);
        break;
    case KeyCodes.KEYBOARD_CYCLE_INSIDE_MODE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.AnyInsideMode);
        break;
    case KeyCodes.KEYBOARD_MODE_CHANGE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.OtherMode);
        break;
    case KeyCodes.CLIPBOARD_COPY:
    case KeyCodes.CLIPBOARD_PASTE:
    case KeyCodes.CLIPBOARD_CUT:
    case KeyCodes.CLIPBOARD_SELECT_ALL:
    case KeyCodes.CLIPBOARD_PASTE_POPUP:
    case KeyCodes.CLIPBOARD_SELECT:
        handleClipboardOperation(key, primaryCode, ic);
        //not allowing undo on-text in clipboard paste operations.
        if (primaryCode == KeyCodes.CLIPBOARD_PASTE)
            mCommittedWord = "";
        break;
    default:
        if (BuildConfig.DEBUG) {
            //this should not happen! We should handle ALL function keys.
            throw new RuntimeException("UNHANDLED FUNCTION KEY! primary code " + primaryCode);
        } else {
            Logger.w(TAG, "UNHANDLED FUNCTION KEY! primary code %d. Ignoring.", primaryCode);
        }
    }
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

private void onFunctionKey(final int primaryCode, final Key key, final int multiTapIndex,
        final int[] nearByKeyCodes, final boolean fromUI) {
    if (BuildConfig.DEBUG)
        Logger.d(TAG, "onFunctionKey %d", primaryCode);

    final InputConnection ic = getCurrentInputConnection();

    switch (primaryCode) {
    case KeyCodes.DELETE:
        if (ic == null)// if we don't want to do anything, lets check null first.
            break;
        // we do backword if the shift is pressed while pressing
        // backspace (like in a PC)
        if (mAskPrefs.useBackword() && mShiftKeyState.isPressed() && !mShiftKeyState.isLocked()) {
            handleBackWord(ic);//from   www . j  a v a  2  s  .co  m
        } else {
            handleDeleteLastCharacter(false);
        }
        break;
    case KeyCodes.SHIFT:
        if (fromUI) {
            handleShift();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.SHIFT_LOCK:
        mShiftKeyState.toggleLocked();
        handleShift();
        break;
    case KeyCodes.DELETE_WORD:
        if (ic == null)// if we don't want to do anything, lets check
            // null first.
            break;
        handleBackWord(ic);
        break;
    case KeyCodes.CLEAR_INPUT:
        if (ic != null) {
            ic.beginBatchEdit();
            abortCorrectionAndResetPredictionState(false);
            ic.deleteSurroundingText(Integer.MAX_VALUE, Integer.MAX_VALUE);
            ic.endBatchEdit();
        }
        break;
    case KeyCodes.CTRL:
        if (fromUI) {
            handleControl();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.CTRL_LOCK:
        mControlKeyState.toggleLocked();
        handleControl();
        break;
    case KeyCodes.ARROW_LEFT:
    case KeyCodes.ARROW_RIGHT:
        final int keyEventKeyCode = primaryCode == KeyCodes.ARROW_LEFT ? KeyEvent.KEYCODE_DPAD_LEFT
                : KeyEvent.KEYCODE_DPAD_RIGHT;
        if (!handleSelectionExpending(keyEventKeyCode, ic, mGlobalSelectionStartPosition,
                mGlobalCursorPosition)) {
            sendDownUpKeyEvents(keyEventKeyCode);
        }
        break;
    case KeyCodes.ARROW_UP:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_UP);
        break;
    case KeyCodes.ARROW_DOWN:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_DOWN);
        break;
    case KeyCodes.MOVE_HOME:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            sendDownUpKeyEvents(0x0000007a/*API 11:KeyEvent.KEYCODE_MOVE_HOME*/);
        } else {
            if (ic != null) {
                CharSequence textBefore = ic.getTextBeforeCursor(1024, 0);
                if (!TextUtils.isEmpty(textBefore)) {
                    int newPosition = textBefore.length() - 1;
                    while (newPosition > 0) {
                        char chatAt = textBefore.charAt(newPosition - 1);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition--;
                    }
                    if (newPosition < 0)
                        newPosition = 0;
                    ic.setSelection(newPosition, newPosition);
                }
            }
        }
        break;
    case KeyCodes.MOVE_END:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            //API 11: KeyEvent.KEYCODE_MOVE_END
            sendDownUpKeyEvents(0x0000007b);
        } else {
            if (ic != null) {
                CharSequence textAfter = ic.getTextAfterCursor(1024, 0);
                if (!TextUtils.isEmpty(textAfter)) {
                    int newPosition = 1;
                    while (newPosition < textAfter.length()) {
                        char chatAt = textAfter.charAt(newPosition);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition++;
                    }
                    if (newPosition > textAfter.length())
                        newPosition = textAfter.length();
                    try {
                        CharSequence textBefore = ic.getTextBeforeCursor(Integer.MAX_VALUE, 0);
                        if (!TextUtils.isEmpty(textBefore)) {
                            newPosition = newPosition + textBefore.length();
                        }
                        ic.setSelection(newPosition, newPosition);
                    } catch (Throwable e/*I'm using Integer.MAX_VALUE, it's scary.*/) {
                        Logger.w(TAG, "Failed to getTextBeforeCursor.", e);
                    }
                }
            }
        }
        break;
    case KeyCodes.VOICE_INPUT:
        if (mVoiceRecognitionTrigger.isInstalled()) {
            mVoiceRecognitionTrigger
                    .startVoiceRecognition(getCurrentAlphabetKeyboard().getDefaultDictionaryLocale());
        } else {
            Intent voiceInputNotInstalledIntent = new Intent(getApplicationContext(),
                    VoiceInputNotInstalledActivity.class);
            voiceInputNotInstalledIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(voiceInputNotInstalledIntent);
        }
        break;
    case KeyCodes.CANCEL:
        hideWindow();
        break;
    case KeyCodes.SETTINGS:
        showOptionsMenu();
        break;
    case KeyCodes.SPLIT_LAYOUT:
    case KeyCodes.MERGE_LAYOUT:
    case KeyCodes.COMPACT_LAYOUT_TO_RIGHT:
    case KeyCodes.COMPACT_LAYOUT_TO_LEFT:
        if (getInputView() != null) {
            mKeyboardInCondensedMode = CondenseType.fromKeyCode(primaryCode);
            setKeyboardForView(getCurrentKeyboard());
        }
        break;
    case KeyCodes.DOMAIN:
        onText(key, mAskPrefs.getDomainText());
        break;
    case KeyCodes.QUICK_TEXT:
        onQuickTextRequested(key);
        break;
    case KeyCodes.QUICK_TEXT_POPUP:
        onQuickTextKeyboardRequested(key);
        break;
    case KeyCodes.MODE_SYMOBLS:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Symbols);
        break;
    case KeyCodes.MODE_ALPHABET:
        if (getKeyboardSwitcher().shouldPopupForLanguageSwitch()) {
            showLanguageSelectionDialog();
        } else {
            nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Alphabet);
        }
        break;
    case KeyCodes.UTILITY_KEYBOARD:
        getInputView().openUtilityKeyboard();
        break;
    case KeyCodes.MODE_ALPHABET_POPUP:
        showLanguageSelectionDialog();
        break;
    case KeyCodes.ALT:
        nextAlterKeyboard(getCurrentInputEditorInfo());
        break;
    case KeyCodes.KEYBOARD_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Any);
        break;
    case KeyCodes.KEYBOARD_REVERSE_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.PreviousAny);
        break;
    case KeyCodes.KEYBOARD_CYCLE_INSIDE_MODE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.AnyInsideMode);
        break;
    case KeyCodes.KEYBOARD_MODE_CHANGE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.OtherMode);
        break;
    case KeyCodes.CLIPBOARD_COPY:
    case KeyCodes.CLIPBOARD_PASTE:
    case KeyCodes.CLIPBOARD_CUT:
    case KeyCodes.CLIPBOARD_SELECT_ALL:
    case KeyCodes.CLIPBOARD_PASTE_POPUP:
    case KeyCodes.CLIPBOARD_SELECT:
        handleClipboardOperation(key, primaryCode, ic);
        //not allowing undo on-text in clipboard paste operations.
        if (primaryCode == KeyCodes.CLIPBOARD_PASTE)
            mCommittedWord = "";
        break;
    default:
        if (BuildConfig.DEBUG) {
            //this should not happen! We should handle ALL function keys.
            throw new RuntimeException("UNHANDLED FUNCTION KEY! primary code " + primaryCode);
        } else {
            Logger.w(TAG, "UNHANDLED FUNCTION KEY! primary code %d. Ignoring.", primaryCode);
        }
    }
}

From source file:android.support.v17.leanback.widget.GridWidgetTest.java

public void testNonFocusableLoseInFastLayout() throws Throwable {
    mInstrumentation = getInstrumentation();
    Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
    intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.vertical_linear);
    int[] items = new int[300];
    for (int i = 0; i < items.length; i++) {
        items[i] = 480;//from ww  w  .  j a v  a2 s. c o m
    }
    intent.putExtra(GridActivity.EXTRA_ITEMS, items);
    intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
    intent.putExtra(GridActivity.EXTRA_REQUEST_LAYOUT_ONFOCUS, true);
    mOrientation = BaseGridView.VERTICAL;
    mNumRows = 1;
    int pressDown = 15;

    initActivity(intent);

    mGridView.setSelectedPositionSmooth(0);
    waitForScrollIdleAndItemAnimation(mVerifyLayout);

    for (int i = 0; i < pressDown; i++) {
        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
    }
    waitForScrollIdleAndItemAnimation(mVerifyLayout);
    assertFalse(mGridView.isFocused());

}

From source file:com.appunite.list.GridView.java

private boolean commonKey(int keyCode, int count, KeyEvent event) {
    if (mAdapter == null) {
        return false;
    }//from   w  ww .ja  v a 2s . c o m

    if (mDataChanged) {
        layoutChildren();
    }

    boolean handled = false;
    int action = event.getAction();

    if (action != KeyEvent.ACTION_UP) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (KeyEventCompat.hasNoModifiers(event)) {
                handled = resurrectSelectionIfNeeded() || arrowScroll(FOCUS_LEFT);
            }
            break;

        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (KeyEventCompat.hasNoModifiers(event)) {
                handled = resurrectSelectionIfNeeded() || arrowScroll(FOCUS_RIGHT);
            }
            break;

        case KeyEvent.KEYCODE_DPAD_UP:
            if (KeyEventCompat.hasNoModifiers(event)) {
                handled = resurrectSelectionIfNeeded() || arrowScroll(FOCUS_UP);
            } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_ALT_ON)) {
                handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_UP);
            }
            break;

        case KeyEvent.KEYCODE_DPAD_DOWN:
            if (KeyEventCompat.hasNoModifiers(event)) {
                handled = resurrectSelectionIfNeeded() || arrowScroll(FOCUS_DOWN);
            } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_ALT_ON)) {
                handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_DOWN);
            }
            break;

        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_ENTER:
            if (KeyEventCompat.hasNoModifiers(event)) {
                handled = resurrectSelectionIfNeeded();
                if (!handled && event.getRepeatCount() == 0 && getChildCount() > 0) {
                    keyPressed();
                    handled = true;
                }
            }
            break;

        case KeyEvent.KEYCODE_SPACE:
            if (mPopup == null || !mPopup.isShowing()) {
                if (KeyEventCompat.hasNoModifiers(event)) {
                    handled = resurrectSelectionIfNeeded() || pageScroll(FOCUS_DOWN);
                } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
                    handled = resurrectSelectionIfNeeded() || pageScroll(FOCUS_UP);
                }
            }
            break;

        case KeyEvent.KEYCODE_PAGE_UP:
            if (KeyEventCompat.hasNoModifiers(event)) {
                handled = resurrectSelectionIfNeeded() || pageScroll(FOCUS_UP);
            } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_ALT_ON)) {
                handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_UP);
            }
            break;

        case KeyEvent.KEYCODE_PAGE_DOWN:
            if (KeyEventCompat.hasNoModifiers(event)) {
                handled = resurrectSelectionIfNeeded() || pageScroll(FOCUS_DOWN);
            } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_ALT_ON)) {
                handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_DOWN);
            }
            break;

        case KeyEvent.KEYCODE_MOVE_HOME:
            if (KeyEventCompat.hasNoModifiers(event)) {
                handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_UP);
            }
            break;

        case KeyEvent.KEYCODE_MOVE_END:
            if (KeyEventCompat.hasNoModifiers(event)) {
                handled = resurrectSelectionIfNeeded() || fullScroll(FOCUS_DOWN);
            }
            break;

        case KeyEvent.KEYCODE_TAB:
            // XXX Sometimes it is useful to be able to TAB through the items in
            //     a GridView sequentially.  Unfortunately this can create an
            //     asymmetry in TAB navigation order unless the list selection
            //     always reverts to the top or bottom when receiving TAB focus from
            //     another widget.  Leaving this behavior disabled for now but
            //     perhaps it should be configurable (and more comprehensive).
            if (false) {
                if (KeyEventCompat.hasNoModifiers(event)) {
                    handled = resurrectSelectionIfNeeded() || sequenceScroll(FOCUS_FORWARD);
                } else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
                    handled = resurrectSelectionIfNeeded() || sequenceScroll(FOCUS_BACKWARD);
                }
            }
            break;
        }
    }

    if (handled) {
        return true;
    }

    if (sendToTextFilter(keyCode, count, event)) {
        return true;
    }

    switch (action) {
    case KeyEvent.ACTION_DOWN:
        return super.onKeyDown(keyCode, event);
    case KeyEvent.ACTION_UP:
        return super.onKeyUp(keyCode, event);
    case KeyEvent.ACTION_MULTIPLE:
        return super.onKeyMultiple(keyCode, count, event);
    default:
        return false;
    }
}

From source file:android.support.v17.leanback.widget.GridWidgetTest.java

public void testSmoothscrollerInterrupted() throws Throwable {
    mInstrumentation = getInstrumentation();
    Intent intent = new Intent(mInstrumentation.getContext(), GridActivity.class);
    intent.putExtra(GridActivity.EXTRA_LAYOUT_RESOURCE_ID, R.layout.vertical_linear);
    intent.putExtra(GridActivity.EXTRA_REQUEST_FOCUS_ONLAYOUT, true);
    int[] items = new int[100];
    for (int i = 0; i < items.length; i++) {
        items[i] = 680;/*  ww  w .java 2s .c  om*/
    }
    intent.putExtra(GridActivity.EXTRA_ITEMS, items);
    intent.putExtra(GridActivity.EXTRA_STAGGERED, false);
    mOrientation = BaseGridView.VERTICAL;
    mNumRows = 1;

    initActivity(intent);

    mGridView.setSelectedPositionSmooth(0);
    waitForScrollIdle(mVerifyLayout);
    assertTrue(mGridView.getChildAt(0).hasFocus());

    // Pressing lots of key to make sure smooth scroller is running
    for (int i = 0; i < 20; i++) {
        sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
    }
    Thread.sleep(100);
    int total = 0;
    while (mGridView.getLayoutManager().isSmoothScrolling()
            || mGridView.getScrollState() != BaseGridView.SCROLL_STATE_IDLE) {
        if ((total += 10) >= WAIT_FOR_SCROLL_IDLE_TIMEOUT_MS) {
            throw new RuntimeException("waitForScrollIdle Timeout");
        }
        try {
            // Repeatedly pressing to make sure pending keys does not drop to zero.
            Thread.sleep(10);
            sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
        } catch (InterruptedException ex) {
            break;
        }
    }

    assertTrue("LinearSmoothScroller would not use many RV.smoothScrollBy() calls",
            ((VerticalGridViewEx) mGridView).mSmoothScrollByCalled < 10);
}

From source file:administrator.example.com.myscrollview.VerticalViewPager.java

/**
 * 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.//from  w w w.j  a v a  2  s.c o  m
 *
 * @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_UP:
            handled = arrowScroll(FOCUS_UP);
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            handled = arrowScroll(FOCUS_DOWN);
            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);
                } /* end of if */
            } /* end of if */
            break;
        } /* end of switch */
    } /* end of if */
    return handled;
}

From source file:com.example.view.VerticalViewPager.java

/**
 * 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.//w  w w.  j  a v  a  2 s . c om
 * 
 * @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_UP:
            handled = arrowScroll(FOCUS_UP);
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:
            handled = arrowScroll(FOCUS_DOWN);
            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);
                } /* end of if */
            } /* end of if */
            break;
        } /* end of switch */
    } /* end of if */
    return handled;
}

From source file:org.bangbang.support.v4.widget.HListView.java

private boolean commonKey(int keyCode, int count, KeyEvent event) {
        if (mAdapter == null) {
            return false;
        }/*from ww w .j  av a2 s.  c  o  m*/

        if (mDataChanged) {
            layoutChildren();
        }

        boolean handled = false;
        int action = event.getAction();

        if (action != KeyEvent.ACTION_UP) {
            if (mSelectedPosition < 0) {
                switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_UP:
                case KeyEvent.KEYCODE_DPAD_DOWN:
                case KeyEvent.KEYCODE_DPAD_CENTER:
                case KeyEvent.KEYCODE_ENTER:
                case KeyEvent.KEYCODE_SPACE:
                    if (resurrectSelection()) {
                        return true;
                    }
                }
            }
            switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                if (!event.isAltPressed()) {
                    while (count > 0) {
                        handled = arrowScroll(FOCUS_LEFT);
                        count--;
                    }
                } else {
                    handled = fullScroll(FOCUS_LEFT);
                }
                break;

            case KeyEvent.KEYCODE_DPAD_RIGHT:
                if (!event.isAltPressed()) {
                    while (count > 0) {
                        handled = arrowScroll(FOCUS_RIGHT);
                        count--;
                    }
                } else {
                    handled = fullScroll(FOCUS_RIGHT);
                }
                break;

            case KeyEvent.KEYCODE_DPAD_UP:
                handled = handleHorizontalFocusWithinListItem(View.FOCUS_UP);
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                handled = handleHorizontalFocusWithinListItem(View.FOCUS_DOWN);
                break;

            case KeyEvent.KEYCODE_DPAD_CENTER:
            case KeyEvent.KEYCODE_ENTER:
                if (mItemCount > 0 && event.getRepeatCount() == 0) {
                    keyPressed();
                }
                handled = true;
                break;

            case KeyEvent.KEYCODE_SPACE:
                if (mPopup == null || !mPopup.isShowing()) {
                    if (!event.isShiftPressed()) {
                        pageScroll(FOCUS_DOWN);
                    } else {
                        pageScroll(FOCUS_UP);
                    }
                    handled = true;
                }
                break;
            }
        }

        if (!handled) {
            handled = sendToTextFilter(keyCode, count, event);
        }

        if (handled) {
            return true;
        } else {
            switch (action) {
            case KeyEvent.ACTION_DOWN:
                return super.onKeyDown(keyCode, event);

            case KeyEvent.ACTION_UP:
                return super.onKeyUp(keyCode, event);

            case KeyEvent.ACTION_MULTIPLE:
                return super.onKeyMultiple(keyCode, count, event);

            default: // shouldn't happen
                return false;
            }
        }
    }

From source file:com.tct.mail.ui.ConversationViewFragment.java

@Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        mOriginalKeyedView = view;/*from www. j av  a  2 s .  c om*/
    }

    if (mOriginalKeyedView != null) {
        final int id = mOriginalKeyedView.getId();
        final boolean isActionUp = keyEvent.getAction() == KeyEvent.ACTION_UP;
        final boolean isLeft = keyCode == KeyEvent.KEYCODE_DPAD_LEFT;
        final boolean isRight = keyCode == KeyEvent.KEYCODE_DPAD_RIGHT;
        final boolean isUp = keyCode == KeyEvent.KEYCODE_DPAD_UP;
        final boolean isDown = keyCode == KeyEvent.KEYCODE_DPAD_DOWN;

        // First we run the event by the controller
        // We manually check if the view+direction combination should shift focus away from the
        // conversation view to the thread list in two-pane landscape mode.
        final boolean isTwoPaneLand = mNavigationController.isTwoPaneLandscape();
        final boolean navigateAway = mConversationContainer.shouldNavigateAway(id, isLeft, isTwoPaneLand);
        if (mNavigationController.onInterceptKeyFromCV(keyCode, keyEvent, navigateAway)) {
            return true;
        }

        // If controller didn't handle the event, check directional interception.
        if ((isLeft || isRight)
                && mConversationContainer.shouldInterceptLeftRightEvents(id, isLeft, isRight, isTwoPaneLand)) {
            return true;
        } else if (isUp || isDown) {
            // We don't do anything on up/down for overlay
            if (id == R.id.conversation_topmost_overlay) {
                return true;
            }

            // We manually handle up/down navigation through the overlay items because the
            // system's default isn't optimal for two-pane landscape since it's not a real list.
            final int position = mConversationContainer.getViewPosition(mOriginalKeyedView);
            final View next = mConversationContainer.getNextOverlayView(position, isDown);
            if (next != null) {
                if (isActionUp) {
                    next.requestFocus();

                    // Make sure that v is in view
                    final int[] coords = new int[2];
                    next.getLocationOnScreen(coords);
                    final int bottom = coords[1] + next.getHeight();
                    if (bottom > mMaxScreenHeight) {
                        mWebView.scrollBy(0, bottom - mMaxScreenHeight);
                    } else if (coords[1] < mTopOfVisibleScreen) {
                        mWebView.scrollBy(0, coords[1] - mTopOfVisibleScreen);
                    }
                }
                return true;
            } else {
                // Special case two end points
                // Start is marked as index 1 because we are currently not allowing focus on
                // conversation view header.
                if ((position == mConversationContainer.getOverlayCount() - 1 && isDown)
                        || (position == 1 && isUp)) {
                    mTopmostOverlay.requestFocus();
                    // Scroll to the the top if we hit the first item
                    if (isUp) {
                        mWebView.scrollTo(0, 0);
                    }
                    return true;
                }
            }
        }

        // Finally we handle the special keys
        if (keyCode == KeyEvent.KEYCODE_BACK && id != R.id.conversation_topmost_overlay) {
            if (isActionUp) {
                mTopmostOverlay.requestFocus();
            }
            //TS: wenggangjin 2014-11-21 EMAIL BUGFIX_845619 MOD_S
            //                return true;
            return false;
            //TS: wenggangjin 2014-11-21 EMAIL BUGFIX_845619 MOD_E
        } else if (keyCode == KeyEvent.KEYCODE_ENTER && id == R.id.conversation_topmost_overlay) {
            if (isActionUp) {
                mConversationContainer.focusFirstMessageHeader();
                mWebView.scrollTo(0, 0);
            }
            return true;
        }
    }
    return false;
}