allow storing only 1 key code into every key

This commit is contained in:
tibbi 2022-01-22 09:48:02 +01:00
parent 430fdb5881
commit f671f2653c
2 changed files with 25 additions and 28 deletions

View File

@ -140,8 +140,8 @@ class MyKeyboard {
* @attr ref android.R.styleable#Keyboard_Key_keyEdgeFlags * @attr ref android.R.styleable#Keyboard_Key_keyEdgeFlags
*/ */
class Key(parent: Row) { class Key(parent: Row) {
/** All the key codes (unicode or custom code) that this key could generate, zero'th being the most important. */ /** Key code that this key generates. */
var codes = ArrayList<Int>() var code = 0
/** Label to display */ /** Label to display */
var label: CharSequence = "" var label: CharSequence = ""
@ -211,7 +211,7 @@ class MyKeyboard {
a.recycle() a.recycle()
a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.MyKeyboard_Key) a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.MyKeyboard_Key)
codes = arrayListOf(a.getInt(R.styleable.MyKeyboard_Key_codes, 0)) code = a.getInt(R.styleable.MyKeyboard_Key_codes, 0)
popupCharacters = a.getText(R.styleable.MyKeyboard_Key_popupCharacters) popupCharacters = a.getText(R.styleable.MyKeyboard_Key_popupCharacters)
popupResId = a.getResourceId(R.styleable.MyKeyboard_Key_popupKeyboard, 0) popupResId = a.getResourceId(R.styleable.MyKeyboard_Key_popupKeyboard, 0)
@ -223,8 +223,8 @@ class MyKeyboard {
label = a.getText(R.styleable.MyKeyboard_Key_keyLabel) ?: "" label = a.getText(R.styleable.MyKeyboard_Key_keyLabel) ?: ""
topSmallNumber = a.getString(R.styleable.MyKeyboard_Key_topSmallNumber) ?: "" topSmallNumber = a.getString(R.styleable.MyKeyboard_Key_topSmallNumber) ?: ""
if (label.isNotEmpty() && codes.firstOrNull() != KEYCODE_MODE_CHANGE && codes.firstOrNull() != KEYCODE_SHIFT) { if (label.isNotEmpty() && code != KEYCODE_MODE_CHANGE && code != KEYCODE_SHIFT) {
codes = arrayListOf(label[0].toInt()) code = label[0].toInt()
} }
a.recycle() a.recycle()
} }
@ -329,7 +329,7 @@ class MyKeyboard {
key.x = x key.x = x
key.y = y key.y = y
key.label = character.toString() key.label = character.toString()
key.codes = arrayListOf(character.toInt()) key.code = character.toInt()
column++ column++
x += key.width + key.gap x += key.width + key.gap
mKeys!!.add(key) mKeys!!.add(key)
@ -439,7 +439,7 @@ class MyKeyboard {
inKey = true inKey = true
key = createKeyFromXml(res, currentRow!!, x, y, parser) key = createKeyFromXml(res, currentRow!!, x, y, parser)
mKeys!!.add(key) mKeys!!.add(key)
if (key.codes[0] == KEYCODE_ENTER) { if (key.code == KEYCODE_ENTER) {
val enterResourceId = when (mEnterKeyType) { val enterResourceId = when (mEnterKeyType) {
EditorInfo.IME_ACTION_SEARCH -> R.drawable.ic_search_vector EditorInfo.IME_ACTION_SEARCH -> R.drawable.ic_search_vector
EditorInfo.IME_ACTION_NEXT, EditorInfo.IME_ACTION_GO -> R.drawable.ic_arrow_right_vector EditorInfo.IME_ACTION_NEXT, EditorInfo.IME_ACTION_GO -> R.drawable.ic_arrow_right_vector

View File

@ -402,7 +402,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
val keyCount = keys.size val keyCount = keys.size
for (i in 0 until keyCount) { for (i in 0 until keyCount) {
val key = keys[i] val key = keys[i]
val code = key.codes.firstOrNull() ?: GENERIC_KEY val code = key.code
var keyBackground = mKeyBackground var keyBackground = mKeyBackground
if (code == KEYCODE_SPACE) { if (code == KEYCODE_SPACE) {
keyBackground = resources.getDrawable(R.drawable.keyboard_space_background, context.theme) keyBackground = resources.getDrawable(R.drawable.keyboard_space_background, context.theme)
@ -456,7 +456,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
// Turn off drop shadow // Turn off drop shadow
paint.setShadowLayer(0f, 0f, 0f, 0) paint.setShadowLayer(0f, 0f, 0f, 0)
} else if (key.icon != null && mKeyboard != null) { } else if (key.icon != null && mKeyboard != null) {
if (key.codes.contains(KEYCODE_SHIFT)) { if (code == KEYCODE_SHIFT) {
val drawableId = when (mKeyboard!!.mShiftState) { val drawableId = when (mKeyboard!!.mShiftState) {
SHIFT_OFF -> R.drawable.ic_caps_outline_vector SHIFT_OFF -> R.drawable.ic_caps_outline_vector
SHIFT_ON_ONE_CHAR -> R.drawable.ic_caps_vector SHIFT_ON_ONE_CHAR -> R.drawable.ic_caps_vector
@ -509,7 +509,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
primaryIndex = nearestKeyIndices[i] primaryIndex = nearestKeyIndices[i]
} }
if (isInside && key.codes[0] > KEYCODE_SPACE) { if (isInside && key.code > KEYCODE_SPACE) {
// Find insertion point // Find insertion point
val nCodes = 1 val nCodes = 1
if (dist < closestKeyDist) { if (dist < closestKeyDist) {
@ -528,15 +528,14 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
mDistances, j, mDistances, j + nCodes, mDistances, j, mDistances, j + nCodes,
mDistances.size - j - nCodes mDistances.size - j - nCodes
) )
System.arraycopy( System.arraycopy(
allKeys, j, allKeys, j + nCodes, allKeys, j, allKeys, j + nCodes,
allKeys.size - j - nCodes allKeys.size - j - nCodes
) )
for (c in 0 until nCodes) { allKeys[j] = key.code
allKeys[j + c] = key.codes[c] mDistances[j] = dist
mDistances[j + c] = dist
}
break break
} }
} }
@ -553,11 +552,10 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private fun detectAndSendKey(index: Int, x: Int, y: Int, eventTime: Long) { private fun detectAndSendKey(index: Int, x: Int, y: Int, eventTime: Long) {
if (index != NOT_A_KEY && index < mKeys.size) { if (index != NOT_A_KEY && index < mKeys.size) {
val key = mKeys[index] val key = mKeys[index]
val code = key.codes[0]
val codes = IntArray(MAX_NEARBY_KEYS) val codes = IntArray(MAX_NEARBY_KEYS)
Arrays.fill(codes, NOT_A_KEY) Arrays.fill(codes, NOT_A_KEY)
getKeyIndices(x, y, codes) getKeyIndices(x, y, codes)
mOnKeyboardActionListener!!.onKey(code, codes) mOnKeyboardActionListener!!.onKey(key.code, codes)
mLastTapTime = eventTime mLastTapTime = eventTime
} }
} }
@ -573,7 +571,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
val oldKey = keys[oldKeyIndex] val oldKey = keys[oldKeyIndex]
oldKey.pressed = false oldKey.pressed = false
invalidateKey(oldKeyIndex) invalidateKey(oldKeyIndex)
val keyCode = oldKey.codes[0] val keyCode = oldKey.code
sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT, keyCode) sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT, keyCode)
sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED, keyCode) sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUS_CLEARED, keyCode)
} }
@ -581,15 +579,14 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
if (mCurrentKeyIndex != NOT_A_KEY && keys.size > mCurrentKeyIndex) { if (mCurrentKeyIndex != NOT_A_KEY && keys.size > mCurrentKeyIndex) {
val newKey = keys[mCurrentKeyIndex] val newKey = keys[mCurrentKeyIndex]
val code = newKey.codes.firstOrNull() ?: GENERIC_KEY val code = newKey.code
if (code == KEYCODE_SHIFT || code == KEYCODE_MODE_CHANGE || code == KEYCODE_DELETE || code == KEYCODE_ENTER || code == KEYCODE_SPACE) { if (code == KEYCODE_SHIFT || code == KEYCODE_MODE_CHANGE || code == KEYCODE_DELETE || code == KEYCODE_ENTER || code == KEYCODE_SPACE) {
newKey.pressed = true newKey.pressed = true
} }
invalidateKey(mCurrentKeyIndex) invalidateKey(mCurrentKeyIndex)
val keyCode = newKey.codes[0] sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER, code)
sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER, keyCode) sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED, code)
sendAccessibilityEventForUnicodeCharacter(AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED, keyCode)
} }
} }
@ -636,7 +633,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
} catch (ignored: Exception) { } catch (ignored: Exception) {
} }
if (key.label.length > 1 && key.codes.size < 2) { if (key.label.length > 1) {
mPreviewText!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize.toFloat()) mPreviewText!!.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize.toFloat())
mPreviewText!!.typeface = Typeface.DEFAULT_BOLD mPreviewText!!.typeface = Typeface.DEFAULT_BOLD
} else { } else {
@ -685,7 +682,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
mPopupPreviewY += popupHeight mPopupPreviewY += popupHeight
} }
if (key.label.isNotEmpty() && key.codes.firstOrNull() != KEYCODE_MODE_CHANGE && key.codes.firstOrNull() != KEYCODE_SHIFT) { if (key.label.isNotEmpty() && key.code != KEYCODE_MODE_CHANGE && key.code != KEYCODE_SHIFT) {
if (previewPopup.isShowing) { if (previewPopup.isShowing) {
previewPopup.update(mPopupPreviewX, mPopupPreviewY, popupWidth, popupHeight) previewPopup.update(mPopupPreviewX, mPopupPreviewY, popupWidth, popupHeight)
} else { } else {
@ -965,7 +962,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
} }
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
mMiniKeyboard?.mKeys?.firstOrNull { it.focused }?.apply { mMiniKeyboard?.mKeys?.firstOrNull { it.focused }?.apply {
mOnKeyboardActionListener!!.onKey(codes[0], codes.toIntArray()) mOnKeyboardActionListener!!.onKey(code, intArrayOf(code))
} }
mMiniKeyboardSelectedKeyIndex = -1 mMiniKeyboardSelectedKeyIndex = -1
dismissPopupKeyboard() dismissPopupKeyboard()
@ -1011,7 +1008,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
mLastMoveTime = mDownTime mLastMoveTime = mDownTime
val onPressKey = if (keyIndex != NOT_A_KEY) { val onPressKey = if (keyIndex != NOT_A_KEY) {
mKeys[keyIndex].codes[0] mKeys[keyIndex].code
} else { } else {
0 0
} }
@ -1025,7 +1022,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
val msg = mHandler!!.obtainMessage(MSG_REPEAT) val msg = mHandler!!.obtainMessage(MSG_REPEAT)
mHandler!!.sendMessageDelayed(msg, REPEAT_START_DELAY.toLong()) mHandler!!.sendMessageDelayed(msg, REPEAT_START_DELAY.toLong())
// if the user long presses Space, move the cursor after swipine left/right // if the user long presses Space, move the cursor after swipine left/right
if (mKeys[mCurrentKey].codes.firstOrNull() == KEYCODE_SPACE) { if (mKeys[mCurrentKey].code == KEYCODE_SPACE) {
mLastSpaceMoveX = -1 mLastSpaceMoveX = -1
} else { } else {
repeatKey(true) repeatKey(true)
@ -1125,7 +1122,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
detectAndSendKey(mCurrentKey, touchX, touchY, eventTime) detectAndSendKey(mCurrentKey, touchX, touchY, eventTime)
} }
if (mKeys.getOrNull(mCurrentKey)?.codes?.firstOrNull() == KEYCODE_SPACE && !mIsLongPressingSpace) { if (mKeys.getOrNull(mCurrentKey)?.code == KEYCODE_SPACE && !mIsLongPressingSpace) {
detectAndSendKey(mCurrentKey, touchX, touchY, eventTime) detectAndSendKey(mCurrentKey, touchX, touchY, eventTime)
} }
@ -1152,7 +1149,7 @@ class MyKeyboardView @JvmOverloads constructor(context: Context, attrs: Attribut
private fun repeatKey(initialCall: Boolean): Boolean { private fun repeatKey(initialCall: Boolean): Boolean {
val key = mKeys[mRepeatKeyIndex] val key = mKeys[mRepeatKeyIndex]
if (!initialCall && key.codes.firstOrNull() == KEYCODE_SPACE) { if (!initialCall && key.code == KEYCODE_SPACE) {
mIsLongPressingSpace = true mIsLongPressingSpace = true
} else { } else {
detectAndSendKey(mCurrentKey, key.x, key.y, mLastTapTime) detectAndSendKey(mCurrentKey, key.x, key.y, mLastTapTime)