Example usage for android.view.inputmethod InputConnection sendKeyEvent

List of usage examples for android.view.inputmethod InputConnection sendKeyEvent

Introduction

In this page you can find the example usage for android.view.inputmethod InputConnection sendKeyEvent.

Prototype

boolean sendKeyEvent(KeyEvent event);

Source Link

Document

Send a key event to the process that is currently attached through this input connection.

Usage

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

private void sendKeyUp(InputConnection ic, int key, int meta) {
    long now = System.currentTimeMillis();
    if (ic != null)
        ic.sendKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, key, 0, meta));
}

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

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_DOWN:
    case KeyEvent.KEYCODE_DPAD_UP:
    case KeyEvent.KEYCODE_DPAD_LEFT:
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        // If tutorial is visible, don't allow dpad to work
        if (mTutorial != null) {
            return true;
        }/*from w w w.  j  a v  a  2 s .c  o  m*/
        TKKeyboardView inputView = mKeyboardSwitcher.getInputView();
        // Enable shift key and DPAD to do selections
        if (inputView != null && inputView.isShown() && inputView.getShiftState() == Keyboard.SHIFT_ON) {
            event = new KeyEvent(event.getDownTime(), event.getEventTime(), event.getAction(),
                    event.getKeyCode(), event.getRepeatCount(), event.getDeviceId(), event.getScanCode(),
                    KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON);
            InputConnection ic = getCurrentInputConnection();
            if (ic != null)
                ic.sendKeyEvent(event);
            return true;
        }
        break;
    case KeyEvent.KEYCODE_VOLUME_UP:
        if (!mVolUpAction.equals("none") && isKeyboardVisible()) {
            return doSwipeAction(mVolUpAction);
        }
        break;
    case KeyEvent.KEYCODE_VOLUME_DOWN:
        if (!mVolDownAction.equals("none") && isKeyboardVisible()) {
            return doSwipeAction(mVolDownAction);
        }
        break;
    }
    return super.onKeyUp(keyCode, 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 w w. j  a  va 2s  . com
        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:org.pocketworkstation.pckeyboard.LatinIME.java

private void sendSpecialKey(int code) {
    if (!isConnectbot()) {
        commitTyped(getCurrentInputConnection(), true);
        sendModifiedKeyDownUp(code);// w  w  w  .j  av  a2 s  .co 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:org.distantshoresmedia.keyboard.LatinIME.java

private void sendSpecialKey(int code) {
    if (!isConnectbot()) {
        commitTyped(getCurrentInputConnection(), true);
        sendModifiedKeyDownUp(code);/*from  ww w .j a  v  a 2s  .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);
}