Example usage for android.view.inputmethod InputConnection commitText

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

Introduction

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

Prototype

boolean commitText(CharSequence text, int newCursorPosition);

Source Link

Document

Commit text to the text box and set the new cursor position.

Usage

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

public void onText(Key key, CharSequence text) {
    Logger.d(TAG, "onText: '%s'", text);
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;/* w  ww. j a va2 s  .c  om*/
    ic.beginBatchEdit();

    abortCorrectionAndResetPredictionState(false);
    ic.commitText(text, 1);

    mJustAddedAutoSpace = false;
    mCommittedWord = text;
    mUndoCommitCursorPosition = UNDO_COMMIT_WAITING_TO_RECORD_POSITION;

    TextEntryState.acceptedDefault(text);
    ic.endBatchEdit();

    setSuggestions(mSuggest.getNextSuggestions(mCommittedWord, false), false, false, false);
}

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 ww  .  jav a  2  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(),
                        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:keyboard.ecloga.com.eclogakeyboard.EclogaKeyboard.java

@Override
public void onPress(final int primaryCode) {
    setVibration();//from   w w w .ja va 2  s. c  o m

    final InputConnection ic = getCurrentInputConnection();

    if (popupKeypress && (primaryCode == 32 || 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)) {

        kv.setPreviewEnabled(false);
    }

    if (oppositeCase) {
        timer = new CountDownTimer(300, 1) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                if (!swipe) {
                    if (primaryCode == 46) {
                        ic.commitText(",", 1);

                        printedCommaa = true;

                        if (autoSpacing) {
                            ic.commitText(" ", 1);
                        }
                    } else if (primaryCode == 32) {
                        Intent intent = new Intent(getApplicationContext(), Home.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } else if (primaryCode == -5) {
                        if (voiceInput) {
                            if (mVoiceRecognitionTrigger.isInstalled()) {
                                mVoiceRecognitionTrigger.startVoiceRecognition();
                            }
                        }
                    } else {
                        char code = (char) primaryCode;

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

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

                    printedDifferent = true;
                }
            }
        }.start();
    }

    if (spaceDot) {
        if (primaryCode == 32) {
            doubleSpace++;

            if (doubleSpace == 2) {
                ic.deleteSurroundingText(1, 0);
                ic.commitText(".", 1);

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

                printedDot = true;
                doubleSpace = 0;
            } else {
                printedDot = false;
            }
        } else {
            printedDot = false;
            doubleSpace = 0;
        }
    }
}

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;
        }/*from ww w.  j av a 2s  . 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:com.anysoftkeyboard.AnySoftKeyboard.java

/**
 * Commits the chosen word to the text field and saves it for later
 * retrieval.//w w  w .  j a  va2  s.  c  om
 *
 * @param wordToCommit the suggestion picked by the user to be committed to the text
 *                     field
 * @param correcting   this is a correction commit
 */
protected void commitWordToInput(@NonNull CharSequence wordToCommit, boolean correcting) {
    mWord.setPreferredWord(wordToCommit);
    InputConnection ic = getCurrentInputConnection();
    if (ic != null) {
        if (correcting) {
            AnyApplication.getDeviceSpecific().commitCorrectionToInputConnection(ic,
                    mGlobalCursorPosition - mWord.getTypedWord().length(), mWord.getTypedWord(),
                    mWord.getPreferredWord());
            // and drawing pop-out text
            getInputView().popTextOutOfKey(mWord.getPreferredWord());
        } else {
            ic.commitText(wordToCommit, 1);
        }
    }
    mCommittedWord = wordToCommit;
    mUndoCommitCursorPosition = UNDO_COMMIT_WAITING_TO_RECORD_POSITION;

    clearSuggestions();
}

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

/**
 * Commits the chosen word to the text field and saves it for later
 * retrieval.//from   w ww.j  av a2  s  .  c o  m
 *
 * @param suggestion
 *            the suggestion picked by the user to be committed to the text
 *            field
 * @param correcting
 *            whether this is due to a correction of an existing word.
 */
private void pickSuggestion(CharSequence suggestion, boolean correcting) {
    LatinKeyboardView inputView = mKeyboardSwitcher.getInputView();
    int shiftState = getShiftState();
    if (shiftState == Keyboard.SHIFT_LOCKED || shiftState == Keyboard.SHIFT_CAPS_LOCKED) {
        suggestion = suggestion.toString().toUpperCase(); // all UPPERCASE
    }
    InputConnection ic = getCurrentInputConnection();
    if (ic != null) {
        rememberReplacedWord(suggestion);
        ic.commitText(suggestion, 1);
    }
    saveWordInHistory(suggestion);
    mPredicting = false;
    mCommittedLength = suggestion.length();
    ((LatinKeyboard) inputView.getKeyboard()).setPreferredLetters(null);
    // If we just corrected a word, then don't show punctuations
    if (!correcting) {
        setNextSuggestions();
    }
    updateShiftKeyState(getCurrentInputEditorInfo());
}

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

/**
 * Commits the chosen word to the text field and saves it for later
 * retrieval./*  w  ww  . ja  v  a2 s .com*/
 *
 * @param suggestion
 *            the suggestion picked by the user to be committed to the text
 *            field
 * @param correcting
 *            whether this is due to a correction of an existing word.
 */
private void pickSuggestion(CharSequence suggestion, boolean correcting) {
    TKKeyboardView inputView = mKeyboardSwitcher.getInputView();
    int shiftState = getShiftState();
    if (shiftState == Keyboard.SHIFT_LOCKED || shiftState == Keyboard.SHIFT_CAPS_LOCKED) {
        suggestion = suggestion.toString().toUpperCase(); // all UPPERCASE
    }
    InputConnection ic = getCurrentInputConnection();
    if (ic != null) {
        rememberReplacedWord(suggestion);
        ic.commitText(suggestion, 1);
    }
    saveWordInHistory(suggestion);
    mPredicting = false;
    mCommittedLength = suggestion.length();
    ((TKKeyboard) inputView.getKeyboard()).setPreferredLetters(null);
    // If we just corrected a word, then don't show punctuations
    if (!correcting) {
        setNextSuggestions();
    }
    updateShiftKeyState(getCurrentInputEditorInfo());
}

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

public void sendModifiableKeyChar(char ch) {
    // Support modified key events
    boolean modShift = isShiftMod();
    if ((modShift || mModCtrl || mModAlt || mModMeta) && ch > 0 && ch < 127) {
        InputConnection ic = getCurrentInputConnection();
        if (isConnectbot()) {
            if (mModAlt) {
                // send ESC prefix
                ic.commitText(Character.toString((char) 27), 1);
            }/*from  w w w . ja v  a  2  s  .  c om*/
            if (mModCtrl) {
                int code = ch & 31;
                if (code == 9) {
                    sendTab();
                } else {
                    ic.commitText(Character.toString((char) code), 1);
                }
            } else {
                ic.commitText(Character.toString(ch), 1);
            }
            handleModifierKeysUp(false, false);
            return;
        }

        // Non-ConnectBot

        // Restrict Shift modifier to ENTER and SPACE, supporting Shift-Enter etc.
        // Note that most special keys such as DEL or cursor keys aren't handled
        // by this charcode-based method.

        int combinedCode = asciiToKeyCode[ch];
        if (combinedCode > 0) {
            int code = combinedCode & KF_MASK;
            boolean shiftable = (combinedCode & KF_SHIFTABLE) > 0;
            boolean upper = (combinedCode & KF_UPPER) > 0;
            boolean letter = (combinedCode & KF_LETTER) > 0;
            boolean shifted = modShift && (upper || shiftable);
            if (letter && !mModCtrl && !mModAlt && !mModMeta) {
                // Try workaround for issue 179 where letters don't get upcased
                ic.commitText(Character.toString(ch), 1);
                handleModifierKeysUp(false, false);
            } else if ((ch == 'a' || ch == 'A') && mModCtrl) {
                // Special case for Ctrl-A to work around accidental select-all-then-replace.
                if (sKeyboardSettings.ctrlAOverride == 0) {
                    // Ignore Ctrl-A, treat Ctrl-Alt-A as Ctrl-A.
                    if (mModAlt) {
                        boolean isChordingAlt = mAltKeyState.isChording();
                        setModAlt(false);
                        sendModifiedKeyDownUp(code, shifted);
                        if (isChordingAlt)
                            setModAlt(true);
                    } else {
                        Toast.makeText(getApplicationContext(),
                                getResources().getString(R.string.toast_ctrl_a_override_info),
                                Toast.LENGTH_LONG).show();
                        // Clear the Ctrl modifier (and others)
                        sendModifierKeysDown(shifted);
                        sendModifierKeysUp(shifted);
                        return; // ignore the key
                    }

                } else if (sKeyboardSettings.ctrlAOverride == 1) {
                    // Clear the Ctrl modifier (and others)
                    sendModifierKeysDown(shifted);
                    sendModifierKeysUp(shifted);
                    return; // ignore the key
                } else {
                    // Standard Ctrl-A behavior.
                    sendModifiedKeyDownUp(code, shifted);
                }
            } else {
                sendModifiedKeyDownUp(code, shifted);
            }
            return;
        }
    }

    if (ch >= '0' && ch <= '9') {
        //WIP
        InputConnection ic = getCurrentInputConnection();
        ic.clearMetaKeyStates(KeyEvent.META_SHIFT_ON | KeyEvent.META_ALT_ON | KeyEvent.META_SYM_ON);
        //EditorInfo ei = getCurrentInputEditorInfo();
        //Log.i(TAG, "capsmode=" + ic.getCursorCapsMode(ei.inputType));
        //sendModifiedKeyDownUp(KeyEvent.KEYCODE_0 + ch - '0');
        //return;
    }

    // Default handling for anything else, including unmodified ENTER and SPACE.
    sendKeyChar(ch);
}

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

public void sendModifiableKeyChar(char ch) {
    // Support modified key events
    boolean modShift = isShiftMod();
    if ((modShift || mModCtrl || mModAlt || mModMeta) && ch > 0 && ch < 127) {
        InputConnection ic = getCurrentInputConnection();
        if (isConnectbot()) {
            if (mModAlt) {
                // send ESC prefix
                ic.commitText(Character.toString((char) 27), 1);
            }/*from   w w  w  .ja  va  2s .  co  m*/
            if (mModCtrl) {
                int code = ch & 31;
                if (code == 9) {
                    sendTab();
                } else {
                    ic.commitText(Character.toString((char) code), 1);
                }
            } else {
                ic.commitText(Character.toString(ch), 1);
            }
            handleModifierKeysUp(false, false);
            return;
        }

        // Non-ConnectBot

        // Restrict Shift modifier to ENTER and SPACE, supporting Shift-Enter etc.
        // Note that most special keys such as DEL or cursor keys aren't handled
        // by this charcode-based method.

        int combinedCode = asciiToKeyCode[ch];
        if (combinedCode > 0) {
            int code = combinedCode & KF_MASK;
            boolean shiftable = (combinedCode & KF_SHIFTABLE) > 0;
            boolean upper = (combinedCode & KF_UPPER) > 0;
            boolean letter = (combinedCode & KF_LETTER) > 0;
            boolean shifted = modShift && (upper || shiftable);
            if (letter && !mModCtrl && !mModAlt && !mModMeta) {
                // Try workaround for issue 179 where letters don't get upcased
                ic.commitText(Character.toString(ch), 1);
                handleModifierKeysUp(false, false);
            } else {
                sendModifiedKeyDownUp(code, shifted);
            }
            return;
        }
    }

    if (ch >= '0' && ch <= '9') {
        //WIP
        InputConnection ic = getCurrentInputConnection();
        ic.clearMetaKeyStates(KeyEvent.META_SHIFT_ON | KeyEvent.META_ALT_ON | KeyEvent.META_SYM_ON);
        //EditorInfo ei = getCurrentInputEditorInfo();
        //Log.i(TAG, "capsmode=" + ic.getCursorCapsMode(ei.inputType));
        //sendModifiedKeyDownUp(KeyEvent.KEYCODE_0 + ch - '0');
        //return;
    }

    // Default handling for anything else, including unmodified ENTER and SPACE.
    sendKeyChar(ch);
}

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

private void commitTyped(InputConnection inputConnection, boolean manual) {
    if (mPredicting) {
        mPredicting = false;//from  ww w.  ja  va  2 s  .c  o  m
        if (mComposing.length() > 0) {
            if (inputConnection != null) {
                inputConnection.commitText(mComposing, 1);
            }
            mCommittedLength = mComposing.length();
            if (manual) {
                TextEntryState.manualTyped(mComposing);
            } else {
                TextEntryState.acceptedTyped(mComposing);
            }
            addToDictionaries(mComposing, AutoDictionary.FREQUENCY_FOR_TYPED);
        }
        updateSuggestions();
    }
}