add toggling between keyboard modes

This commit is contained in:
tibbi 2022-01-15 21:21:55 +01:00
parent 9dc7abaaa7
commit 3ddd9609c1
2 changed files with 35 additions and 9 deletions

View File

@ -15,10 +15,14 @@ import com.simplemobiletools.keyboard.views.MyKeyboardView
// based on https://www.androidauthority.com/lets-build-custom-keyboard-android-832362/
class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionListener {
private var SHIFT_PERM_TOGGLE_SPEED = 500 // how quickly do we have to doubletap shift to enable permanent caps lock
private val KEYBOARD_LETTERS = 0
private val KEYBOARD_SYMBOLS = 1
private val KEYBOARD_SYMBOLS_SHIFT = 2
private var keyboard: MyKeyboard? = null
private var keyboardView: MyKeyboardView? = null
private var lastShiftPressTS = 0L
private var keyboardMode = KEYBOARD_LETTERS
override fun onCreateInputView(): View {
keyboardView = layoutInflater.inflate(R.layout.keyboard_view_keyboard, null) as MyKeyboardView
@ -55,18 +59,40 @@ class SimpleKeyboardIME : InputMethodService(), MyKeyboardView.OnKeyboardActionL
keyboardView!!.invalidateAllKeys()
}
MyKeyboard.KEYCODE_SHIFT -> {
when {
keyboard!!.shiftState == SHIFT_ON_PERMANENT -> keyboard!!.shiftState = SHIFT_OFF
System.currentTimeMillis() - lastShiftPressTS < SHIFT_PERM_TOGGLE_SPEED -> keyboard!!.shiftState = SHIFT_ON_PERMANENT
keyboard!!.shiftState == SHIFT_ON_ONE_CHAR -> keyboard!!.shiftState = SHIFT_OFF
keyboard!!.shiftState == SHIFT_OFF -> keyboard!!.shiftState = SHIFT_ON_ONE_CHAR
}
if (keyboardMode == KEYBOARD_LETTERS) {
when {
keyboard!!.shiftState == SHIFT_ON_PERMANENT -> keyboard!!.shiftState = SHIFT_OFF
System.currentTimeMillis() - lastShiftPressTS < SHIFT_PERM_TOGGLE_SPEED -> keyboard!!.shiftState = SHIFT_ON_PERMANENT
keyboard!!.shiftState == SHIFT_ON_ONE_CHAR -> keyboard!!.shiftState = SHIFT_OFF
keyboard!!.shiftState == SHIFT_OFF -> keyboard!!.shiftState = SHIFT_ON_ONE_CHAR
}
lastShiftPressTS = System.currentTimeMillis()
lastShiftPressTS = System.currentTimeMillis()
} else {
val keyboardXml = if (keyboardMode == KEYBOARD_SYMBOLS) {
keyboardMode = KEYBOARD_SYMBOLS_SHIFT
R.xml.keys_symbols_shift
} else {
keyboardMode = KEYBOARD_SYMBOLS
R.xml.keys_symbols
}
keyboard = MyKeyboard(this, keyboardXml)
keyboardView!!.setKeyboard(keyboard!!)
}
keyboardView!!.invalidateAllKeys()
}
MyKeyboard.KEYCODE_DONE -> inputConnection.sendKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER))
MyKeyboard.KEYCODE_MODE_CHANGE -> {}
MyKeyboard.KEYCODE_MODE_CHANGE -> {
val keyboardXml = if (keyboardMode == KEYBOARD_LETTERS) {
keyboardMode = KEYBOARD_SYMBOLS
R.xml.keys_symbols
} else {
keyboardMode = KEYBOARD_LETTERS
R.xml.keys_letters
}
keyboard = MyKeyboard(this, keyboardXml)
keyboardView!!.setKeyboard(keyboard!!)
}
else -> {
var code = primaryCode.toChar()
if (Character.isLetter(code) && keyboard!!.shiftState > SHIFT_OFF) {

View File

@ -879,7 +879,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
mPopupPreviewY += popupHeight
}
if (key.label.isNotEmpty() && key.codes.firstOrNull() != KEYCODE_MODE_CHANGE) {
if (key.label.isNotEmpty() && key.codes.firstOrNull() != MyKeyboard.KEYCODE_MODE_CHANGE) {
if (previewPopup.isShowing) {
previewPopup.update(mPopupPreviewX, mPopupPreviewY, popupWidth, popupHeight)
} else {