Example usage for android.view.inputmethod InputConnection performEditorAction

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

Introduction

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

Prototype

boolean performEditorAction(int editorAction);

Source Link

Document

Have the editor perform an action it has said it can do.

Usage

From source file:Main.java

public static void performEditorActionNext(InputConnection ic) {
    ic.performEditorAction(EditorInfo.IME_ACTION_NEXT);
}

From source file:com.googlecode.eyesfree.brailleback.BrailleIME.java

public boolean sendDefaultAction() {
    if (!allowsDefaultAction()) {
        return false;
    }/*from   w w w .  j a va  2s. co m*/
    EditorInfo ei = getCurrentInputEditorInfo();
    InputConnection ic = getCurrentInputConnection();
    if (ei == null || ic == null) {
        return false;
    }

    int actionId = ei.actionId;
    if (actionId != 0) {
        return ic.performEditorAction(actionId);
    } else {
        return sendDefaultEditorAction(false);
    }
}

From source file:net.zhdev.ctrlvkeyboard.CtrlVKeyboard.java

/**
 * Inserts text in the current focused text editor if possible and if it is a valid one.
 *
 * @param text          the text to be inserted
 * @param clearBefore   whether the text editor should be cleared before inserting the text
 * @param endWithAction whether the IME action should be performed at the end
 * @return {@code true} if the text was inserted and the actions performed, {@code false}
 * otherwise/*from w w w . j  a  v  a2 s .c  o m*/
 */
private boolean type(String text, boolean clearBefore, boolean endWithAction) {
    boolean result = false;

    if (text == null) {
        text = "";
    }

    EditorInfo info = getCurrentInputEditorInfo();

    if (info != null) {
        // If the view accepts text as input
        if ((info.inputType & EditorInfo.TYPE_MASK_CLASS) != EditorInfo.TYPE_NULL) {
            InputConnection ic = getCurrentInputConnection();

            if (ic != null) {
                boolean enterShouldHaveAction = true;
                ic.beginBatchEdit();
                if (endWithAction) {
                    enterShouldHaveAction = (info.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0;
                    if (!enterShouldHaveAction) {
                        text += "\n";
                    }
                }
                if (clearBefore) {
                    ic.deleteSurroundingText(Integer.MAX_VALUE, Integer.MAX_VALUE);
                }
                ic.commitText(text, 1);
                result = ic.endBatchEdit();

                if (result && endWithAction && enterShouldHaveAction) {
                    result = ic.performEditorAction(info.imeOptions & EditorInfo.IME_MASK_ACTION);
                }
            }
        }
    }
    return result;
}

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

private void onNonFunctionKey(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.ENTER:
        if (mShiftKeyState.isPressed() && ic != null) {
            //power-users feature ahead: Shift+Enter
            //getting away from firing the default editor action, by forcing newline
            ic.commitText("\n", 1);
            break;
        }//from   w w  w.  j a v  a 2s .com
        final EditorInfo editorInfo = getCurrentInputEditorInfo();
        final int imeOptionsActionId = IMEUtil.getImeOptionsActionIdFromEditorInfo(editorInfo);
        if (ic != null && IMEUtil.IME_ACTION_CUSTOM_LABEL == imeOptionsActionId) {
            // Either we have an actionLabel and we should performEditorAction with
            // actionId regardless of its value.
            ic.performEditorAction(editorInfo.actionId);
        } else if (ic != null && EditorInfo.IME_ACTION_NONE != imeOptionsActionId) {
            // We didn't have an actionLabel, but we had another action to execute.
            // EditorInfo.IME_ACTION_NONE explicitly means no action. In contrast,
            // EditorInfo.IME_ACTION_UNSPECIFIED is the default value for an action, so it
            // means there should be an action and the app didn't bother to set a specific
            // code for it - presumably it only handles one. It does not have to be treated
            // in any specific way: anything that is not IME_ACTION_NONE should be sent to
            // performEditorAction.
            ic.performEditorAction(imeOptionsActionId);
        } else {
            handleSeparator(primaryCode);
        }
        break;
    case KeyCodes.SPACE:
        //shortcut. Nothing more.
        handleSeparator(primaryCode);
        //should we switch to alphabet keyboard?
        if (!isInAlphabetKeyboardMode()) {
            Logger.d(TAG, "SPACE/ENTER while in symbols mode");
            if (mAskPrefs.getSwitchKeyboardOnSpace()) {
                Logger.d(TAG, "Switching to Alphabet is required by the user");
                getKeyboardSwitcher().nextKeyboard(getCurrentInputEditorInfo(),
                        KeyboardSwitcher.NextKeyboardType.Alphabet);
            }
        }
        break;
    case KeyCodes.TAB:
        sendTab();
        break;
    case KeyCodes.ESCAPE:
        sendEscape();
        break;
    default:
        if (isWordSeparator(primaryCode)) {
            handleSeparator(primaryCode);
        } else {
            if (mControlKeyState.isActive() && primaryCode >= 32 && primaryCode < 127) {
                // http://en.wikipedia.org/wiki/Control_character#How_control_characters_map_to_keyboards
                int controlCode = primaryCode & 31;
                Logger.d(TAG, "CONTROL state: Char was %d and now it is %d", primaryCode, controlCode);
                if (controlCode == 9) {
                    sendTab();
                } else {
                    ic.commitText(Character.toString((char) controlCode), 1);
                }
            } else {
                handleCharacter(primaryCode, key, multiTapIndex, nearByKeyCodes);
            }
            mJustAddedAutoSpace = false;
        }
        break;
    }
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

private void onNonFunctionKey(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.ENTER:
        if (mShiftKeyState.isPressed() && ic != null) {
            //power-users feature ahead: Shift+Enter
            //getting away from firing the default editor action, by forcing newline
            ic.commitText("\n", 1);
            break;
        }/*w  ww  . ja  v a2 s  .c o  m*/
        final EditorInfo editorInfo = getCurrentInputEditorInfo();
        final int imeOptionsActionId = IMEUtil.getImeOptionsActionIdFromEditorInfo(editorInfo);
        if (ic != null && IMEUtil.IME_ACTION_CUSTOM_LABEL == imeOptionsActionId) {
            // Either we have an actionLabel and we should performEditorAction with
            // actionId regardless of its value.
            ic.performEditorAction(editorInfo.actionId);
        } else if (ic != null && EditorInfo.IME_ACTION_NONE != imeOptionsActionId) {
            // We didn't have an actionLabel, but we had another action to execute.
            // EditorInfo.IME_ACTION_NONE explicitly means no action. In contrast,
            // EditorInfo.IME_ACTION_UNSPECIFIED is the default value for an action, so it
            // means there should be an action and the app didn't bother to set a specific
            // code for it - presumably it only handles one. It does not have to be treated
            // in any specific way: anything that is not IME_ACTION_NONE should be sent to
            // performEditorAction.
            ic.performEditorAction(imeOptionsActionId);
        } else {
            handleSeparator(primaryCode);
        }
        break;
    case KeyCodes.SPACE:
        //shortcut. Nothing more.
        handleSeparator(primaryCode);
        //should we switch to alphabet keyboard?
        if (!isInAlphabetKeyboardMode()) {
            Logger.d(TAG, "SPACE/ENTER while in symbols mode");
            if (mAskPrefs.getSwitchKeyboardOnSpace()) {
                Logger.d(TAG, "Switching to Alphabet is required by the user");
                getKeyboardSwitcher().nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Alphabet);
            }
        }
        break;
    case KeyCodes.TAB:
        sendTab();
        break;
    case KeyCodes.ESCAPE:
        sendEscape();
        break;
    default:
        if (isWordSeparator(primaryCode)) {
            handleSeparator(primaryCode);
        } else {
            if (mControlKeyState.isActive() && primaryCode >= 32 && primaryCode < 127) {
                // http://en.wikipedia.org/wiki/Control_character#How_control_characters_map_to_keyboards
                int controlCode = primaryCode & 31;
                Logger.d(TAG, "CONTROL state: Char was %d and now it is %d", primaryCode, controlCode);
                if (controlCode == 9) {
                    sendTab();
                } else {
                    ic.commitText(Character.toString((char) controlCode), 1);
                }
            } else {
                handleCharacter(primaryCode, key, multiTapIndex, nearByKeyCodes);
            }
            mJustAddedAutoSpace = false;
        }
        break;
    }
}

From source file:org.kde.kdeconnect.Plugins.RemoteKeyboardPlugin.RemoteKeyboardPlugin.java

private boolean handleSpecialKey(int key, boolean shift, boolean ctrl, boolean alt) {
    int keyEvent = specialKeyMap.get(key, 0);
    if (keyEvent == 0)
        return false;
    InputConnection inputConn = RemoteKeyboardService.instance.getCurrentInputConnection();
    //        Log.d("RemoteKeyboardPlugin", "Handling special key " + key + " translated to " + keyEvent + " shift=" + shift + " ctrl=" + ctrl + " alt=" + alt);

    // special sequences:
    if (ctrl && (keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT)) {
        // Ctrl + right -> next word
        ExtractedText extractedText = inputConn.getExtractedText(new ExtractedTextRequest(), 0);
        int pos = getCharPos(extractedText, ' ', keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT);
        if (pos == -1)
            pos = currentTextLength(extractedText);
        else//from  www . j av a2 s.  co m
            pos++;
        int startPos = pos;
        int endPos = pos;
        if (shift) { // Shift -> select word (otherwise jump)
            Pair<Integer, Integer> sel = currentSelection(extractedText);
            int cursor = currentCursorPos(extractedText);
            //                Log.d("RemoteKeyboardPlugin", "Selection (to right): " + sel.first + " / " + sel.second + " cursor: " + cursor);
            startPos = cursor;
            if (sel.first < cursor || // active selection from left to right -> grow
                    sel.first > sel.second) // active selection from right to left -> shrink
                startPos = sel.first;
        }
        inputConn.setSelection(startPos, endPos);
    } else if (ctrl && keyEvent == KeyEvent.KEYCODE_DPAD_LEFT) {
        // Ctrl + left -> previous word
        ExtractedText extractedText = inputConn.getExtractedText(new ExtractedTextRequest(), 0);
        int pos = getCharPos(extractedText, ' ', keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT);
        if (pos == -1)
            pos = 0;
        else
            pos++;
        int startPos = pos;
        int endPos = pos;
        if (shift) {
            Pair<Integer, Integer> sel = currentSelection(extractedText);
            int cursor = currentCursorPos(extractedText);
            //                Log.d("RemoteKeyboardPlugin", "Selection (to left): " + sel.first + " / " + sel.second + " cursor: " + cursor);
            startPos = cursor;
            if (cursor < sel.first || // active selection from right to left -> grow
                    sel.first < sel.second) // active selection from right to left -> shrink
                startPos = sel.first;
        }
        inputConn.setSelection(startPos, endPos);
    } else if (shift && (keyEvent == KeyEvent.KEYCODE_DPAD_LEFT || keyEvent == KeyEvent.KEYCODE_DPAD_RIGHT
            || keyEvent == KeyEvent.KEYCODE_DPAD_UP || keyEvent == KeyEvent.KEYCODE_DPAD_DOWN
            || keyEvent == KeyEvent.KEYCODE_MOVE_HOME || keyEvent == KeyEvent.KEYCODE_MOVE_END)) {
        // Shift + up/down/left/right/home/end
        long now = SystemClock.uptimeMillis();
        inputConn.sendKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0));
        inputConn.sendKeyEvent(
                new KeyEvent(now, now, KeyEvent.ACTION_DOWN, keyEvent, 0, KeyEvent.META_SHIFT_LEFT_ON));
        inputConn.sendKeyEvent(
                new KeyEvent(now, now, KeyEvent.ACTION_UP, keyEvent, 0, KeyEvent.META_SHIFT_LEFT_ON));
        inputConn.sendKeyEvent(new KeyEvent(now, now, KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SHIFT_LEFT, 0, 0));
    } else if (keyEvent == KeyEvent.KEYCODE_NUMPAD_ENTER || keyEvent == KeyEvent.KEYCODE_ENTER) {
        // Enter key
        EditorInfo editorInfo = RemoteKeyboardService.instance.getCurrentInputEditorInfo();
        //            Log.d("RemoteKeyboardPlugin", "Enter: " + editorInfo.imeOptions);
        if (editorInfo != null
                && (((editorInfo.imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) || ctrl)) { // Ctrl+Return overrides IME_FLAG_NO_ENTER_ACTION (FIXME: make configurable?)
            // check for special DONE/GO/etc actions first:
            int[] actions = { EditorInfo.IME_ACTION_GO, EditorInfo.IME_ACTION_NEXT, EditorInfo.IME_ACTION_SEND,
                    EditorInfo.IME_ACTION_SEARCH, EditorInfo.IME_ACTION_DONE }; // note: DONE should be last or we might hide the ime instead of "go"
            for (int i = 0; i < actions.length; i++) {
                if ((editorInfo.imeOptions & actions[i]) == actions[i]) {
                    //                        Log.d("RemoteKeyboardPlugin", "Enter-action: " + actions[i]);
                    inputConn.performEditorAction(actions[i]);
                    return true;
                }
            }
        } else {
            // else: fall back to regular Enter-event:
            //                Log.d("RemoteKeyboardPlugin", "Enter: normal keypress");
            inputConn.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyEvent));
            inputConn.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyEvent));
        }
    } else {
        // default handling:
        inputConn.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyEvent));
        inputConn.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, keyEvent));
    }

    return true;
}

From source file:keyboard.ecloga.com.eclogakeyboard.EclogaKeyboard.java

@Override
public void onKey(int primaryCode, int[] keyCodes) {
    final InputConnection ic = getCurrentInputConnection();

    playClick();//www.  ja  va2  s .  c o  m
    printChar = true;

    if (primaryCode != 126 && primaryCode != -5 && primaryCode != -1
            && primaryCode != EmojiKeyboardView.KEYCODE_EMOJI_1
            && primaryCode != EmojiKeyboardView.KEYCODE_EMOJI_2
            && primaryCode != EmojiKeyboardView.KEYCODE_EMOJI_3
            && primaryCode != EmojiKeyboardView.KEYCODE_EMOJI_4
            && primaryCode != EmojiKeyboardView.KEYCODE_EMOJI_5) {

        keyPressCounter++;

        if (keyPressCounter > 100 && keyPressCounter <= 1000 && keypresscounter1.equals("false")) {
            NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify = new Notification(R.drawable.notify, "Ecloga Keyboard",
                    System.currentTimeMillis());
            PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this, Home.class), 0);

            notify.setLatestEventInfo(getApplicationContext(), "Warming up!", "Type more than 100 characters",
                    pending);
            notif.notify(0, notify);

            Preferences.setDefaults("keypresscounter1", "true", getApplicationContext());
        } else if (keyPressCounter > 10000 && keyPressCounter <= 10000 && keypresscounter2.equals("false")) {
            NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify = new Notification(R.drawable.notify, "Ecloga Keyboard",
                    System.currentTimeMillis());
            PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this, Home.class), 0);

            notify.setLatestEventInfo(getApplicationContext(), "Keep it up!", "Type more than 1000 characters",
                    pending);
            notif.notify(0, notify);

            Preferences.setDefaults("keypresscounter2", "true", getApplicationContext());
        } else if (keyPressCounter > 10000 && keypresscounter3.equals("false")) {
            NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notify = new Notification(R.drawable.notify, "Ecloga Keyboard",
                    System.currentTimeMillis());
            PendingIntent pending = PendingIntent.getActivity(this, 0, new Intent(this, Home.class), 0);

            notify.setLatestEventInfo(getApplicationContext(), "Typing master!",
                    "Type more than 10000 characters", pending);
            notif.notify(0, notify);

            Preferences.setDefaults("keypresscounter3", "true", getApplicationContext());
        }

        Preferences.setDefaults("keypresses", String.valueOf(keyPressCounter), getApplicationContext());
    }

    switch (primaryCode) {
    case 58:
        ic.commitText(":", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }
        break;
    case 59:
        ic.commitText(";", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }
        break;
    case 33:
        ic.commitText("!", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }

        if (autoCapitalize) {
            caps = true;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            printChar = false;
        }
        break;
    case 63:
        ic.commitText("?", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }

        if (autoCapitalize) {
            caps = true;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            printChar = false;
        }
        break;
    case -5:
        if (lang.equals("seclang")) {
            if (orient.equals("portrait")) {
                keyboard = prilang;
            } else if (orient.equals("landscape")) {
                keyboard = prilang_landscape;
            }
            kv.setKeyboard(keyboard);
            kv.invalidateAllKeys();
            lang = "prilang";
        } else if (lang.equals("prilang")) {
            if (orient.equals("portrait")) {
                keyboard = seclang;
            } else if (orient.equals("landscape")) {
                keyboard = seclang_landscape;
            }
            kv.setKeyboard(keyboard);
            kv.invalidateAllKeys();
            lang = "seclang";
        }
        break;
    case 32:
        ic.commitText(" ", 1);

        if (autoCapitalize) {
            if (String.valueOf(ic.getTextBeforeCursor(2, 0)).equals(". ")) {
                caps = true;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                printChar = false;
            }
        }
        break;
    case 126:
        ic.deleteSurroundingText(1, 0);

        if (autoCapitalize) {
            if (String.valueOf(ic.getTextBeforeCursor(1, 0)).equals(".")
                    || String.valueOf(ic.getTextBeforeCursor(2, 0)).equals(". ")
                    || String.valueOf(ic.getTextBeforeCursor(2, 0)).equals("")) {
                caps = true;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                printChar = false;
            }
        }
        break;
    case 44:
        ic.commitText(",", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }
        break;
    case 46:
        ic.commitText(".", 1);

        if (autoSpacing) {
            ic.commitText(" ", 1);
        }

        if (autoCapitalize) {
            caps = true;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            printChar = false;
        }
        break;
    case -1:
        ic.performEditorAction(EditorInfo.IME_ACTION_GO);
        break;
    case EmojiKeyboardView.KEYCODE_EMOJI_1:
        keyboard = new Keyboard(this, R.xml.emoji_a1);
        kv.setKeyboard(keyboard);
        kv.invalidateAllKeys();
        emoji = 1;
        eScreen = 1;
        break;
    case EmojiKeyboardView.KEYCODE_EMOJI_2:
        keyboard = new Keyboard(this, R.xml.emoji_b1);
        kv.setKeyboard(keyboard);
        kv.invalidateAllKeys();
        emoji = 2;
        eScreen = 1;
        break;
    case EmojiKeyboardView.KEYCODE_EMOJI_3:
        keyboard = new Keyboard(this, R.xml.emoji_c1);
        kv.setKeyboard(keyboard);
        kv.invalidateAllKeys();
        emoji = 3;
        eScreen = 1;
        break;
    case EmojiKeyboardView.KEYCODE_EMOJI_4:
        keyboard = new Keyboard(this, R.xml.emoji_d1);
        kv.setKeyboard(keyboard);
        kv.invalidateAllKeys();
        emoji = 4;
        eScreen = 1;
        break;
    case EmojiKeyboardView.KEYCODE_EMOJI_5:
        keyboard = new Keyboard(this, R.xml.emoji_e1);
        kv.setKeyboard(keyboard);
        kv.invalidateAllKeys();
        emoji = 5;
        eScreen = 1;
        break;
    default:
        char code = (char) primaryCode;

        if (allCaps) {
            if (Character.isLetter(code) && capsLock) {
                code = Character.toLowerCase(code);
            } else if (Character.isLetter(code) && !capsLock) {
                code = Character.toUpperCase(code);
            }
        } else {
            if (Character.isLetter(code) && caps) {
                code = Character.toUpperCase(code);
            }
        }

        String character = String.valueOf(code);
        ic.commitText(character, 1);
    }

    if (printedDifferent) {
        ic.deleteSurroundingText(1, 0);
    }

    if (printedDot) {
        ic.deleteSurroundingText(1, 0);

        if (autoCapitalize) {
            caps = true;
            keyboard.setShifted(caps);
            kv.invalidateAllKeys();
            printChar = false;
        }
    }

    if (doubleUp == 2) {
        capsChange();
    } else {
        changeCaps();
    }
}