From cb0c6eb661c7b12e56d34352e8cb6c385213964a Mon Sep 17 00:00:00 2001 From: andreas-byfut <356109509+andreas-byfut@users.noreply.github.com> Date: Fri, 13 Nov 2026 07:46:36 +0100 Subject: [PATCH] fix(terminal): send AltGr characters instead of ESC-prefixing them MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On layouts with AltGr (e.g. German AltGr+Q = @), Android reports Right Alt as Alt, so the hardware key handler sent ESC plus the unmodified base character (ESC q) instead of the composed character. Right Alt now sends the composed character as is when it changes the character. Behaviour change: this also applies to Right Alt on layouts without AltGr that still define Alt mappings, such as the AOSP Generic layout (C and S map to รง and รŸ), which now send those characters instead of ESC+letter. Right Alt keeps the ESC prefix where it composes nothing. Left Alt is unchanged and always sends ESC+base character. AltGr dead keys (no printable composed character) still fall back to ESC+base, as before. The decision is a pure function, altChordText. --- .../shellwave/terminal/TerminalInput.kt | 25 +++++++++-- .../shellwave/terminal/AltChordTextTest.kt | 44 +++++++++++++++++++ 2 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 app/src/test/java/io/github/lordofpolls/shellwave/terminal/AltChordTextTest.kt diff --git a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt index 888e5b6..71b128e 100644 --- a/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt +++ b/app/src/main/java/io/github/lordofpolls/shellwave/terminal/TerminalInput.kt @@ -185,16 +185,33 @@ fun hardwareKeyCode(event: ComposeKeyEvent, cursorApp: Boolean, keypadApp: Boole ctrlChordCode(native.getUnicodeChar(0))?.let { return it.toString() } } - // xterm/bash convention: ESC then the character, e.g. Alt+. to recall the last argument. if (alt && !ctrl) { - val codePoint = - native.getUnicodeChar(native.metaState and android.view.KeyEvent.META_SHIFT_ON) - if (codePoint > 0) return "\u001B" + String(Character.toChars(codePoint)) + val shift = native.metaState and android.view.KeyEvent.META_SHIFT_ON + altChordText( + rightAlt = native.metaState and android.view.KeyEvent.META_ALT_RIGHT_ON != 0, + composed = native.getUnicodeChar(native.metaState), + base = native.getUnicodeChar(shift), + )?.let { return it } } return null } +/** + * Text for a key pressed with Alt held. Left Alt follows the xterm/bash convention: ESC then the + * base character, e.g. Alt+. to recall the last argument. + * + * Right Alt is AltGr on layouts that have one (German AltGr+Q = @). Android reports it as Alt too, + * so it is told apart by the character it composes: when Right Alt changes the character, that + * composed character is sent as is, without the ESC prefix. [composed] is the code point with the + * full meta state applied, [base] the one with only Shift applied. Null when neither is printable. + */ +internal fun altChordText(rightAlt: Boolean, composed: Int, base: Int): String? { + if (rightAlt && composed > 0 && composed != base) return String(Character.toChars(composed)) + if (base > 0) return "\u001B" + String(Character.toChars(base)) + return null +} + /** Ported from upstream `TerminalView.inputCodePoint`. */ private fun ctrlChordCode(codePoint: Int): Char? { if (codePoint in 0..0xFFFF) { diff --git a/app/src/test/java/io/github/lordofpolls/shellwave/terminal/AltChordTextTest.kt b/app/src/test/java/io/github/lordofpolls/shellwave/terminal/AltChordTextTest.kt new file mode 100644 index 0000000..86c2213 --- /dev/null +++ b/app/src/test/java/io/github/lordofpolls/shellwave/terminal/AltChordTextTest.kt @@ -0,0 +1,44 @@ +package io.github.lordofpolls.shellwave.terminal + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Test + +class AltChordTextTest { + @Test + fun leftAlt_sendsEscapePrefixedBaseCharacter() { + assertEquals("\u001B.", altChordText(rightAlt = false, composed = '.'.code, base = '.'.code)) + } + + @Test + fun leftAlt_ignoresComposedCharacter() { + assertEquals("\u001Bq", altChordText(rightAlt = false, composed = '@'.code, base = 'q'.code)) + } + + @Test + fun altGr_sendsComposedCharacterWithoutEscape() { + assertEquals("@", altChordText(rightAlt = true, composed = '@'.code, base = 'q'.code)) + assertEquals("[", altChordText(rightAlt = true, composed = '['.code, base = '8'.code)) + } + + @Test + fun rightAlt_withoutComposedCharacter_actsAsMeta() { + assertEquals("\u001Bx", altChordText(rightAlt = true, composed = 'x'.code, base = 'x'.code)) + } + + @Test + fun rightAlt_noMapping_zeroComposedCharacter_actsAsMeta() { + assertEquals("\u001Bx", altChordText(rightAlt = true, composed = 0, base = 'x'.code)) + } + + @Test + fun altGr_supplementaryCodePoint() { + assertEquals("๐Ÿ˜€", altChordText(rightAlt = true, composed = 0x1F600, base = 'e'.code)) + } + + @Test + fun unprintableKey_isNull() { + assertNull(altChordText(rightAlt = false, composed = 0, base = 0)) + assertNull(altChordText(rightAlt = true, composed = 0, base = 0)) + } +}