Skip to content

Commit

Permalink
Merge pull request #451 from florisboard/alternate-shift-code
Browse files Browse the repository at this point in the history
Add option for an alternate key code when caps state is active
  • Loading branch information
patrickgold committed Mar 10, 2021
2 parents 62fc549 + 15f0316 commit e4f5fcf
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/src/main/assets/ime/text/characters/colemak.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"relevant": [
{ "code": 58, "label": ":" }
]
} }
}, "shift": { "code": 58, "label": ":" } }
],
[
{ "code": 97, "label": "a" },
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/assets/ime/text/characters/dvorak.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@
{ "code": 33, "label": "!" },
{ "code": 34, "label": "\"" }
]
} },
}, "shift": { "code": 34, "label": "\"" } },
{ "code": 39, "label": "'", "groupId": 101, "variation": "password", "popup": {
"relevant": [
{ "code": 33, "label": "!" },
{ "code": 34, "label": "\"" }
]
} },
}, "shift": { "code": 34, "label": "\"" } },
{ "code": 47, "label": "/", "groupId": 101, "variation": "uri" },
{ "code": 44, "label": ",", "popup": {
"relevant": [
{ "code": 60, "label": "<" },
{ "code": 63, "label": "?" }
]
} },
}, "shift": { "code": 60, "label": "<" } },
{ "code": 46, "label": ".", "popup": {
"relevant": [
{ "code": 62, "label": ">" }
]
} },
}, "shift": { "code": 62, "label": ">" } },
{ "code": 112, "label": "p" },
{ "code": 121, "label": "y" },
{ "code": 102, "label": "f" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,16 @@ class TextInputManager private constructor() : CoroutineScope by MainScope(), In
}
}

/**
* Adjusts a given key data for caps state and returns the correct reference.
*/
private fun getAdjustedKeyData(keyData: KeyData): KeyData {
return if (caps && keyData is FlorisKeyData && keyData.shift != null) { keyData.shift!! } else { keyData }
}

override fun onInputKeyDown(ev: InputKeyEvent) {
when (ev.data.code) {
val data = getAdjustedKeyData(ev.data)
when (data.code) {
KeyCode.INTERNAL_BATCH_EDIT -> {
florisboard.beginInternalBatchEdit()
return
Expand All @@ -665,7 +673,8 @@ class TextInputManager private constructor() : CoroutineScope by MainScope(), In
}

override fun onInputKeyUp(ev: InputKeyEvent) {
when (ev.data.code) {
val data = getAdjustedKeyData(ev.data)
when (data.code) {
KeyCode.ARROW_DOWN,
KeyCode.ARROW_LEFT,
KeyCode.ARROW_RIGHT,
Expand All @@ -674,9 +683,9 @@ class TextInputManager private constructor() : CoroutineScope by MainScope(), In
KeyCode.MOVE_END_OF_PAGE,
KeyCode.MOVE_START_OF_LINE,
KeyCode.MOVE_END_OF_LINE -> if (ev.action == InputKeyEvent.Action.DOWN_UP || ev.action == InputKeyEvent.Action.REPEAT) {
handleArrow(ev.data.code, ev.count)
handleArrow(data.code, ev.count)
} else {
handleArrow(ev.data.code, 1)
handleArrow(data.code, 1)
}
KeyCode.CLIPBOARD_CUT -> activeEditorInstance.performClipboardCut()
KeyCode.CLIPBOARD_COPY -> activeEditorInstance.performClipboardCopy()
Expand Down Expand Up @@ -727,29 +736,29 @@ class TextInputManager private constructor() : CoroutineScope by MainScope(), In
KeyboardMode.NUMERIC,
KeyboardMode.NUMERIC_ADVANCED,
KeyboardMode.PHONE,
KeyboardMode.PHONE2 -> when (ev.data.type) {
KeyboardMode.PHONE2 -> when (data.type) {
KeyType.CHARACTER,
KeyType.NUMERIC -> {
val text = ev.data.code.toChar().toString()
val text = data.code.toChar().toString()
activeEditorInstance.commitText(text)
}
else -> when (ev.data.code) {
else -> when (data.code) {
KeyCode.PHONE_PAUSE,
KeyCode.PHONE_WAIT -> {
val text = ev.data.code.toChar().toString()
val text = data.code.toChar().toString()
activeEditorInstance.commitText(text)
}
}
}
else -> when (ev.data.type) {
KeyType.CHARACTER, KeyType.NUMERIC -> when (ev.data.code) {
else -> when (data.type) {
KeyType.CHARACTER, KeyType.NUMERIC -> when (data.code) {
KeyCode.SPACE -> handleSpace(ev)
KeyCode.URI_COMPONENT_TLD -> {
val tld = ev.data.label.toLowerCase(Locale.ENGLISH)
val tld = data.label.toLowerCase(Locale.ENGLISH)
activeEditorInstance.commitText(tld)
}
else -> {
var text = ev.data.code.toChar().toString()
var text = data.code.toChar().toString()
text = when (caps && activeKeyboardMode == KeyboardMode.CHARACTERS) {
true -> text.toUpperCase(florisboard.activeSubtype.locale)
false -> text
Expand All @@ -758,14 +767,14 @@ class TextInputManager private constructor() : CoroutineScope by MainScope(), In
}
}
else -> {
Timber.e("sendKeyPress(keyData): Received unknown key: ${ev.data}")
Timber.e("sendKeyPress(keyData): Received unknown key: $data")
}
}
}
smartbarView?.resetClipboardSuggestion()
}
}
if (ev.data.code != KeyCode.SHIFT && !capsLock && !inputEventDispatcher.isPressed(KeyCode.SHIFT)) {
if (data.code != KeyCode.SHIFT && !capsLock && !inputEventDispatcher.isPressed(KeyCode.SHIFT)) {
updateCapsState()
}
smartbarView?.updateSmartbarState()
Expand All @@ -776,7 +785,8 @@ class TextInputManager private constructor() : CoroutineScope by MainScope(), In
}

override fun onInputKeyCancel(ev: InputKeyEvent) {
when (ev.data.code) {
val data = getAdjustedKeyData(ev.data)
when (data.code) {
KeyCode.SHIFT -> handleShiftCancel()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,19 @@ open class KeyData(
* or if the key should always be visible. Defaults to [KeyVariation.ALL].
* @property popup List of keys which will be accessible while long pressing the key. Defaults to
* an empty set (no extended popup).
* @property shift An alternative key to use when the keyboard caps state is true. Useful for layouts
* such as Colemak and Dvorak. Defaults to null (don't override base uppercase key). This override
* property should only be used to provide an uppercase variant of two else not related variants, but
* should not be used for providing an uppercase letter (e.g. 'a' -> 'A').
*/
class FlorisKeyData(
type: KeyType = KeyType.CHARACTER,
code: Int = 0,
label: String = "",
var groupId: Int = GROUP_DEFAULT,
var variation: KeyVariation = KeyVariation.ALL,
var popup: PopupSet<KeyData> = PopupSet()
var popup: PopupSet<KeyData> = PopupSet(),
var shift: KeyData? = null
) : KeyData(type, code, label) {
companion object {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,21 @@ class KeyView(
*/
fun getComputedLetter(
keyData: KeyData = data,
caps: Boolean = florisboard?.textInputManager?.caps ?: false && florisboard?.textInputManager?.getActiveKeyboardMode() == KeyboardMode.CHARACTERS,
caps: Boolean = florisboard?.textInputManager?.caps ?: false,
subtype: Subtype = florisboard?.activeSubtype ?: Subtype.DEFAULT
): String {
return when (data.code) {
KeyCode.URI_COMPONENT_TLD -> keyData.label.toLowerCase(Locale.ENGLISH)
else -> {
val labelText = (keyData.code.toChar()).toString()
if (caps) {
labelText.toUpperCase(subtype.locale)
} else {
labelText
return if (caps && keyData is FlorisKeyData && keyData.shift != null) {
(keyData.shift!!.code.toChar()).toString()
} else {
when (data.code) {
KeyCode.URI_COMPONENT_TLD -> keyData.label.toLowerCase(Locale.ENGLISH)
else -> {
val labelText = (keyData.code.toChar()).toString()
if (caps) {
labelText.toUpperCase(subtype.locale)
} else {
labelText
}
}
}
}
Expand Down

0 comments on commit e4f5fcf

Please sign in to comment.