Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

fix: AltGr characters and Ctrl+C, Ctrl+X, Ctrl+A reach the phone

Bee1968f95d8a5b
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt
index 674116b..1ebec89 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt
@@ -739,8 +739,12 @@ object Elevated {
"$action ${event.x * w} ${event.y * h}"
}
is MirrorInputEvent.Scroll -> "S ${event.x * w} ${event.y * h} ${event.dx} ${event.dy}"
- is MirrorInputEvent.Key -> keys[event.key]?.let { "K $it" }
- ?: if (event.key == "notifications" && display == null) "C cmd statusbar expand-notifications" else return true
+ is MirrorInputEvent.Key -> keys[event.key]?.let { "K $it" } ?: when {
+ // No key code selects all: Ctrl+A, which text fields take as a shortcut.
+ event.key == "select_all" -> "Y ${KeyEvent.KEYCODE_A} ${KeyEvent.META_CTRL_ON or KeyEvent.META_CTRL_LEFT_ON}"
+ event.key == "notifications" && display == null -> "C cmd statusbar expand-notifications"
+ else -> return true
+ }
// A key typed on the PC types; anything longer (a paste, a word from
// an input method, an emoji) goes on the clipboard, quietly, and is
// pasted at once: typing it out took a moment per letter, and a line
@@ -914,6 +918,9 @@ object Elevated {
"up" to KeyEvent.KEYCODE_DPAD_UP,
"down" to KeyEvent.KEYCODE_DPAD_DOWN,
"tab" to KeyEvent.KEYCODE_TAB,
+ // Text fields copy and cut on these, as on a keyboard's Copy and Cut keys.
+ "copy" to KeyEvent.KEYCODE_COPY,
+ "cut" to KeyEvent.KEYCODE_CUT,
)
/** The screen's size in pixels, as it's turned now. */
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt
index 3f5a38a..e9305f4 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt
@@ -559,6 +559,10 @@ object InputServer {
scroll(x, y, dx, dy)
}
"K" -> key(rest.toInt())
+ "Y" -> {
+ val (code, meta) = rest.split(' ').map { it.toInt() }
+ shortcut(code, meta)
+ }
"T" -> type(rest)
"G" -> gamepad(rest)
"C" -> if (allowedCommand(rest) && display < 0) {
@@ -623,6 +627,18 @@ object InputServer {
}
}
+ /**
+ * A key with modifiers (`Y <code> <meta>`), Ctrl+A say. Injected, since
+ * the virtual HID keyboard sends no modifiers. A helper from before
+ * this ignores the line.
+ */
+ private fun shortcut(code: Int, meta: Int) {
+ val now = SystemClock.uptimeMillis()
+ for (action in intArrayOf(KeyEvent.ACTION_DOWN, KeyEvent.ACTION_UP)) {
+ send(KeyEvent(now, now, action, code, 0, meta, KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD))
+ }
+ }
+
private fun type(text: String) {
val map = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD)
// What the keyboard map can't type (most non-Latin letters, emoji) is skipped.
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/mirror/InputService.kt b/android/app/src/main/kotlin/app/nectarlink/android/mirror/InputService.kt
index 4d5856c..72f8aa9 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/mirror/InputService.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/mirror/InputService.kt
@@ -263,6 +263,23 @@ class InputService : AccessibilityService() {
Edit(text, if (nextNewline >= 0) (nextNewline + 1).coerceAtMost(text.length) else text.length)
}
"tab" -> focused()?.focusSearch(android.view.View.FOCUS_FORWARD)?.performAction(AccessibilityNodeInfo.ACTION_FOCUS)
+ "copy" -> focused()?.performAction(AccessibilityNodeInfo.ACTION_COPY)
+ "cut" -> focused()?.let { node ->
+ pendingText = null
+ node.performAction(AccessibilityNodeInfo.ACTION_CUT)
+ }
+ "select_all" -> focused()?.takeIf { isNodeEditable(it) }?.let { node ->
+ runCatching { node.refresh() }
+ val length = if (node.isShowingHintText) 0 else node.text?.length ?: 0
+ pendingText = null
+ node.performAction(
+ AccessibilityNodeInfo.ACTION_SET_SELECTION,
+ android.os.Bundle().apply {
+ putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT, 0)
+ putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT, length)
+ },
+ )
+ }
}
}
diff --git a/core/nectarlink-protocol/src/messages.rs b/core/nectarlink-protocol/src/messages.rs
index 8a7cc1f..93395cb 100644
--- a/core/nectarlink-protocol/src/messages.rs
+++ b/core/nectarlink-protocol/src/messages.rs
@@ -1529,6 +1529,11 @@ pub mod mirror_keys {
pub const NOTIFICATIONS: &str = "notifications";
pub const KEYBOARD_ON: &str = "keyboard_on";
pub const KEYBOARD_OFF: &str = "keyboard_off";
+ /// Copy, cut and select all in the focused text field (the PC's Ctrl+C,
+ /// Ctrl+X and Ctrl+A). Phones from before these drop them.
+ pub const COPY: &str = "copy";
+ pub const CUT: &str = "cut";
+ pub const SELECT_ALL: &str = "select_all";
pub const ALL: &[&str] = &[
BACK,
HOME,
@@ -1544,6 +1549,9 @@ pub mod mirror_keys {
NOTIFICATIONS,
KEYBOARD_ON,
KEYBOARD_OFF,
+ COPY,
+ CUT,
+ SELECT_ALL,
];
}
@@ -4843,6 +4851,9 @@ mod tests {
assert!(!MirrorInput::Touch { action: TouchAction::Up, x: f32::NAN, y: 0.0 }.is_valid());
assert!(MirrorInput::Key { key: "back".into() }.is_valid());
assert!(!MirrorInput::Key { key: "power".into() }.is_valid());
+ for key in [mirror_keys::COPY, mirror_keys::CUT, mirror_keys::SELECT_ALL] {
+ assert!(MirrorInput::Key { key: key.into() }.is_valid());
+ }
assert!(!MirrorInput::Text { text: String::new() }.is_valid());
assert!(!format!("{:?}", MirrorInput::Text { text: "secret".into() }).contains("secret"));
}
diff --git a/desktop/app/qml/MirrorWindow.qml b/desktop/app/qml/MirrorWindow.qml
index a5eb42a..4a17991 100644
--- a/desktop/app/qml/MirrorWindow.qml
+++ b/desktop/app/qml/MirrorWindow.qml
@@ -245,14 +245,27 @@ Window {
keys[Qt.Key_Up] = "up"
keys[Qt.Key_Down] = "down"
keys[Qt.Key_Tab] = "tab"
+ // Copy, cut and select all on the phone; what's copied there
+ // comes back with the clipboard.
+ const edits = {}
+ edits[Qt.Key_C] = "copy"
+ edits[Qt.Key_X] = "cut"
+ edits[Qt.Key_A] = "select_all"
+ const ctrlOnly = (event.modifiers & (Qt.ControlModifier | Qt.AltModifier | Qt.ShiftModifier | Qt.MetaModifier))
+ === Qt.ControlModifier
if ((event.modifiers & Qt.ControlModifier) && event.key === Qt.Key_V) {
Mirror.paste(win.mirrorKey)
event.accepted = true
+ } else if (ctrlOnly && edits[event.key] !== undefined) {
+ Mirror.press(win.mirrorKey, edits[event.key])
+ event.accepted = true
} else if (keys[event.key] !== undefined) {
Mirror.press(win.mirrorKey, keys[event.key])
event.accepted = true
- } else if (event.text.length > 0 && !(event.modifiers & (Qt.ControlModifier | Qt.AltModifier))
- && event.text.charCodeAt(0) >= 32) {
+ } else if (event.text.length > 0 && event.text.charCodeAt(0) >= 32
+ && (!(event.modifiers & (Qt.ControlModifier | Qt.AltModifier | Qt.MetaModifier))
+ // AltGr (@ on a French keyboard, say) arrives as Ctrl+Alt on Windows.
+ || ((event.modifiers & Qt.ControlModifier) && (event.modifiers & Qt.AltModifier)))) {
Mirror.text(win.mirrorKey, event.text)
event.accepted = true
}
diff --git a/desktop/app/qml/pages/HomePage.qml b/desktop/app/qml/pages/HomePage.qml
index cb51bd6..eb663f9 100644
--- a/desktop/app/qml/pages/HomePage.qml
+++ b/desktop/app/qml/pages/HomePage.qml
@@ -2426,8 +2426,18 @@ Item {
event.accepted = true
return
}
- if (event.text && event.text.length > 0
- && !(event.modifiers & (Qt.ControlModifier | Qt.AltModifier | Qt.MetaModifier))) {
+ const edit = (event.modifiers & (Qt.ControlModifier | Qt.AltModifier | Qt.ShiftModifier | Qt.MetaModifier))
+ === Qt.ControlModifier
+ ? ({ [Qt.Key_C]: "copy", [Qt.Key_X]: "cut", [Qt.Key_A]: "select_all" })[event.key] : undefined
+ if (edit !== undefined) {
+ Mirror.keyboardPress(home.deviceId, edit)
+ event.accepted = true
+ return
+ }
+ if (event.text && event.text.length > 0 && event.text.charCodeAt(0) >= 32
+ && (!(event.modifiers & (Qt.ControlModifier | Qt.AltModifier | Qt.MetaModifier))
+ // AltGr (@ on a French keyboard, say) arrives as Ctrl+Alt on Windows.
+ || ((event.modifiers & Qt.ControlModifier) && (event.modifiers & Qt.AltModifier)))) {
Mirror.keyboardText(home.deviceId, event.text)
typeOnPhoneCard.lastSent = event.text
event.accepted = true
diff --git a/docs/protocol/mirror.md b/docs/protocol/mirror.md
index e53519d..42bf822 100644
--- a/docs/protocol/mirror.md
+++ b/docs/protocol/mirror.md
@@ -129,8 +129,10 @@ bottom), so they hold at any size and rotation.
- `scroll`: the mouse wheel at a point, in notches (positive: down or
right); the phone swipes the other way.
- `key`: `back`, `home`, `recents`, `notifications`, `enter`, `backspace`,
- `delete`, `left`, `right`, `up`, `down`, `tab`, `keyboard_on` or
- `keyboard_off`. Others are dropped. `keyboard_on` and `keyboard_off` mark
+ `delete`, `left`, `right`, `up`, `down`, `tab`, `copy`, `cut`,
+ `select_all`, `keyboard_on` or `keyboard_off`. Others are dropped (so
+ phones from before `copy`, `cut` and `select_all` ignore them). `copy`,
+ `cut` and `select_all` act on the focused text field. `keyboard_on` and `keyboard_off` mark
when a PC starts or stops typing on the phone (`session` 0) without
opening a video stream; the phone shows a banner with a button to stop it
(which sends `mirror.stop` for `session` 0 back to the PC).