Code example for KeyboardView

Methods: getKeyboard, isShifted, setKeyboard, setShifted

0
     * Helper to update the shift state of our keyboard based on the initial 
     * editor state. 
     */ 
    private void updateShiftKeyState(EditorInfo attr) {
        if (attr != null 
                && mKeyboardView != null && mMainKeyboard == mKeyboardView.getKeyboard()) {
            int caps = 0;
            EditorInfo ei = getCurrentInputEditorInfo();
            if (ei != null && ei.inputType != EditorInfo.TYPE_NULL) {
                caps = getCurrentInputConnection().getCursorCapsMode(attr.inputType);
            } 
            mKeyboardView.setShifted(mCapsLock || caps != 0);
        } 
    } 
     
    /** 
     * Helper to determine if a given character code is alphabetic. 
     */ 
    private boolean isAlphabet(int code) {
        if (Character.isLetter(code)) {
            return true; 
        } else { 
            return false; 
        } 
    } 
     
    /** 
     * Helper to send a key down / key up pair to the current editor. 
     */ 
    private void keyDownUp(int keyEventCode) {
        getCurrentInputConnection().sendKeyEvent(
                new KeyEvent(KeyEvent.ACTION_DOWN, keyEventCode));
        getCurrentInputConnection().sendKeyEvent(
                new KeyEvent(KeyEvent.ACTION_UP, keyEventCode));
    } 
     
    /** 
     * Helper to send a character to the editor as raw key events. 
     */ 
    private void sendKey(int keyCode) {
        switch (keyCode) {
            case '\n': 
                keyDownUp(KeyEvent.KEYCODE_ENTER);
                break; 
            default: 
                if (keyCode >= '0' && keyCode <= '9') {
                    keyDownUp(keyCode - '0' + KeyEvent.KEYCODE_0);
                } else { 
                    getCurrentInputConnection().commitText(String.valueOf((char) keyCode), 1);
                } 
                break; 
        } 
    } 
 
     
    // Implementation of KeyboardViewListener 
    public void onPress(int primaryCode){
    	Resources res = getResources();
    	if(mKeyboardView.getKeyboard() == mMainKeyboard){
    		if(primaryCode == res.getInteger(R.integer.first_quarter_code)){
    			mKeyboardView.setKeyboard(mFirstQuarterKeyboard);
    		} else if(primaryCode == res.getInteger(R.integer.second_quarter_code)){
    			mKeyboardView.setKeyboard(mSecondQuarterKeyboard);
    		} else if(primaryCode == res.getInteger(R.integer.third_quarter_code)){
    			mKeyboardView.setKeyboard(mThirdQuarterKeyboard);
    		} else if(primaryCode == res.getInteger(R.integer.fourth_quarter_code)){
    			mKeyboardView.setKeyboard(mFourthQuarterKeyboard);
    		} 
    		// Important! 
    		mKeyboardView.onTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis()-4, SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 200, 700, 0));
    	} 
    } 
     
    public void onRelease(int primaryCode) {
    	if(mKeyboardView != null && mKeyboardView.getKeyboard() != mMainKeyboard){
    		mKeyboardView.setKeyboard(mMainKeyboard);
    	} 
    } 
 
    public void onKey(int primaryCode, int[] keyCodes) {
        if (isWordSeparator(primaryCode)) {
            // Handle separator 
            if (mComposing.length() > 0) {
                commitTyped(getCurrentInputConnection());
            } 
            sendKey(primaryCode);
            updateShiftKeyState(getCurrentInputEditorInfo());
        } else if (primaryCode == Keyboard.KEYCODE_DELETE) {
            handleBackspace(); 
        } else if (primaryCode == Keyboard.KEYCODE_SHIFT) {
            handleShift(); 
        } else if (primaryCode == Keyboard.KEYCODE_CANCEL) {
            handleClose(); 
            return; 
        } else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE
                && mKeyboardView != null) {
            Keyboard current = mKeyboardView.getKeyboard();
            if (current == mSymbolsKeyboard || current == mSymbolsShiftedKeyboard) {
                current = mMainKeyboard;
            } else { 
                current = mSymbolsKeyboard;
            } 
            mKeyboardView.setKeyboard(current);
            if (current == mSymbolsKeyboard) {
                current.setShifted(false);
            } 
        } else { 
            handleCharacter(primaryCode, keyCodes);
        } 
    } 
     
    public void onText(CharSequence text) {
        InputConnection ic = getCurrentInputConnection();
        if (ic == null) return;
        ic.beginBatchEdit();
        if (mComposing.length() > 0) {
            commitTyped(ic);
        } 
        ic.commitText(text, 0);
        ic.endBatchEdit();
        updateShiftKeyState(getCurrentInputEditorInfo());
    } 
 
    /** 
     * Update the list of available candidates from the current composing 
     * text.  This will need to be filled in by however you are determining 
     * candidates. 
     */ 
    private void updateCandidates() { 
        if (!mCompletionOn) {
            if (mComposing.length() > 0) {
                ArrayList<String> list = new ArrayList<String>();
                list.add(mComposing.toString());
                setSuggestions(list, true, true);
            } else { 
                setSuggestions(null, false, false); 
            } 
        } 
    } 
     
    public void setSuggestions(List<String> suggestions, boolean completions,
            boolean typedWordValid) {
        if (suggestions != null && suggestions.size() > 0) {
            setCandidatesViewShown(true);
        } else if (isExtractViewShown()) {
            setCandidatesViewShown(true);
        } 
        if (mCandidateView != null) {
            mCandidateView.setSuggestions(suggestions, completions, typedWordValid);
        } 
    } 
     
    private void handleBackspace() { 
        final int length = mComposing.length();
        if (length > 1) {
            mComposing.delete(length - 1, length);
            getCurrentInputConnection().setComposingText(mComposing, 1);
            updateCandidates(); 
        } else if (length > 0) {
            mComposing.setLength(0);
            getCurrentInputConnection().commitText("", 0);
            updateCandidates(); 
        } else { 
            keyDownUp(KeyEvent.KEYCODE_DEL);
        } 
        updateShiftKeyState(getCurrentInputEditorInfo());
    } 
 
    private void handleShift() { 
        if (mKeyboardView == null) {
            return; 
        } 
         
        Keyboard currentKeyboard = mKeyboardView.getKeyboard();
        if (mMainKeyboard == currentKeyboard) {
            // Alphabet keyboard 
            checkToggleCapsLock(); 
            mKeyboardView.setShifted(mCapsLock || !mKeyboardView.isShifted());
        } else if (currentKeyboard == mSymbolsKeyboard) {
            mSymbolsKeyboard.setShifted(true);
            mKeyboardView.setKeyboard(mSymbolsShiftedKeyboard);
            mSymbolsShiftedKeyboard.setShifted(true);
        } else if (currentKeyboard == mSymbolsShiftedKeyboard) {
            mSymbolsShiftedKeyboard.setShifted(false);
            mKeyboardView.setKeyboard(mSymbolsKeyboard);
            mSymbolsKeyboard.setShifted(false);
        } 
    } 
     
    private void handleCharacter(int primaryCode, int[] keyCodes) {
        if (isInputViewShown()) {
            if (mKeyboardView.isShifted()) {
                primaryCode = Character.toUpperCase(primaryCode);
            } 
        } 
        if (isAlphabet(primaryCode) && mPredictionOn) {
            mComposing.append((char) primaryCode);