Question

I'm currently working on own implementation of Input Method Editor (IME) or can be called as Softkeyboard in Android, I have read creating an input method and I have downloaded the SoftKeyboard sample code provided as part of the SDK. I have the following code from sample Softkeyboard:

private void handleCharacter(int primaryCode, int[] keyCodes) {
    if (isInputViewShown()) {
        if (mInputView.isShifted()) {
            primaryCode = Character.toUpperCase(primaryCode);
        }
    }
    if (isAlphabet(primaryCode) && mPredictionOn) {
        /**
         * Swapping here with my desired unicode character
         * */
        if (primaryCode >= 97 && primaryCode <= 122 ) {
            mComposing.append(Swap.swapLetters(primaryCode));
        }else{
            mComposing.append((char) primaryCode);
        }
        getCurrentInputConnection().setComposingText(mComposing, 1);
        updateShiftKeyState(getCurrentInputEditorInfo());
        updateCandidates();
    } else {
        getCurrentInputConnection().commitText(
                String.valueOf((char) primaryCode), 1);
    }
}

the code given above works fine, but when I click the key:

<Key android:codes="-1" 
            android:keyWidth="15%p" android:isModifier="true"
            android:isSticky="true" android:keyEdgeFlags="left"/>

which is like Shift key, this converts the keys to uppercase I don't know how to show my next keys this key is clicked/pressed, the following is Logcat of exception:

Logcat of exception

I have traced that this might be occurring in this place where we handle the Shift key :

private void handleShift() {
    if (mInputView == null) {
        return;
    }

    Keyboard currentKeyboard = mInputView.getKeyboard();
    if (mQwertyKeyboard == currentKeyboard) {
        // Alphabet keyboard
        checkToggleCapsLock();          
        mInputView.setKeyboard(mSindhi);            
    } 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);
    }
}

now I have not any idea how to get rid of exception mentioned above, and get my code working. please solve this one!

Was it helpful?

Solution

There may be some other solutions for your problem, but I have done one trick and came up with following :

Step-1 You have to remove all the calls to the method updateShiftKeyState(getCurrentInputEditorInfo()); in your code like:

private void handleCharacter(int primaryCode, int[] keyCodes) {
if (isInputViewShown()) {
    if (mInputView.isShifted()) {
        primaryCode = Character.toUpperCase(primaryCode);
    }
}
if (isAlphabet(primaryCode) && mPredictionOn) {
    /**
     * Swapping here with my desired unicode character
     * */
    if (primaryCode >= 97 && primaryCode <= 122 ) {
        mComposing.append(Swap.swapLetters(primaryCode));
    }else{
        mComposing.append((char) primaryCode);
    }
    getCurrentInputConnection().setComposingText(mComposing, 1);
    //updateShiftKeyState(getCurrentInputEditorInfo()); // you can delete this method from your class and clean-up all the occurances of that 
    updateCandidates();
} else {
    getCurrentInputConnection().commitText(
            String.valueOf((char) primaryCode), 1);
}}

Step-2 change your handleShift method to his:

private void handleShift() {
    if (mInputView == null) {
        return;
    }

    Keyboard currentKeyboard = mInputView.getKeyboard();
    if (mQwertyKeyboard == currentKeyboard) {           
        mInputView.setKeyboard(mSindhi);

    } else if (currentKeyboard == mSymbolsKeyboard) {
        mInputView.setKeyboard(mSymbolsShiftedKeyboard);
    } else if (currentKeyboard == mSymbolsShiftedKeyboard) {
        mInputView.setKeyboard(mSymbolsKeyboard);
    }
}

that is all, Hope this may work for you...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top