Example usage for android.view.inputmethod InputConnection deleteSurroundingText

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

Introduction

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

Prototype

boolean deleteSurroundingText(int beforeLength, int afterLength);

Source Link

Document

Delete beforeLength characters of text before the current cursor position, and delete afterLength characters of text after the current cursor position, excluding the selection.

Usage

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

private void handleDeleteLastCharacter(boolean forMultiTap) {
    InputConnection ic = getCurrentInputConnection();

    boolean deleteChar = false;
    if (TextEntryState.isPredicting()) {
        final boolean wordManipulation = mWord.length() > 0 && mWord.cursorPosition() > 0;
        if (wordManipulation) {
            mWord.deleteLast();//w  w w .  ja va 2s.  c  o m
            final int cursorPosition;
            if (mWord.cursorPosition() != mWord.length())
                cursorPosition = getCursorPosition(ic);
            else
                cursorPosition = -1;

            if (cursorPosition >= 0)
                ic.beginBatchEdit();

            ic.setComposingText(mWord.getTypedWord(), 1);
            if (mWord.length() == 0) {
                TextEntryState.newSession(mPredictionOn);
            } else if (cursorPosition >= 0) {
                ic.setSelection(cursorPosition - 1, cursorPosition - 1);
            }

            if (cursorPosition >= 0)
                ic.endBatchEdit();

            postUpdateSuggestions();
        } else {
            ic.deleteSurroundingText(1, 0);
        }
    } else {
        deleteChar = true;
    }

    TextEntryState.backspace();
    if (TextEntryState.getState() == TextEntryState.State.UNDO_COMMIT) {
        revertLastWord();
    } else if (deleteChar) {
        //just making sure that
        if (mCandidateView != null)
            mCandidateView.dismissAddToDictionaryHint();

        if (!forMultiTap) {
            sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
        } else {
            // this code tries to delete the text in a different way,
            // because of multi-tap stuff
            // using "deleteSurroundingText" will actually get the input
            // updated faster!
            // but will not handle "delete all selected text" feature,
            // hence the "if (!forMultiTap)" above
            final CharSequence beforeText = ic == null ? null : ic.getTextBeforeCursor(1, 0);
            final int textLengthBeforeDelete = (TextUtils.isEmpty(beforeText)) ? 0 : beforeText.length();
            if (textLengthBeforeDelete > 0)
                ic.deleteSurroundingText(1, 0);
            else
                sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
        }
    }
}

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

private void swapPunctuationAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;// w w  w  . jav a  2s.co  m
    CharSequence lastTwo = ic.getTextBeforeCursor(2, 0);
    if (lastTwo != null && lastTwo.length() == 2 && lastTwo.charAt(0) == ASCII_SPACE
            && isSentenceSeparator(lastTwo.charAt(1))) {
        ic.beginBatchEdit();
        ic.deleteSurroundingText(2, 0);
        ic.commitText(lastTwo.charAt(1) + " ", 1);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo());
        mJustAddedAutoSpace = true;
    }
}

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

private void reswapPeriodAndSpace() {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;//from  w w  w  .j  av a  2  s.co  m
    CharSequence lastThree = ic.getTextBeforeCursor(3, 0);
    if (lastThree != null && lastThree.length() == 3 && lastThree.charAt(0) == ASCII_PERIOD
            && lastThree.charAt(1) == ASCII_SPACE && lastThree.charAt(2) == ASCII_PERIOD) {
        ic.beginBatchEdit();
        ic.deleteSurroundingText(3, 0);
        ic.commitText(" ..", 1);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo());
    }
}

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

private void doubleSpace() {
    // if (!mAutoPunctuate) return;
    if (mCorrectionMode == Suggest.CORRECTION_NONE)
        return;/* w  ww. ja  v  a2 s  .  c o m*/
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;
    CharSequence lastThree = ic.getTextBeforeCursor(3, 0);
    if (lastThree != null && lastThree.length() == 3 && Character.isLetterOrDigit(lastThree.charAt(0))
            && lastThree.charAt(1) == ASCII_SPACE && lastThree.charAt(2) == ASCII_SPACE) {
        ic.beginBatchEdit();
        ic.deleteSurroundingText(2, 0);
        ic.commitText(". ", 1);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo());
        mJustAddedAutoSpace = true;
    }
}

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

private void maybeRemovePreviousPeriod(CharSequence text) {
    final InputConnection ic = getCurrentInputConnection();
    if (ic == null || text.length() == 0)
        return;/*w ww.j a v  a2  s  . co m*/

    // When the text's first character is '.', remove the previous period
    // if there is one.
    CharSequence lastOne = ic.getTextBeforeCursor(1, 0);
    if (lastOne != null && lastOne.length() == 1 && lastOne.charAt(0) == ASCII_PERIOD
            && text.charAt(0) == ASCII_PERIOD) {
        ic.deleteSurroundingText(1, 0);
    }
}

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

private void handleBackspace() {
    boolean deleteChar = false;
    InputConnection ic = getCurrentInputConnection();
    if (ic == null)
        return;//from w  ww. j  ava2 s .  c o  m

    ic.beginBatchEdit();

    if (mPredicting) {
        final int length = mComposing.length();
        if (length > 0) {
            mComposing.delete(length - 1, length);
            mWord.deleteLast();
            ic.setComposingText(mComposing, 1);
            if (mComposing.length() == 0) {
                mPredicting = false;
            }
            postUpdateSuggestions();
        } else {
            ic.deleteSurroundingText(1, 0);
        }
    } else {
        deleteChar = true;
    }
    postUpdateShiftKeyState();
    TextEntryState.backspace();
    if (TextEntryState.getState() == TextEntryState.State.UNDO_COMMIT) {
        revertLastWord(deleteChar);
        ic.endBatchEdit();
        return;
    } else if (mEnteredText != null && sameAsTextBeforeCursor(ic, mEnteredText)) {
        ic.deleteSurroundingText(mEnteredText.length(), 0);
    } else if (deleteChar) {
        if (mCandidateView != null && mCandidateView.dismissAddToDictionaryHint()) {
            // Go back to the suggestion mode if the user canceled the
            // "Touch again to save".
            // NOTE: In gerenal, we don't revert the word when backspacing
            // from a manual suggestion pick. We deliberately chose a
            // different behavior only in the case of picking the first
            // suggestion (typed word). It's intentional to have made this
            // inconsistent with backspacing after selecting other
            // suggestions.
            revertLastWord(deleteChar);
        } else {
            sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
            if (mDeleteCount > DELETE_ACCELERATE_AT) {
                sendDownUpKeyEvents(KeyEvent.KEYCODE_DEL);
            }
        }
    }
    mJustRevertedSeparator = null;
    ic.endBatchEdit();
}

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

private void onFunctionKey(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.DELETE:
        if (ic == null)// if we don't want to do anything, lets check null first.
            break;
        // we do backword if the shift is pressed while pressing
        // backspace (like in a PC)
        if (mAskPrefs.useBackword() && mShiftKeyState.isPressed() && !mShiftKeyState.isLocked()) {
            handleBackWord(ic);//w  w  w . j  av a 2  s .c  o  m
        } else {
            handleDeleteLastCharacter(false);
        }
        break;
    case KeyCodes.SHIFT:
        if (fromUI) {
            handleShift();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.SHIFT_LOCK:
        mShiftKeyState.toggleLocked();
        handleShift();
        break;
    case KeyCodes.DELETE_WORD:
        if (ic == null)// if we don't want to do anything, lets check
            // null first.
            break;
        handleBackWord(ic);
        break;
    case KeyCodes.CLEAR_INPUT:
        if (ic != null) {
            ic.beginBatchEdit();
            abortCorrectionAndResetPredictionState(false);
            ic.deleteSurroundingText(Integer.MAX_VALUE, Integer.MAX_VALUE);
            ic.endBatchEdit();
        }
        break;
    case KeyCodes.CTRL:
        if (fromUI) {
            handleControl();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.CTRL_LOCK:
        mControlKeyState.toggleLocked();
        handleControl();
        break;
    case KeyCodes.ARROW_LEFT:
    case KeyCodes.ARROW_RIGHT:
        final int keyEventKeyCode = primaryCode == KeyCodes.ARROW_LEFT ? KeyEvent.KEYCODE_DPAD_LEFT
                : KeyEvent.KEYCODE_DPAD_RIGHT;
        if (!handleSelectionExpending(keyEventKeyCode, ic, mGlobalSelectionStartPosition,
                mGlobalCursorPosition)) {
            sendDownUpKeyEvents(keyEventKeyCode);
        }
        break;
    case KeyCodes.ARROW_UP:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_UP);
        break;
    case KeyCodes.ARROW_DOWN:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_DOWN);
        break;
    case KeyCodes.MOVE_HOME:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            sendDownUpKeyEvents(0x0000007a/*API 11:KeyEvent.KEYCODE_MOVE_HOME*/);
        } else {
            if (ic != null) {
                CharSequence textBefore = ic.getTextBeforeCursor(1024, 0);
                if (!TextUtils.isEmpty(textBefore)) {
                    int newPosition = textBefore.length() - 1;
                    while (newPosition > 0) {
                        char chatAt = textBefore.charAt(newPosition - 1);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition--;
                    }
                    if (newPosition < 0)
                        newPosition = 0;
                    ic.setSelection(newPosition, newPosition);
                }
            }
        }
        break;
    case KeyCodes.MOVE_END:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            //API 11: KeyEvent.KEYCODE_MOVE_END
            sendDownUpKeyEvents(0x0000007b);
        } else {
            if (ic != null) {
                CharSequence textAfter = ic.getTextAfterCursor(1024, 0);
                if (!TextUtils.isEmpty(textAfter)) {
                    int newPosition = 1;
                    while (newPosition < textAfter.length()) {
                        char chatAt = textAfter.charAt(newPosition);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition++;
                    }
                    if (newPosition > textAfter.length())
                        newPosition = textAfter.length();
                    try {
                        CharSequence textBefore = ic.getTextBeforeCursor(Integer.MAX_VALUE, 0);
                        if (!TextUtils.isEmpty(textBefore)) {
                            newPosition = newPosition + textBefore.length();
                        }
                        ic.setSelection(newPosition, newPosition);
                    } catch (Throwable e/*I'm using Integer.MAX_VALUE, it's scary.*/) {
                        Logger.w(TAG, "Failed to getTextBeforeCursor.", e);
                    }
                }
            }
        }
        break;
    case KeyCodes.VOICE_INPUT:
        if (mVoiceRecognitionTrigger.isInstalled()) {
            mVoiceRecognitionTrigger
                    .startVoiceRecognition(getCurrentAlphabetKeyboard().getDefaultDictionaryLocale());
        } else {
            Intent voiceInputNotInstalledIntent = new Intent(getApplicationContext(),
                    VoiceInputNotInstalledActivity.class);
            voiceInputNotInstalledIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(voiceInputNotInstalledIntent);
        }
        break;
    case KeyCodes.CANCEL:
        handleClose();
        break;
    case KeyCodes.SETTINGS:
        showOptionsMenu();
        break;
    case KeyCodes.SPLIT_LAYOUT:
    case KeyCodes.MERGE_LAYOUT:
    case KeyCodes.COMPACT_LAYOUT_TO_RIGHT:
    case KeyCodes.COMPACT_LAYOUT_TO_LEFT:
        if (getInputView() != null) {
            mKeyboardInCondensedMode = CondenseType.fromKeyCode(primaryCode);
            setKeyboardForView(getCurrentKeyboard());
        }
        break;
    case KeyCodes.DOMAIN:
        onText(key, mAskPrefs.getDomainText());
        break;
    case KeyCodes.QUICK_TEXT:
        onQuickTextRequested(key);
        break;
    case KeyCodes.QUICK_TEXT_POPUP:
        onQuickTextKeyboardRequested(key);
        break;
    case KeyCodes.MODE_SYMOBLS:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.Symbols);
        break;
    case KeyCodes.MODE_ALPHABET:
        if (getKeyboardSwitcher().shouldPopupForLanguageSwitch()) {
            showLanguageSelectionDialog();
        } else {
            nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.Alphabet);
        }
        break;
    case KeyCodes.UTILITY_KEYBOARD:
        getInputView().openUtilityKeyboard();
        break;
    case KeyCodes.MODE_ALPHABET_POPUP:
        showLanguageSelectionDialog();
        break;
    case KeyCodes.ALT:
        nextAlterKeyboard(getCurrentInputEditorInfo());
        break;
    case KeyCodes.KEYBOARD_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.Any);
        break;
    case KeyCodes.KEYBOARD_REVERSE_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.PreviousAny);
        break;
    case KeyCodes.KEYBOARD_CYCLE_INSIDE_MODE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.AnyInsideMode);
        break;
    case KeyCodes.KEYBOARD_MODE_CHANGE:
        nextKeyboard(getCurrentInputEditorInfo(), KeyboardSwitcher.NextKeyboardType.OtherMode);
        break;
    case KeyCodes.CLIPBOARD_COPY:
    case KeyCodes.CLIPBOARD_PASTE:
    case KeyCodes.CLIPBOARD_CUT:
    case KeyCodes.CLIPBOARD_SELECT_ALL:
    case KeyCodes.CLIPBOARD_PASTE_POPUP:
    case KeyCodes.CLIPBOARD_SELECT:
        handleClipboardOperation(key, primaryCode, ic);
        //not allowing undo on-text in clipboard paste operations.
        if (primaryCode == KeyCodes.CLIPBOARD_PASTE)
            mCommittedWord = "";
        break;
    default:
        if (BuildConfig.DEBUG) {
            //this should not happen! We should handle ALL function keys.
            throw new RuntimeException("UNHANDLED FUNCTION KEY! primary code " + primaryCode);
        } else {
            Logger.w(TAG, "UNHANDLED FUNCTION KEY! primary code %d. Ignoring.", primaryCode);
        }
    }
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

private void onFunctionKey(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.DELETE:
        if (ic == null)// if we don't want to do anything, lets check null first.
            break;
        // we do backword if the shift is pressed while pressing
        // backspace (like in a PC)
        if (mAskPrefs.useBackword() && mShiftKeyState.isPressed() && !mShiftKeyState.isLocked()) {
            handleBackWord(ic);//ww  w. j  ava2s.  com
        } else {
            handleDeleteLastCharacter(false);
        }
        break;
    case KeyCodes.SHIFT:
        if (fromUI) {
            handleShift();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.SHIFT_LOCK:
        mShiftKeyState.toggleLocked();
        handleShift();
        break;
    case KeyCodes.DELETE_WORD:
        if (ic == null)// if we don't want to do anything, lets check
            // null first.
            break;
        handleBackWord(ic);
        break;
    case KeyCodes.CLEAR_INPUT:
        if (ic != null) {
            ic.beginBatchEdit();
            abortCorrectionAndResetPredictionState(false);
            ic.deleteSurroundingText(Integer.MAX_VALUE, Integer.MAX_VALUE);
            ic.endBatchEdit();
        }
        break;
    case KeyCodes.CTRL:
        if (fromUI) {
            handleControl();
        } else {
            //not from UI (user not actually pressed that button)
            onPress(primaryCode);
            onRelease(primaryCode);
        }
        break;
    case KeyCodes.CTRL_LOCK:
        mControlKeyState.toggleLocked();
        handleControl();
        break;
    case KeyCodes.ARROW_LEFT:
    case KeyCodes.ARROW_RIGHT:
        final int keyEventKeyCode = primaryCode == KeyCodes.ARROW_LEFT ? KeyEvent.KEYCODE_DPAD_LEFT
                : KeyEvent.KEYCODE_DPAD_RIGHT;
        if (!handleSelectionExpending(keyEventKeyCode, ic, mGlobalSelectionStartPosition,
                mGlobalCursorPosition)) {
            sendDownUpKeyEvents(keyEventKeyCode);
        }
        break;
    case KeyCodes.ARROW_UP:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_UP);
        break;
    case KeyCodes.ARROW_DOWN:
        sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_DOWN);
        break;
    case KeyCodes.MOVE_HOME:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            sendDownUpKeyEvents(0x0000007a/*API 11:KeyEvent.KEYCODE_MOVE_HOME*/);
        } else {
            if (ic != null) {
                CharSequence textBefore = ic.getTextBeforeCursor(1024, 0);
                if (!TextUtils.isEmpty(textBefore)) {
                    int newPosition = textBefore.length() - 1;
                    while (newPosition > 0) {
                        char chatAt = textBefore.charAt(newPosition - 1);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition--;
                    }
                    if (newPosition < 0)
                        newPosition = 0;
                    ic.setSelection(newPosition, newPosition);
                }
            }
        }
        break;
    case KeyCodes.MOVE_END:
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            //API 11: KeyEvent.KEYCODE_MOVE_END
            sendDownUpKeyEvents(0x0000007b);
        } else {
            if (ic != null) {
                CharSequence textAfter = ic.getTextAfterCursor(1024, 0);
                if (!TextUtils.isEmpty(textAfter)) {
                    int newPosition = 1;
                    while (newPosition < textAfter.length()) {
                        char chatAt = textAfter.charAt(newPosition);
                        if (chatAt == '\n' || chatAt == '\r') {
                            break;
                        }
                        newPosition++;
                    }
                    if (newPosition > textAfter.length())
                        newPosition = textAfter.length();
                    try {
                        CharSequence textBefore = ic.getTextBeforeCursor(Integer.MAX_VALUE, 0);
                        if (!TextUtils.isEmpty(textBefore)) {
                            newPosition = newPosition + textBefore.length();
                        }
                        ic.setSelection(newPosition, newPosition);
                    } catch (Throwable e/*I'm using Integer.MAX_VALUE, it's scary.*/) {
                        Logger.w(TAG, "Failed to getTextBeforeCursor.", e);
                    }
                }
            }
        }
        break;
    case KeyCodes.VOICE_INPUT:
        if (mVoiceRecognitionTrigger.isInstalled()) {
            mVoiceRecognitionTrigger
                    .startVoiceRecognition(getCurrentAlphabetKeyboard().getDefaultDictionaryLocale());
        } else {
            Intent voiceInputNotInstalledIntent = new Intent(getApplicationContext(),
                    VoiceInputNotInstalledActivity.class);
            voiceInputNotInstalledIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(voiceInputNotInstalledIntent);
        }
        break;
    case KeyCodes.CANCEL:
        hideWindow();
        break;
    case KeyCodes.SETTINGS:
        showOptionsMenu();
        break;
    case KeyCodes.SPLIT_LAYOUT:
    case KeyCodes.MERGE_LAYOUT:
    case KeyCodes.COMPACT_LAYOUT_TO_RIGHT:
    case KeyCodes.COMPACT_LAYOUT_TO_LEFT:
        if (getInputView() != null) {
            mKeyboardInCondensedMode = CondenseType.fromKeyCode(primaryCode);
            setKeyboardForView(getCurrentKeyboard());
        }
        break;
    case KeyCodes.DOMAIN:
        onText(key, mAskPrefs.getDomainText());
        break;
    case KeyCodes.QUICK_TEXT:
        onQuickTextRequested(key);
        break;
    case KeyCodes.QUICK_TEXT_POPUP:
        onQuickTextKeyboardRequested(key);
        break;
    case KeyCodes.MODE_SYMOBLS:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Symbols);
        break;
    case KeyCodes.MODE_ALPHABET:
        if (getKeyboardSwitcher().shouldPopupForLanguageSwitch()) {
            showLanguageSelectionDialog();
        } else {
            nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Alphabet);
        }
        break;
    case KeyCodes.UTILITY_KEYBOARD:
        getInputView().openUtilityKeyboard();
        break;
    case KeyCodes.MODE_ALPHABET_POPUP:
        showLanguageSelectionDialog();
        break;
    case KeyCodes.ALT:
        nextAlterKeyboard(getCurrentInputEditorInfo());
        break;
    case KeyCodes.KEYBOARD_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.Any);
        break;
    case KeyCodes.KEYBOARD_REVERSE_CYCLE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.PreviousAny);
        break;
    case KeyCodes.KEYBOARD_CYCLE_INSIDE_MODE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.AnyInsideMode);
        break;
    case KeyCodes.KEYBOARD_MODE_CHANGE:
        nextKeyboard(getCurrentInputEditorInfo(), NextKeyboardType.OtherMode);
        break;
    case KeyCodes.CLIPBOARD_COPY:
    case KeyCodes.CLIPBOARD_PASTE:
    case KeyCodes.CLIPBOARD_CUT:
    case KeyCodes.CLIPBOARD_SELECT_ALL:
    case KeyCodes.CLIPBOARD_PASTE_POPUP:
    case KeyCodes.CLIPBOARD_SELECT:
        handleClipboardOperation(key, primaryCode, ic);
        //not allowing undo on-text in clipboard paste operations.
        if (primaryCode == KeyCodes.CLIPBOARD_PASTE)
            mCommittedWord = "";
        break;
    default:
        if (BuildConfig.DEBUG) {
            //this should not happen! We should handle ALL function keys.
            throw new RuntimeException("UNHANDLED FUNCTION KEY! primary code " + primaryCode);
        } else {
            Logger.w(TAG, "UNHANDLED FUNCTION KEY! primary code %d. Ignoring.", primaryCode);
        }
    }
}

From source file:com.anysoftkeyboard.AnySoftKeyboard.java

private void handleSeparator(int primaryCode) {
    // Issue 146: Right to left languages require reversed parenthesis
    if (!getCurrentAlphabetKeyboard().isLeftToRightLanguage()) {
        if (primaryCode == (int) ')')
            primaryCode = (int) '(';
        else if (primaryCode == (int) '(')
            primaryCode = (int) ')';
    }/*from   www  .  j a v  a2s  .c o m*/
    mExpectingSelectionUpdateBy = SystemClock.uptimeMillis() + MAX_TIME_TO_EXPECT_SELECTION_UPDATE;
    //will not show next-word suggestion in case of a new line or if the separator is a sentence separator.
    boolean isEndOfSentence = (primaryCode == KeyCodes.ENTER || mSentenceSeparators.get(primaryCode));

    // Should dismiss the "Touch again to save" message when handling
    // separator
    if (mCandidateView != null && mCandidateView.dismissAddToDictionaryHint()) {
        postUpdateSuggestions();
    }

    // Handle separator
    InputConnection ic = getCurrentInputConnection();
    if (ic != null) {
        ic.beginBatchEdit();
    }
    // this is a special case, when the user presses a separator WHILE
    // inside the predicted word.
    // in this case, I will want to just dump the separator.
    final boolean separatorInsideWord = (mWord.cursorPosition() < mWord.length());
    if (TextEntryState.isPredicting() && !separatorInsideWord) {
        //ACTION does not invoke default picking. See https://github.com/AnySoftKeyboard/AnySoftKeyboard/issues/198
        pickDefaultSuggestion(mAutoCorrectOn && primaryCode != KeyCodes.ENTER);
        // Picked the suggestion by a space/punctuation character: we will treat it
        // as "added an auto space".
        mJustAddedAutoSpace = true;
    } else if (separatorInsideWord) {
        // when putting a separator in the middle of a word, there is no
        // need to do correction, or keep knowledge
        abortCorrectionAndResetPredictionState(false);
    }

    if (mJustAddedAutoSpace && primaryCode == KeyCodes.ENTER) {
        removeTrailingSpace();
        mJustAddedAutoSpace = false;
    }

    boolean handledOutputToInputConnection = false;

    if (ic != null) {
        if (primaryCode == KeyCodes.SPACE) {
            if (mAskPrefs.isDoubleSpaceChangesToPeriod()) {
                if ((SystemClock.uptimeMillis()
                        - mLastSpaceTimeStamp) < ((long) mAskPrefs.getMultiTapTimeout())) {
                    //current text in the input-box should be something like "word "
                    //the user pressed on space again. So we want to change the text in the input-box
                    //into "word "->"word. "
                    ic.deleteSurroundingText(1, 0);
                    ic.commitText(". ", 1);
                    mJustAddedAutoSpace = true;
                    isEndOfSentence = true;
                    handledOutputToInputConnection = true;
                }
            }
        } else if (mJustAddedAutoSpace && mLastSpaceTimeStamp != NEVER_TIME_STAMP/*meaning last key was SPACE*/
                && mAskPrefs.shouldSwapPunctuationAndSpace() && primaryCode != KeyCodes.ENTER
                && isSentenceSeparator(primaryCode)) {
            //current text in the input-box should be something like "word "
            //the user pressed a punctuation (say ","). So we want to change the text in the input-box
            //into "word "->"word, "
            ic.deleteSurroundingText(1, 0);
            ic.commitText(((char) primaryCode) + " ", 1);
            mJustAddedAutoSpace = true;
            handledOutputToInputConnection = true;
        }
    }

    if (!handledOutputToInputConnection) {
        sendKeyChar((char) primaryCode);
    }
    TextEntryState.typedCharacter((char) primaryCode, true);

    if (ic != null) {
        ic.endBatchEdit();
    }

    if (isEndOfSentence) {
        mSuggest.resetNextWordSentence();
        clearSuggestions();
    } else if (!TextUtils.isEmpty(mCommittedWord)) {
        setSuggestions(mSuggest.getNextSuggestions(mCommittedWord, mWord.isAllUpperCase()), false, false,
                false);
        mWord.setFirstCharCapitalized(false);
    }
}

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

@Override
public void onPress(final int primaryCode) {
    setVibration();/* ww w. j av a2s .c  om*/

    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;
        }
    }
}