switch to letters keyboard if Space is pressed with symbols keyboard

This commit is contained in:
tibbi 2022-01-19 14:36:22 +01:00
parent 05f98338a9
commit ce45a14fa7
2 changed files with 39 additions and 5 deletions

View File

@ -7,6 +7,7 @@ import android.view.KeyEvent
import android.view.View import android.view.View
import android.view.inputmethod.EditorInfo import android.view.inputmethod.EditorInfo
import android.view.inputmethod.EditorInfo.IME_ACTION_NONE import android.view.inputmethod.EditorInfo.IME_ACTION_NONE
import android.view.inputmethod.ExtractedTextRequest
import com.simplemobiletools.commons.extensions.performHapticFeedback import com.simplemobiletools.commons.extensions.performHapticFeedback
import com.simplemobiletools.keyboard.R import com.simplemobiletools.keyboard.R
import com.simplemobiletools.keyboard.helpers.MyKeyboard import com.simplemobiletools.keyboard.helpers.MyKeyboard
@ -28,6 +29,7 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
private var keyboardMode = KEYBOARD_LETTERS private var keyboardMode = KEYBOARD_LETTERS
private var inputTypeClass = InputType.TYPE_CLASS_TEXT private var inputTypeClass = InputType.TYPE_CLASS_TEXT
private var enterKeyType = IME_ACTION_NONE private var enterKeyType = IME_ACTION_NONE
private var switchToLetters = false
override fun onInitializeInterface() { override fun onInitializeInterface() {
super.onInitializeInterface() super.onInitializeInterface()
@ -144,11 +146,16 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
code = Character.toUpperCase(code) code = Character.toUpperCase(code)
} }
inputConnection.commitText(code.toString(), 1) // If the keyboard is set to symbols and the user presses space, we usually should switch back to the letters keyboard.
if (primaryCode == MyKeyboard.KEYCODE_SPACE && keyboardMode != KEYBOARD_LETTERS) { // However, avoid doing that in cases when the EditText for example requires numbers as the input.
keyboardMode = KEYBOARD_LETTERS // We can detect that by the text not changing on pressing Space.
keyboard = MyKeyboard(this, R.xml.keys_letters, enterKeyType) if (keyboardMode != KEYBOARD_LETTERS && primaryCode == MyKeyboard.KEYCODE_SPACE) {
keyboardView!!.setKeyboard(keyboard!!) val originalText = inputConnection.getExtractedText(ExtractedTextRequest(), 0).text
inputConnection.commitText(code.toString(), 1)
val newText = inputConnection.getExtractedText(ExtractedTextRequest(), 0).text
switchToLetters = originalText != newText
} else {
inputConnection.commitText(code.toString(), 1)
} }
if (keyboard!!.shiftState == SHIFT_ON_ONE_CHAR && keyboardMode == KEYBOARD_LETTERS) { if (keyboard!!.shiftState == SHIFT_ON_ONE_CHAR && keyboardMode == KEYBOARD_LETTERS) {
@ -163,6 +170,23 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
} }
} }
override fun onActionUp() {
if (switchToLetters) {
keyboardMode = KEYBOARD_LETTERS
keyboard = MyKeyboard(this, R.xml.keys_letters, enterKeyType)
val editorInfo = currentInputEditorInfo
if (editorInfo != null && editorInfo.inputType != InputType.TYPE_NULL && keyboard?.shiftState != SHIFT_ON_PERMANENT) {
if (currentInputConnection.getCursorCapsMode(editorInfo.inputType) != 0) {
keyboard?.setShifted(SHIFT_ON_ONE_CHAR)
}
}
keyboardView!!.setKeyboard(keyboard!!)
switchToLetters = false
}
}
override fun onText(text: CharSequence?) {} override fun onText(text: CharSequence?) {}
override fun swipeLeft() {} override fun swipeLeft() {}

View File

@ -98,6 +98,11 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
* Called when the user quickly moves the finger from down to up. * Called when the user quickly moves the finger from down to up.
*/ */
fun swipeUp() fun swipeUp()
/**
* Called when the finger has been lifted after pressing a key
*/
fun onActionUp()
} }
private var mKeyboard: MyKeyboard? = null private var mKeyboard: MyKeyboard? = null
@ -1060,6 +1065,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
override fun onRelease(primaryCode: Int) { override fun onRelease(primaryCode: Int) {
onKeyboardActionListener!!.onRelease(primaryCode) onKeyboardActionListener!!.onRelease(primaryCode)
} }
override fun onActionUp() {
onKeyboardActionListener!!.onActionUp()
}
} }
//mInputView.setSuggest(mSuggest); //mInputView.setSuggest(mSuggest);
@ -1369,6 +1378,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
} }
invalidateKey(keyIndex) invalidateKey(keyIndex)
mRepeatKeyIndex = NOT_A_KEY mRepeatKeyIndex = NOT_A_KEY
onKeyboardActionListener!!.onActionUp()
} }
MotionEvent.ACTION_CANCEL -> { MotionEvent.ACTION_CANCEL -> {
removeMessages() removeMessages()