Example usage for android.view KeyEvent ACTION_DOWN

List of usage examples for android.view KeyEvent ACTION_DOWN

Introduction

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

Prototype

int ACTION_DOWN

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

Click Source Link

Document

#getAction value: the key has been pressed down.

Usage

From source file:cn.xiaowen.news.view.LazyViewPager.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  ww . j a va2s.  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_LEFT:
            handled = arrowScroll(FOCUS_LEFT);
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            handled = arrowScroll(FOCUS_RIGHT);
            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);
                }
            }
            break;
        }
    }
    return handled;
}

From source file:org.distantshoresmedia.keyboard.LatinIME.java

private void sendSpecialKey(int code) {
    if (!isConnectbot()) {
        commitTyped(getCurrentInputConnection(), true);
        sendModifiedKeyDownUp(code);/*from   ww w.ja va  2 s . c o m*/
        return;
    }

    // TODO(klausw): properly support xterm sequences for Ctrl/Alt modifiers?
    // See http://slackware.osuosl.org/slackware-12.0/source/l/ncurses/xterm.terminfo
    // and the output of "$ infocmp -1L". Support multiple sets, and optional 
    // true numpad keys?
    if (ESC_SEQUENCES == null) {
        ESC_SEQUENCES = new HashMap<Integer, String>();
        CTRL_SEQUENCES = new HashMap<Integer, Integer>();

        // VT escape sequences without leading Escape
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_HOME, "[1~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_END, "[4~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_PAGE_UP, "[5~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_PAGE_DOWN, "[6~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F1, "OP");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F2, "OQ");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F3, "OR");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F4, "OS");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F5, "[15~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F6, "[17~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F7, "[18~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F8, "[19~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F9, "[20~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F10, "[21~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F11, "[23~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F12, "[24~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_FORWARD_DEL, "[3~");
        ESC_SEQUENCES.put(-TKKeyboardView.KEYCODE_INSERT, "[2~");

        // Special ConnectBot hack: Ctrl-1 to Ctrl-0 for F1-F10.
        CTRL_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F1, KeyEvent.KEYCODE_1);
        CTRL_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F2, KeyEvent.KEYCODE_2);
        CTRL_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F3, KeyEvent.KEYCODE_3);
        CTRL_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F4, KeyEvent.KEYCODE_4);
        CTRL_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F5, KeyEvent.KEYCODE_5);
        CTRL_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F6, KeyEvent.KEYCODE_6);
        CTRL_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F7, KeyEvent.KEYCODE_7);
        CTRL_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F8, KeyEvent.KEYCODE_8);
        CTRL_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F9, KeyEvent.KEYCODE_9);
        CTRL_SEQUENCES.put(-TKKeyboardView.KEYCODE_FKEY_F10, KeyEvent.KEYCODE_0);

        // Natively supported by ConnectBot
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_DPAD_UP, "OA");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_DPAD_DOWN, "OB");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_DPAD_LEFT, "OD");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_DPAD_RIGHT, "OC");

        // No VT equivalents?
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_DPAD_CENTER, "");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_SYSRQ, "");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_BREAK, "");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_NUM_LOCK, "");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_SCROLL_LOCK, "");
    }
    InputConnection ic = getCurrentInputConnection();
    Integer ctrlseq = null;
    if (mConnectbotTabHack) {
        ctrlseq = CTRL_SEQUENCES.get(code);
    }
    String seq = ESC_SEQUENCES.get(code);

    if (ctrlseq != null) {
        if (mModAlt) {
            // send ESC prefix for "Alt"
            ic.commitText(Character.toString((char) 27), 1);
        }
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, ctrlseq));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, ctrlseq));
    } else if (seq != null) {
        if (mModAlt) {
            // send ESC prefix for "Alt"
            ic.commitText(Character.toString((char) 27), 1);
        }
        // send ESC prefix of escape sequence
        ic.commitText(Character.toString((char) 27), 1);
        ic.commitText(seq, 1);
    } else {
        // send key code, let connectbot handle it
        sendDownUpKeyEvents(code);
    }
    handleModifierKeysUp(false, false);
}

From source file:org.pocketworkstation.pckeyboard.LatinIME.java

private void sendSpecialKey(int code) {
    if (!isConnectbot()) {
        commitTyped(getCurrentInputConnection(), true);
        sendModifiedKeyDownUp(code);/*w w  w  .  jav a2  s  . c o  m*/
        return;
    }

    // TODO(klausw): properly support xterm sequences for Ctrl/Alt modifiers?
    // See http://slackware.osuosl.org/slackware-12.0/source/l/ncurses/xterm.terminfo
    // and the output of "$ infocmp -1L". Support multiple sets, and optional 
    // true numpad keys?
    if (ESC_SEQUENCES == null) {
        ESC_SEQUENCES = new HashMap<Integer, String>();
        CTRL_SEQUENCES = new HashMap<Integer, Integer>();

        // VT escape sequences without leading Escape
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_HOME, "[1~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_END, "[4~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_PAGE_UP, "[5~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_PAGE_DOWN, "[6~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F1, "OP");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F2, "OQ");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F3, "OR");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F4, "OS");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F5, "[15~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F6, "[17~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F7, "[18~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F8, "[19~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F9, "[20~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F10, "[21~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F11, "[23~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F12, "[24~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FORWARD_DEL, "[3~");
        ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_INSERT, "[2~");

        // Special ConnectBot hack: Ctrl-1 to Ctrl-0 for F1-F10.
        CTRL_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F1, KeyEvent.KEYCODE_1);
        CTRL_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F2, KeyEvent.KEYCODE_2);
        CTRL_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F3, KeyEvent.KEYCODE_3);
        CTRL_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F4, KeyEvent.KEYCODE_4);
        CTRL_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F5, KeyEvent.KEYCODE_5);
        CTRL_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F6, KeyEvent.KEYCODE_6);
        CTRL_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F7, KeyEvent.KEYCODE_7);
        CTRL_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F8, KeyEvent.KEYCODE_8);
        CTRL_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F9, KeyEvent.KEYCODE_9);
        CTRL_SEQUENCES.put(-LatinKeyboardView.KEYCODE_FKEY_F10, KeyEvent.KEYCODE_0);

        // Natively supported by ConnectBot
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_DPAD_UP, "OA");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_DPAD_DOWN, "OB");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_DPAD_LEFT, "OD");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_DPAD_RIGHT, "OC");

        // No VT equivalents?
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_DPAD_CENTER, "");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_SYSRQ, "");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_BREAK, "");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_NUM_LOCK, "");
        // ESC_SEQUENCES.put(-LatinKeyboardView.KEYCODE_SCROLL_LOCK, "");
    }
    InputConnection ic = getCurrentInputConnection();
    Integer ctrlseq = null;
    if (mConnectbotTabHack) {
        ctrlseq = CTRL_SEQUENCES.get(code);
    }
    String seq = ESC_SEQUENCES.get(code);

    if (ctrlseq != null) {
        if (mModAlt) {
            // send ESC prefix for "Alt"
            ic.commitText(Character.toString((char) 27), 1);
        }
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, ctrlseq));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, ctrlseq));
    } else if (seq != null) {
        if (mModAlt) {
            // send ESC prefix for "Alt"
            ic.commitText(Character.toString((char) 27), 1);
        }
        // send ESC prefix of escape sequence
        ic.commitText(Character.toString((char) 27), 1);
        ic.commitText(seq, 1);
    } else {
        // send key code, let connectbot handle it
        sendDownUpKeyEvents(code);
    }
    handleModifierKeysUp(false, false);
}

From source file:com.tandong.sa.sherlock.widget.SearchView.java

/**
 * React to the user typing while in the suggestions list. First, check for
 * action keys. If not handled, try refocusing regular characters into the
 * EditText.//from w  w  w. j av  a2s . c  o m
 */
private boolean onSuggestionsKey(View v, int keyCode, KeyEvent event) {
    // guard against possible race conditions (late arrival after dismiss)
    if (mSearchable == null) {
        return false;
    }
    if (mSuggestionsAdapter == null) {
        return false;
    }
    if (event.getAction() == KeyEvent.ACTION_DOWN && KeyEventCompat.hasNoModifiers(event)) {
        // First, check for enter or search (both of which we'll treat as a
        // "click")
        if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_SEARCH
                || keyCode == KeyEvent.KEYCODE_TAB) {
            int position = mQueryTextView.getListSelection();
            return onItemClicked(position, KeyEvent.KEYCODE_UNKNOWN, null);
        }

        // Next, check for left/right moves, which we use to "return" the
        // user to the edit view
        if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
            // give "focus" to text editor, with cursor at the beginning if
            // left key, at end if right key
            // TODO: Reverse left/right for right-to-left languages, e.g.
            // Arabic
            int selPoint = (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) ? 0 : mQueryTextView.length();
            mQueryTextView.setSelection(selPoint);
            mQueryTextView.setListSelection(0);
            mQueryTextView.clearListSelection();
            ensureImeVisible(mQueryTextView, true);

            return true;
        }

        // Next, check for an "up and out" move
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP && 0 == mQueryTextView.getListSelection()) {
            // TODO: restoreUserQuery();
            // let ACTV complete the move
            return false;
        }

        // Next, check for an "action key"
        // TODO SearchableInfo.ActionKeyInfo actionKey =
        // mSearchable.findActionKey(keyCode);
        // TODO if ((actionKey != null)
        // TODO && ((actionKey.getSuggestActionMsg() != null) || (actionKey
        // TODO .getSuggestActionMsgColumn() != null))) {
        // TODO // launch suggestion using action key column
        // TODO int position = mQueryTextView.getListSelection();
        // TODO if (position != ListView.INVALID_POSITION) {
        // TODO Cursor c = mSuggestionsAdapter.getCursor();
        // TODO if (c.moveToPosition(position)) {
        // TODO final String actionMsg = getActionKeyMessage(c, actionKey);
        // TODO if (actionMsg != null && (actionMsg.length() > 0)) {
        // TODO return onItemClicked(position, keyCode, actionMsg);
        // TODO }
        // TODO }
        // TODO }
        // TODO }
    }
    return false;
}

From source file:com.android.launcher2.Launcher.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_HOME:
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
                dumpState();//from w ww  .  ja  va2s . c om
                return true;
            }
            break;
        }
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_HOME:
            return true;
        }
    }

    return super.dispatchKeyEvent(event);
}

From source file:org.distantshoresmedia.keyboard.LatinIME.java

private void sendTab() {
    InputConnection ic = getCurrentInputConnection();
    boolean tabHack = isConnectbot() && mConnectbotTabHack;

    // FIXME: tab and ^I don't work in connectbot, hackish workaround
    if (tabHack) {
        if (mModAlt) {
            // send ESC prefix
            ic.commitText(Character.toString((char) 27), 1);
        }//from w ww.ja  v  a  2 s.co m
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_I));
        ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_I));
    } else {
        sendModifiedKeyDownUp(KeyEvent.KEYCODE_TAB);
    }
}

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./*  w  w w  .  ja  va2 s .co 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./*from   ww  w .j  av  a2  s.co  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.gxapplications.android.gxsuite.launcher.Launcher.java

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_HOME:
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (doesFileExist(DUMP_STATE_PROPERTY)) {
                dumpState();//from   ww  w .j a v  a 2s  .c  om
                return true;
            }
            break;
        }
    } else if (event.getAction() == KeyEvent.ACTION_UP) {
        switch (event.getKeyCode()) {
        case KeyEvent.KEYCODE_HOME:
            return true;
        }
    }

    return super.dispatchKeyEvent(event);
}

From source file:com.sonetel.ui.dialpad.DialerFragment.java

@Override
public boolean onKey(View arg0, int keyCode, KeyEvent arg2) {
    KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);

    return digits.onKeyDown(keyCode, event);
}