Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

Browser takeover: typing works on a Mac's Chrome (#293)

Oleksandr Ivanchenko54d7771f0ac8
diff --git a/src/server/browser_input.rs b/src/server/browser_input.rs
index c26eaaf2..43b62cbb 100644
--- a/src/server/browser_input.rs
+++ b/src/server/browser_input.rs
@@ -197,8 +197,11 @@ pub fn key_params(event: &KeyEvent<'_>) -> Result<Value, &'static str> {
"type": kind,
"key": event.key,
"code": event.code,
+ // No `nativeVirtualKeyCode`: Chrome reads it as the OS's own key number, and on a Mac
+ // builds a real key event from it. A Windows number there is a different Mac key (65,
+ // "A", is the keypad's "."), so nothing typed on a Mac Chrome. Puppeteer and Playwright
+ // leave it out too.
"windowsVirtualKeyCode": vk,
- "nativeVirtualKeyCode": vk,
"modifiers": event.modifiers,
"location": location,
"autoRepeat": event.auto_repeat,
@@ -464,6 +467,18 @@ mod tests {
assert_eq!(auto["autoRepeat"], true);
}
+ #[test]
+ fn keys_never_carry_a_native_key_code() {
+ // Chrome on a Mac turns nativeVirtualKeyCode into a real Mac key event, and a Windows
+ // number there is another key: typing did nothing in the Mac app
+ for (key, code, mods) in [("a", "KeyA", 0), ("Backspace", "Backspace", 0), ("Enter", "Enter", 0), ("a", "KeyA", CTRL), ("1", "Numpad1", 0)] {
+ let p = key_params(&down(key, code, mods)).unwrap();
+ assert!(p.get("nativeVirtualKeyCode").is_none(), "{code}");
+ let up = key_params(&KeyEvent { kind: KeyKind::Up, ..down(key, code, mods) }).unwrap();
+ assert!(up.get("nativeVirtualKeyCode").is_none(), "{code} up");
+ }
+ }
+
#[test]
fn shortcuts_carry_no_text_and_an_editing_command() {
let p = key_params(&down("a", "KeyA", CTRL)).unwrap();
diff --git a/src/server/browser_stream.rs b/src/server/browser_stream.rs
index b4735273..48e12a2f 100644
--- a/src/server/browser_stream.rs
+++ b/src/server/browser_stream.rs
@@ -886,6 +886,10 @@ async fn stream(
let mut lease: Option<OwnedRwLockWriteGuard<()>> = None;
let result = async {
cdp.call("Page.enable", json!({}), Duration::from_secs(10)).await?;
+ // A Chrome window behind other apps on a Mac doesn't have the OS's focus, so its page
+ // can act unfocused and drop typed keys. While anyone watches or controls it, the page
+ // behaves as focused (as Playwright does for every page). It ends with this connection.
+ let _ = cdp.call("Emulation.setFocusEmulationEnabled", json!({"enabled": true}), PROBE_WAIT).await;
guard_app_links(cdp).await;
let (w, h) = shared.view();
cdp.call("Page.startScreencast", screencast_params(w as u32, h as u32), Duration::from_secs(10)).await?;