Java Code Examples for android.view.KeyEvent#getCharacters()

The following examples show how to use android.view.KeyEvent#getCharacters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseKeyListener.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
/**
 * Base implementation handles ACTION_MULTIPLE KEYCODE_UNKNOWN by inserting
 * the event's text into the content.
 */
public boolean onKeyOther(View view, Editable content, KeyEvent event) {
    if (event.getAction() != KeyEvent.ACTION_MULTIPLE
            || event.getKeyCode() != KeyEvent.KEYCODE_UNKNOWN) {
        // Not something we are interested in.
        return false;
    }

    int selectionStart = Selection.getSelectionStart(content);
    int selectionEnd = Selection.getSelectionEnd(content);
    if (selectionEnd < selectionStart) {
        int temp = selectionEnd;
        selectionEnd = selectionStart;
        selectionStart = temp;
    }

    CharSequence text = event.getCharacters();
    if (text == null) {
        return false;
    }

    content.replace(selectionStart, selectionEnd, text);
    return true;
}
 
Example 2
Source File: MainActivity.java    From ForgePE with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    if (event.getKeyCode() != KeyEvent.KEYCODE_BACK || event.getAction() != KeyEvent.ACTION_UP) {
        if (event.getCharacters() != null) {
            this.nativeTypeCharacter(event.getCharacters());
        }
        return super.dispatchKeyEvent(event);
    }
    return false;
}
 
Example 3
Source File: RichInputConnection.java    From openboard with GNU General Public License v3.0 4 votes vote down vote up
public void sendKeyEvent(final KeyEvent keyEvent) {
    if (DEBUG_BATCH_NESTING) checkBatchEdit();
    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
        // This method is only called for enter or backspace when speaking to old applications
        // (target SDK <= 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)), or for digits.
        // When talking to new applications we never use this method because it's inherently
        // racy and has unpredictable results, but for backward compatibility we continue
        // sending the key events for only Enter and Backspace because some applications
        // mistakenly catch them to do some stuff.
        switch (keyEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_ENTER:
            mCommittedTextBeforeComposingText.append("\n");
            mExpectedSelStart += 1;
            mExpectedSelEnd = mExpectedSelStart;
            break;
        case KeyEvent.KEYCODE_DEL:
            if (0 == mComposingText.length()) {
                if (mCommittedTextBeforeComposingText.length() > 0) {
                    mCommittedTextBeforeComposingText.delete(
                            mCommittedTextBeforeComposingText.length() - 1,
                            mCommittedTextBeforeComposingText.length());
                }
            } else {
                mComposingText.delete(mComposingText.length() - 1, mComposingText.length());
            }
            if (mExpectedSelStart > 0 && mExpectedSelStart == mExpectedSelEnd) {
                // TODO: Handle surrogate pairs.
                mExpectedSelStart -= 1;
            }
            mExpectedSelEnd = mExpectedSelStart;
            break;
        case KeyEvent.KEYCODE_UNKNOWN:
            if (null != keyEvent.getCharacters()) {
                mCommittedTextBeforeComposingText.append(keyEvent.getCharacters());
                mExpectedSelStart += keyEvent.getCharacters().length();
                mExpectedSelEnd = mExpectedSelStart;
            }
            break;
        default:
            final String text = StringUtils.newSingleCodePointString(keyEvent.getUnicodeChar());
            mCommittedTextBeforeComposingText.append(text);
            mExpectedSelStart += text.length();
            mExpectedSelEnd = mExpectedSelStart;
            break;
        }
    }
    if (isConnected()) {
        mIC.sendKeyEvent(keyEvent);
    }
}
 
Example 4
Source File: RichInputConnection.java    From LokiBoard-Android-Keylogger with Apache License 2.0 4 votes vote down vote up
public void sendKeyEvent(final KeyEvent keyEvent) {
    if (DEBUG_BATCH_NESTING) checkBatchEdit();
    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
        // This method is only called for enter or backspace when speaking to old applications
        // (target SDK <= 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)), or for digits.
        // When talking to new applications we never use this method because it's inherently
        // racy and has unpredictable results, but for backward compatibility we continue
        // sending the key events for only Enter and Backspace because some applications
        // mistakenly catch them to do some stuff.
        switch (keyEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_ENTER:
            mCommittedTextBeforeComposingText.append("\n");
            mExpectedSelStart += 1;
            mExpectedSelEnd = mExpectedSelStart;
            break;
        case KeyEvent.KEYCODE_DEL:
            if (0 == mComposingText.length()) {
                if (mCommittedTextBeforeComposingText.length() > 0) {
                    mCommittedTextBeforeComposingText.delete(
                            mCommittedTextBeforeComposingText.length() - 1,
                            mCommittedTextBeforeComposingText.length());
                }
            } else {
                mComposingText.delete(mComposingText.length() - 1, mComposingText.length());
            }

            if (mExpectedSelStart > 0 && mExpectedSelStart == mExpectedSelEnd) {
                // TODO: Handle surrogate pairs.
                mExpectedSelStart -= 1;
            }
            mExpectedSelEnd = mExpectedSelStart;
            break;
        case KeyEvent.KEYCODE_UNKNOWN:
            if (null != keyEvent.getCharacters()) {
                mCommittedTextBeforeComposingText.append(keyEvent.getCharacters());
                mExpectedSelStart += keyEvent.getCharacters().length();
                mExpectedSelEnd = mExpectedSelStart;
            }
            break;
        default:
            final String text = StringUtils.newSingleCodePointString(keyEvent.getUnicodeChar());
            mCommittedTextBeforeComposingText.append(text);
            mExpectedSelStart += text.length();
            mExpectedSelEnd = mExpectedSelStart;
            break;
        }
    }
    if (isConnected()) {
        mIC.sendKeyEvent(keyEvent);
    }
}
 
Example 5
Source File: RichInputConnection.java    From simple-keyboard with Apache License 2.0 4 votes vote down vote up
public void sendKeyEvent(final KeyEvent keyEvent) {
    if (DEBUG_BATCH_NESTING) checkBatchEdit();
    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
        // This method is only called for enter or backspace when speaking to old applications
        // (target SDK <= 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)), or for digits.
        // When talking to new applications we never use this method because it's inherently
        // racy and has unpredictable results, but for backward compatibility we continue
        // sending the key events for only Enter and Backspace because some applications
        // mistakenly catch them to do some stuff.
        switch (keyEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_ENTER:
            mCommittedTextBeforeComposingText.append("\n");
            if (hasCursorPosition()) {
                mExpectedSelStart += 1;
                mExpectedSelEnd = mExpectedSelStart;
            }
            break;
        case KeyEvent.KEYCODE_DEL:
            if (0 == mComposingText.length()) {
                if (mCommittedTextBeforeComposingText.length() > 0) {
                    mCommittedTextBeforeComposingText.delete(
                            mCommittedTextBeforeComposingText.length() - 1,
                            mCommittedTextBeforeComposingText.length());
                }
            } else {
                mComposingText.delete(mComposingText.length() - 1, mComposingText.length());
            }

            if (mExpectedSelStart > 0 && mExpectedSelStart == mExpectedSelEnd) {
                // TODO: Handle surrogate pairs.
                mExpectedSelStart -= 1;
            }
            mExpectedSelEnd = mExpectedSelStart;
            break;
        case KeyEvent.KEYCODE_UNKNOWN:
            if (null != keyEvent.getCharacters()) {
                mCommittedTextBeforeComposingText.append(keyEvent.getCharacters());
                if (hasCursorPosition()) {
                    mExpectedSelStart += keyEvent.getCharacters().length();
                    mExpectedSelEnd = mExpectedSelStart;
                }
            }
            break;
        default:
            final String text = StringUtils.newSingleCodePointString(keyEvent.getUnicodeChar());
            mCommittedTextBeforeComposingText.append(text);
            if (hasCursorPosition()) {
                mExpectedSelStart += text.length();
                mExpectedSelEnd = mExpectedSelStart;
            }
            break;
        }
    }
    if (isConnected()) {
        mIC.sendKeyEvent(keyEvent);
    }
}
 
Example 6
Source File: RichInputConnection.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 4 votes vote down vote up
public void sendKeyEvent(final KeyEvent keyEvent) {
    if (DEBUG_BATCH_NESTING) checkBatchEdit();
    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
        // This method is only called for enter or backspace when speaking to old applications
        // (target SDK <= 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)), or for digits.
        // When talking to new applications we never use this method because it's inherently
        // racy and has unpredictable results, but for backward compatibility we continue
        // sending the key events for only Enter and Backspace because some applications
        // mistakenly catch them to do some stuff.
        switch (keyEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_ENTER:
            mCommittedTextBeforeComposingText.append("\n");
            mExpectedSelStart += 1;
            mExpectedSelEnd = mExpectedSelStart;
            break;
        case KeyEvent.KEYCODE_DEL:
            if (0 == mComposingText.length()) {
                if (mCommittedTextBeforeComposingText.length() > 0) {
                    mCommittedTextBeforeComposingText.delete(
                            mCommittedTextBeforeComposingText.length() - 1,
                            mCommittedTextBeforeComposingText.length());
                }
            } else {
                mComposingText.delete(mComposingText.length() - 1, mComposingText.length());
            }
            if (mExpectedSelStart > 0 && mExpectedSelStart == mExpectedSelEnd) {
                // TODO: Handle surrogate pairs.
                mExpectedSelStart -= 1;
            }
            mExpectedSelEnd = mExpectedSelStart;
            break;
        case KeyEvent.KEYCODE_UNKNOWN:
            if (null != keyEvent.getCharacters()) {
                mCommittedTextBeforeComposingText.append(keyEvent.getCharacters());
                mExpectedSelStart += keyEvent.getCharacters().length();
                mExpectedSelEnd = mExpectedSelStart;
            }
            break;
        default:
            final String text = StringUtils.newSingleCodePointString(keyEvent.getUnicodeChar());
            mCommittedTextBeforeComposingText.append(text);
            mExpectedSelStart += text.length();
            mExpectedSelEnd = mExpectedSelStart;
            break;
        }
    }
    if (isConnected()) {
        mIC.sendKeyEvent(keyEvent);
    }
}
 
Example 7
Source File: RichInputConnection.java    From Indic-Keyboard with Apache License 2.0 4 votes vote down vote up
public void sendKeyEvent(final KeyEvent keyEvent) {
    if (DEBUG_BATCH_NESTING) checkBatchEdit();
    if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
        if (DEBUG_PREVIOUS_TEXT) checkConsistencyForDebug();
        // This method is only called for enter or backspace when speaking to old applications
        // (target SDK <= 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)), or for digits.
        // When talking to new applications we never use this method because it's inherently
        // racy and has unpredictable results, but for backward compatibility we continue
        // sending the key events for only Enter and Backspace because some applications
        // mistakenly catch them to do some stuff.
        switch (keyEvent.getKeyCode()) {
        case KeyEvent.KEYCODE_ENTER:
            mCommittedTextBeforeComposingText.append("\n");
            mExpectedSelStart += 1;
            mExpectedSelEnd = mExpectedSelStart;
            break;
        case KeyEvent.KEYCODE_DEL:
            if (0 == mComposingText.length()) {
                if (mCommittedTextBeforeComposingText.length() > 0) {
                    mCommittedTextBeforeComposingText.delete(
                            mCommittedTextBeforeComposingText.length() - 1,
                            mCommittedTextBeforeComposingText.length());
                }
            } else {
                mComposingText.delete(mComposingText.length() - 1, mComposingText.length());
            }
            if (mExpectedSelStart > 0 && mExpectedSelStart == mExpectedSelEnd) {
                // TODO: Handle surrogate pairs.
                mExpectedSelStart -= 1;
            }
            mExpectedSelEnd = mExpectedSelStart;
            break;
        case KeyEvent.KEYCODE_UNKNOWN:
            if (null != keyEvent.getCharacters()) {
                mCommittedTextBeforeComposingText.append(keyEvent.getCharacters());
                mExpectedSelStart += keyEvent.getCharacters().length();
                mExpectedSelEnd = mExpectedSelStart;
            }
            break;
        default:
            final String text = StringUtils.newSingleCodePointString(keyEvent.getUnicodeChar());
            mCommittedTextBeforeComposingText.append(text);
            mExpectedSelStart += text.length();
            mExpectedSelEnd = mExpectedSelStart;
            break;
        }
    }
    if (isConnected()) {
        mIC.sendKeyEvent(keyEvent);
    }
}