Code example for KeyboardView

Methods: getKeyboard, isShifted, 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 && mInputView != null
				&& mQwertyKeyboard == mInputView.getKeyboard()) {
			int caps = 0;
			EditorInfo ei = getCurrentInputEditorInfo();
			if (ei != null && ei.inputType != EditorInfo.TYPE_NULL) {
				caps = getCurrentInputConnection().getCursorCapsMode(
						attr.inputType);
			} 
			mInputView.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; 
		} 
	} 
 
	/* 
	 * public void externalKeyPress(int keyEventCode) { keyDownUp(keyEventCode); 
	 * } 
	 */ 
	/** 
	 * 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 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 == LatinKeyboardView.KEYCODE_OPTIONS) {
			// Show a menu or somethin' 
		} else if (primaryCode == Keyboard.KEYCODE_MODE_CHANGE
				&& mInputView != null) {
			Keyboard current = mInputView.getKeyboard();
			if (current == mSymbolsKeyboard
					|| current == mSymbolsShiftedKeyboard) {
				current = mQwertyKeyboard;
			} else { 
				current = mSymbolsKeyboard;
			} 
			mInputView.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 (mInputView == null) {
			return; 
		} 
 
		Keyboard currentKeyboard = mInputView.getKeyboard();
		if (mQwertyKeyboard == currentKeyboard) {
			// Alphabet keyboard 
			checkToggleCapsLock(); 
			mInputView.setShifted(mCapsLock || !mInputView.isShifted());
		} else if (currentKeyboard == mSymbolsKeyboard) {
			mSymbolsKeyboard.setShifted(true);
			mInputView.setKeyboard(mSymbolsShiftedKeyboard);
			mSymbolsShiftedKeyboard.setShifted(true);
		} else if (currentKeyboard == mSymbolsShiftedKeyboard) {
			mSymbolsShiftedKeyboard.setShifted(false);
			mInputView.setKeyboard(mSymbolsKeyboard);
			mSymbolsKeyboard.setShifted(false);
		} 
	} 
 
	private void handleCharacter(int primaryCode, int[] keyCodes) {
		if (isInputViewShown()) {
			if (mInputView.isShifted()) {
				primaryCode = Character.toUpperCase(primaryCode);
			} 
		} 
		if (isAlphabet(primaryCode) && mPredictionOn) {
			mComposing.append((char) primaryCode);