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

The following examples show how to use android.view.KeyEvent#isConfirmKey() . 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: Gallery.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (KeyEvent.isConfirmKey(keyCode)) {
        if (mReceivedInvokeKeyDown) {
            if (mItemCount > 0) {
                dispatchPress(mSelectedChild);
                postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        dispatchUnpress();
                    }
                }, ViewConfiguration.getPressedStateDuration());

                int selectedIndex = mSelectedPosition - mFirstPosition;
                performItemClick(getChildAt(selectedIndex), mSelectedPosition, mAdapter
                        .getItemId(mSelectedPosition));
            }
        }

        // Clear the flag
        mReceivedInvokeKeyDown = false;
        return true;
    }
    return super.onKeyUp(keyCode, event);
}
 
Example 2
Source File: ListPopupWindow.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Filter key up events. By forwarding key up 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 onKeyUp
 * @param event event param passed to the host view's onKeyUp
 * @return true if the event was handled, false if it was ignored.
 *
 * @see #setModal(boolean)
 * @see #onKeyDown(int, KeyEvent)
 */
public boolean onKeyUp(int keyCode, @NonNull KeyEvent event) {
    if (isShowing() && mDropDownList.getSelectedItemPosition() >= 0) {
        boolean consumed = mDropDownList.onKeyUp(keyCode, event);
        if (consumed && KeyEvent.isConfirmKey(keyCode)) {
            // if the list accepts the key events and the key event was a click, the text view
            // gets the selected item from the drop down as its content
            dismiss();
        }
        return consumed;
    }
    return false;
}
 
Example 3
Source File: ListPopupWindow.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * 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)
 * @see #onKeyUp(int, KeyEvent)
 */
public boolean onKeyDown(int keyCode, @NonNull 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
                        || !KeyEvent.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;
}