Skip to main content

agentmux_srv/server/streamed_browser/screen/
held.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The modifier keys the person is holding down on the screen. A paste is typed key by key; with Control held (the paste itself is Ctrl+V) every
5//! letter would be a shortcut (`a` selects all, `w` closes the tab). So while text is typed the held modifiers are let go, and pressed again after.
6
7use super::keymap;
8
9/// The keys that change what another key means.
10const MODIFIERS: [&str; 8] = ["ShiftLeft", "ShiftRight", "ControlLeft", "ControlRight", "AltLeft", "AltRight", "MetaLeft", "MetaRight"];
11
12#[derive(Default)]
13pub struct Held(Vec<u8>);
14
15impl Held {
16    /// A key went down or up: remember it when it is a modifier.
17    pub fn note(&mut self, code: &str, down: bool) {
18        let Some(k) = keymap::keycode(code).filter(|_| MODIFIERS.contains(&code)) else { return };
19        self.0.retain(|held| *held != k);
20        if down {
21            self.0.push(k);
22        }
23    }
24
25    /// The modifier keys down now.
26    pub fn keys(&self) -> &[u8] {
27        &self.0
28    }
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34
35    #[test]
36    fn only_modifiers_are_remembered_and_a_release_forgets_them() {
37        let mut h = Held::default();
38        h.note("ControlLeft", true);
39        h.note("KeyV", true);
40        h.note("ShiftLeft", true);
41        assert_eq!(h.keys(), &[keymap::keycode("ControlLeft").unwrap(), keymap::keycode("ShiftLeft").unwrap()]);
42        h.note("ControlLeft", false);
43        assert_eq!(h.keys(), &[keymap::keycode("ShiftLeft").unwrap()]);
44        h.note("ControlLeft", true);
45        h.note("ControlLeft", true);
46        assert_eq!(h.keys().len(), 2, "a key pressed twice is held once");
47    }
48}