diff --git a/remote/chandra-remote.c b/remote/chandra-remote.c index 8ee9f7e..1515fca 100644 --- a/remote/chandra-remote.c +++ b/remote/chandra-remote.c @@ -203,6 +203,46 @@ int ctl_fire(CtlConn *c, const char *line) { return ctl_cmd(c, line, NULL, 0); } +void remote_osk_suppress_acquire(RemoteApp *app, int *client_flag) { + if (*client_flag) return; + pthread_mutex_lock(&app->osk_mu); + if (!*client_flag) { + *client_flag = 1; + if (app->osk_suppress_refs == 0) { + char reply[64] = {0}; + if (ctl_cmd(&app->ctl, "osk status", reply, sizeof reply) == 0 && + strncmp(reply, "ok on", 5) == 0) { + ctl_fire(&app->ctl, "osk off"); + app->osk_restore_on_end = 1; + } else { + app->osk_restore_on_end = 0; + } + } + app->osk_suppress_refs++; + } + pthread_mutex_unlock(&app->osk_mu); +} + +void remote_osk_suppress_release(RemoteApp *app, int *client_flag) { + if (!*client_flag) return; + pthread_mutex_lock(&app->osk_mu); + if (*client_flag) { + *client_flag = 0; + if (app->osk_suppress_refs > 0) app->osk_suppress_refs--; + if (app->osk_suppress_refs == 0 && app->osk_restore_on_end) { + app->osk_restore_on_end = 0; + ctl_fire(&app->ctl, "osk on"); + } + } + pthread_mutex_unlock(&app->osk_mu); +} + +void remote_osk_manual_override(RemoteApp *app) { + pthread_mutex_lock(&app->osk_mu); + app->osk_restore_on_end = 0; + pthread_mutex_unlock(&app->osk_mu); +} + /* ---- Shared-memory framebuffer reader ---- */ void fb_reader_init(FbReader *r, const char *shm_path) { @@ -605,6 +645,7 @@ int main(int argc, char **argv) { app.running = &g_running; pthread_mutex_init(&app.cfg_mu, NULL); pthread_mutex_init(&app.ping_mu, NULL); + pthread_mutex_init(&app.osk_mu, NULL); snprintf(app.config_path, sizeof app.config_path, "%s", config_path); snprintf(app.web_dir, sizeof app.web_dir, "%s", web_dir); snprintf(app.shm_path, sizeof app.shm_path, "%s", shm_path); @@ -761,6 +802,12 @@ int main(int argc, char **argv) { if (web_ts_fd >= 0) close(web_ts_fd); if (vnc_lo_fd >= 0) close(vnc_lo_fd); if (web_lo_fd >= 0) close(web_lo_fd); + pthread_mutex_lock(&app.osk_mu); + if (app.osk_restore_on_end) { + app.osk_restore_on_end = 0; + ctl_fire(&app.ctl, "osk on"); + } + pthread_mutex_unlock(&app.osk_mu); ctl_close(&app.ctl); unlink(pid_path); return 0; diff --git a/remote/keysym.c b/remote/keysym.c index 64340c1..4cce443 100644 --- a/remote/keysym.c +++ b/remote/keysym.c @@ -89,6 +89,8 @@ static const KeyMapEntry special_keys[] = { { 0xFFE5u, 58u, 0, 2 }, /* Caps_Lock */ { 0xFF7Fu, 69u, 0, 0 }, /* Num_Lock */ { 0xFF67u, 127u, 0, 0 }, /* Menu */ + { 0xFF61u, 99u, 0, 0 }, /* Print / SysRq */ + { 0xFF13u, 119u, 0, 0 }, /* Pause */ /* Media / Android hardware keys */ { 0x1008FF11u, 114u, 0, 0 }, /* XF86AudioLowerVolume */ { 0x1008FF12u, 113u, 0, 0 }, /* XF86AudioMute */ @@ -179,6 +181,27 @@ static void flush_unicode_run(CtlConn *ctl, const uint8_t *buf, size_t len) { ctl_fire(ctl, cmd); } +void remote_send_codepoint(CtlConn *ctl, uint32_t uc) { + uint8_t u8[8]; + size_t n = 0; + if (uc < 0x80u) { + u8[n++] = (uint8_t)uc; + } else if (uc < 0x800u) { + u8[n++] = (uint8_t)(0xC0u | (uc >> 6)); + u8[n++] = (uint8_t)(0x80u | (uc & 0x3Fu)); + } else if (uc < 0x10000u) { + u8[n++] = (uint8_t)(0xE0u | (uc >> 12)); + u8[n++] = (uint8_t)(0x80u | ((uc >> 6) & 0x3Fu)); + u8[n++] = (uint8_t)(0x80u | (uc & 0x3Fu)); + } else if (uc <= 0x10FFFFu) { + u8[n++] = (uint8_t)(0xF0u | (uc >> 18)); + u8[n++] = (uint8_t)(0x80u | ((uc >> 12) & 0x3Fu)); + u8[n++] = (uint8_t)(0x80u | ((uc >> 6) & 0x3Fu)); + u8[n++] = (uint8_t)(0x80u | (uc & 0x3Fu)); + } + if (n > 0) flush_unicode_run(ctl, u8, n); +} + void remote_send_utf8_text(CtlConn *ctl, const char *utf8) { const uint8_t *p = (const uint8_t *)utf8; uint8_t run[900]; diff --git a/remote/remote_internal.h b/remote/remote_internal.h index fde1534..64d5195 100644 --- a/remote/remote_internal.h +++ b/remote/remote_internal.h @@ -50,6 +50,9 @@ typedef struct { uint8_t hmac_key[32]; volatile sig_atomic_t *running; pthread_mutex_t ping_mu; + pthread_mutex_t osk_mu; + int osk_suppress_refs; + int osk_restore_on_end; char shm_path[512]; } RemoteApp; @@ -75,6 +78,9 @@ void ctl_init(CtlConn *c, const char *sock_path); void ctl_close(CtlConn *c); int ctl_cmd(CtlConn *c, const char *line, char *reply, size_t reply_cap); int ctl_fire(CtlConn *c, const char *line); +void remote_osk_suppress_acquire(RemoteApp *app, int *client_flag); +void remote_osk_suppress_release(RemoteApp *app, int *client_flag); +void remote_osk_manual_override(RemoteApp *app); /* Shared framebuffer reader (chandra-remote.c) */ void fb_reader_init(FbReader *r, const char *shm_path); @@ -102,6 +108,7 @@ int remote_find_tailscale_ipv4(char *out, size_t cap); /* Keysym & UTF-8 translation (keysym.c) */ int keysym_to_evdev(uint32_t keysym, uint32_t *out_evdev, int *out_shift, int *out_mod_bit, uint32_t *out_unicode); +void remote_send_codepoint(CtlConn *ctl, uint32_t uc); void remote_send_utf8_text(CtlConn *ctl, const char *utf8); void hex_encode(const uint8_t *in, size_t len, char *out); diff --git a/remote/rfb.c b/remote/rfb.c index c1aea68..cd1c845 100644 --- a/remote/rfb.c +++ b/remote/rfb.c @@ -45,6 +45,7 @@ typedef struct { uint64_t last_send_ms; uint8_t prev_btn_mask; uint32_t held_mods; + int osk_suppressed; int z_init; z_stream z; tjhandle tj; @@ -512,32 +513,12 @@ static int rfb_handle_message(RfbClient *c) { if (read_exact(c->fd, buf, 7) < 0) return -1; int down = buf[0] != 0; uint32_t sym = get_be32(buf + 3); + if (down) remote_osk_suppress_acquire(c->app, &c->osk_suppressed); uint32_t ev = 0, uc = 0; int sh = 0, mod_bit = 0; if (!keysym_to_evdev(sym, &ev, &sh, &mod_bit, &uc)) break; if (uc != 0) { - if (down) { - uint8_t u8[8]; - size_t n = 0; - if (uc < 0x80u) { u8[n++] = (uint8_t)uc; } - else if (uc < 0x800u) { - u8[n++] = (uint8_t)(0xC0u | (uc >> 6)); - u8[n++] = (uint8_t)(0x80u | (uc & 0x3Fu)); - } else if (uc < 0x10000u) { - u8[n++] = (uint8_t)(0xE0u | (uc >> 12)); - u8[n++] = (uint8_t)(0x80u | ((uc >> 6) & 0x3Fu)); - u8[n++] = (uint8_t)(0x80u | (uc & 0x3Fu)); - } else { - u8[n++] = (uint8_t)(0xF0u | (uc >> 18)); - u8[n++] = (uint8_t)(0x80u | ((uc >> 12) & 0x3Fu)); - u8[n++] = (uint8_t)(0x80u | ((uc >> 6) & 0x3Fu)); - u8[n++] = (uint8_t)(0x80u | (uc & 0x3Fu)); - } - char cmd[48]; - memcpy(cmd, "input-text ", 11); - hex_encode(u8, n, cmd + 11); - ctl_fire(&c->app->ctl, cmd); - } + if (down) remote_send_codepoint(&c->app->ctl, uc); break; } if (mod_bit) { @@ -754,6 +735,7 @@ void rfb_handle_client(RemoteApp *app, int fd) { done: if (c->held_mods) rfb_send_mods(c, 0u); + remote_osk_suppress_release(app, &c->osk_suppressed); if (c->prev_btn_mask & 0x01u) ctl_fire(&app->ctl, "input-btn 272 0"); if (c->prev_btn_mask & 0x02u) ctl_fire(&app->ctl, "input-btn 274 0"); if (c->prev_btn_mask & 0x04u) ctl_fire(&app->ctl, "input-btn 273 0"); diff --git a/remote/test_remote.py b/remote/test_remote.py index 651731e..3d629e2 100644 --- a/remote/test_remote.py +++ b/remote/test_remote.py @@ -236,6 +236,17 @@ class MockCtlServer: elif cmd == "screen toggle": self.screen = "off" if self.screen == "on" else "on" rep = f"ok {self.screen}\n" + elif cmd == "osk status": + rep = f"ok {'on' if self.osk else 'off'}\n" + elif cmd == "osk off": + self.osk = 0 + rep = "ok off\n" + elif cmd == "osk on": + self.osk = 1 + rep = "ok on\n" + elif cmd == "osk toggle": + self.osk = 0 if self.osk else 1 + rep = f"ok {'on' if self.osk else 'off'}\n" else: rep = "ok\n" conn.sendall(rep.encode()) @@ -638,6 +649,118 @@ class RemoteTests(unittest.TestCase): self.assertIn("101", status_line) ws.close() + def test_06_web_keyboard_forwarding_release_and_osk_autosuppress(self): + self.shm.write_frame(160, 120, lambda x, y: (30, 80, 180)) + self.start_daemon(auth="none") + + c = http.client.HTTPConnection("127.0.0.1", self.web_port, timeout=5) + c.request("GET", "/") + r = c.getresponse() + self.assertEqual(r.status, 200) + html = r.read().decode("utf-8") + self.assertIn("window.addEventListener('keydown'", html) + c.close() + + # Connect WebSocket and wait for initial status (osk=1) + ws, status_line, _, extra = ws_connect("127.0.0.1", self.web_port) + self.assertIn("101", status_line) + buf = bytearray(extra) + + def read_next_status(sock, rxbuf, timeout=3.0): + deadline = time.time() + timeout + while time.time() < deadline: + op, payload = ws_read_frame(sock, rxbuf) + if op == 1: + return json.loads(payload) + raise TimeoutError("timed out waiting for status") + + st0 = read_next_status(ws, buf) + self.assertEqual(st0["osk"], 1) + + # Shifted letter, Ctrl+C, F5, Super, CapsLock, non-ASCII Unicode, and release + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFE1, "d": 1})) # Shift_L + ws_send_text(ws, json.dumps({"t": "key", "k": 0x0041, "d": 1})) # 'A' + ws_send_text(ws, json.dumps({"t": "key", "k": 0x0041, "d": 0})) + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFE1, "d": 0})) + + # OSK should auto-suppress to 0 on first keystroke and push updated status + st1 = read_next_status(ws, buf) + self.assertEqual(st1["osk"], 0) + self.assertEqual(self.ctl.osk, 0) + + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFE3, "d": 1})) # Control_L + ws_send_text(ws, json.dumps({"t": "key", "k": 0x0063, "d": 1})) # 'c' + ws_send_text(ws, json.dumps({"t": "key", "k": 0x0063, "d": 0})) + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFE3, "d": 0})) + + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFC2, "d": 1})) # F5 (evdev 63) + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFC2, "d": 0})) + + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFEB, "d": 1})) # Super_L (evdev 125, mod 64) + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFEB, "d": 0})) + + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFE5, "d": 1})) # CapsLock (evdev 58, no depressed mod) + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFE5, "d": 0})) + + ws_send_text(ws, json.dumps({"t": "key", "k": 0x00E9, "d": 1})) # Unicode 'é' + ws_send_text(ws, json.dumps({"t": "key", "k": 0x00E9, "d": 0})) + + # Hold Shift_L + 'b' (evdev 48) and test release clearing both + ws_send_text(ws, json.dumps({"t": "key", "k": 0xFFE1, "d": 1})) + ws_send_text(ws, json.dumps({"t": "key", "k": 0x0062, "d": 1})) + ws_send_text(ws, json.dumps({"t": "release"})) + + time.sleep(0.2) + cmds = self.ctl.commands + joined = "\n".join(cmds) + self.assertIn("osk status", joined) + self.assertIn("osk off", joined) + self.assertIn("input-mods 1 0 0 0", joined) + self.assertIn("input-key 30 1", joined) + self.assertIn("input-mods 4 0 0 0", joined) + self.assertIn("input-key 46 1", joined) + self.assertIn("input-key 63 1", joined) + self.assertIn("input-mods 64 0 0 0", joined) + self.assertIn("input-key 125 1", joined) + self.assertIn("input-key 58 1", joined) + self.assertIn("input-key 58 0", joined) + self.assertNotIn("input-mods 2 0 0 0", joined) + self.assertIn("input-text c3a9", joined) + # Release cleared stuck 'b' (48), Shift_L (42), and depressed mods + rel_idx = len(cmds) - 1 - cmds[::-1].index("input-key 48 0") + tail = "\n".join(cmds[rel_idx:]) + self.assertIn("input-key 48 0", tail) + self.assertIn("input-key 42 0", tail) + self.assertIn("input-mods 0 0 0 0", tail) + + ws.close() + deadline = time.time() + 3.0 + while time.time() < deadline and self.ctl.osk != 1: + time.sleep(0.05) + self.assertEqual(self.ctl.osk, 1) + + # Manual override: explicit `ctl: "osk on"` stays on across later keystrokes & disconnect + ws2, status_line2, _, extra2 = ws_connect("127.0.0.1", self.web_port) + self.assertIn("101", status_line2) + buf2 = bytearray(extra2) + _ = read_next_status(ws2, buf2) + ws_send_text(ws2, json.dumps({"t": "key", "k": 0x0061, "d": 1})) + ws_send_text(ws2, json.dumps({"t": "key", "k": 0x0061, "d": 0})) + self.assertEqual(read_next_status(ws2, buf2)["osk"], 0) + self.assertEqual(self.ctl.osk, 0) + + ws_send_text(ws2, json.dumps({"t": "ctl", "cmd": "osk on"})) + self.assertEqual(read_next_status(ws2, buf2)["osk"], 1) + self.assertEqual(self.ctl.osk, 1) + + ws_send_text(ws2, json.dumps({"t": "key", "k": 0x0062, "d": 1})) + ws_send_text(ws2, json.dumps({"t": "key", "k": 0x0062, "d": 0})) + time.sleep(0.15) + self.assertEqual(self.ctl.osk, 1) + ws2.close() + time.sleep(0.15) + self.assertEqual(self.ctl.osk, 1) + if __name__ == "__main__": unittest.main() diff --git a/remote/web.c b/remote/web.c index 42c4470..e3b769f 100644 --- a/remote/web.c +++ b/remote/web.c @@ -264,6 +264,10 @@ typedef struct { int fd; RemoteApp *app; uint32_t held_mods; + uint32_t mod_keys_mask; + uint32_t down_ev[16]; + int down_ev_len; + int osk_suppressed; uint32_t acked_id; uint32_t next_frame_id; int quality; @@ -282,6 +286,55 @@ static void ws_send_status(WsState *ws) { } } +static uint32_t ws_mods_from_mask(uint32_t m) { + return ((m & 0x03u) ? 1u : 0u) | + ((m & 0x0Cu) ? 4u : 0u) | + ((m & 0x30u) ? 8u : 0u) | + ((m & 0xC0u) ? 64u : 0u); +} + +static int ws_mod_evdev_bit(uint32_t ev) { + switch (ev) { + case 42u: return 0; + case 54u: return 1; + case 29u: return 2; + case 97u: return 3; + case 56u: return 4; + case 100u: return 5; + case 125u: return 6; + case 126u: return 7; + default: return -1; + } +} + +static void ws_release_all(WsState *ws) { + char cmd[64]; + for (int i = 0; i < ws->down_ev_len; i++) { + snprintf(cmd, sizeof cmd, "input-key %u 0", ws->down_ev[i]); + ctl_fire(&ws->app->ctl, cmd); + } + ws->down_ev_len = 0; + static const uint32_t mod_evdevs[8] = { 42u, 54u, 29u, 97u, 56u, 100u, 125u, 126u }; + for (int i = 0; i < 8; i++) { + if (ws->mod_keys_mask & (1u << i)) { + snprintf(cmd, sizeof cmd, "input-key %u 0", mod_evdevs[i]); + ctl_fire(&ws->app->ctl, cmd); + } + } + int had_mods = (ws->mod_keys_mask != 0 || ws->held_mods != 0); + ws->mod_keys_mask = 0; + ws->held_mods = 0; + if (had_mods) ctl_fire(&ws->app->ctl, "input-mods 0 0 0 0"); +} + +static void ws_note_key_activity(WsState *ws) { + if (!ws->osk_suppressed) { + remote_osk_suppress_acquire(ws->app, &ws->osk_suppressed); + ws->last_status_json[0] = 0; + ws_send_status(ws); + } +} + static void ws_handle_text(WsState *ws, const char *msg) { char type[32] = {0}; if (!json_get_str(msg, "t", type, sizeof type)) return; @@ -346,9 +399,14 @@ static void ws_handle_text(WsState *ws, const char *msg) { } return; } + if (strcmp(type, "release") == 0) { + ws_release_all(ws); + return; + } if (strcmp(type, "ktap") == 0) { long code = 0, mods = 0; if (json_get_int(msg, "c", &code) && code > 0) { + ws_note_key_activity(ws); json_get_int(msg, "m", &mods); char cmd[64]; snprintf(cmd, sizeof cmd, "input-key-tap %ld %ld", code, mods); @@ -360,19 +418,51 @@ static void ws_handle_text(WsState *ws, const char *msg) { long sym = 0, down = 1; if (!json_get_int(msg, "k", &sym)) return; json_get_int(msg, "d", &down); + if (down) ws_note_key_activity(ws); uint32_t ev = 0, uc = 0; int sh = 0, mod_bit = 0; if (!keysym_to_evdev((uint32_t)sym, &ev, &sh, &mod_bit, &uc)) return; + if (uc != 0) { + if (down) remote_send_codepoint(&ws->app->ctl, uc); + return; + } + if (mod_bit == 2) { /* CapsLock */ + char cmd[64]; + snprintf(cmd, sizeof cmd, "input-key %u %d", ev, down ? 1 : 0); + ctl_fire(&ws->app->ctl, cmd); + return; + } if (mod_bit) { - if (down) ws->held_mods |= (uint32_t)mod_bit; - else ws->held_mods &= ~(uint32_t)mod_bit; + int bit = ws_mod_evdev_bit(ev); + if (bit >= 0) { + if (down) ws->mod_keys_mask |= (1u << bit); + else ws->mod_keys_mask &= ~(1u << bit); + } + ws->held_mods = ws_mods_from_mask(ws->mod_keys_mask); char cmd[64]; snprintf(cmd, sizeof cmd, "input-mods %u 0 0 0", ws->held_mods); ctl_fire(&ws->app->ctl, cmd); snprintf(cmd, sizeof cmd, "input-key %u %d", ev, down ? 1 : 0); ctl_fire(&ws->app->ctl, cmd); } else if (ev) { - uint32_t em = ws->held_mods | (sh ? 1u : 0u); + if (down) { + int found = 0; + for (int i = 0; i < ws->down_ev_len; i++) { + if (ws->down_ev[i] == ev) { found = 1; break; } + } + if (!found && ws->down_ev_len < 16) ws->down_ev[ws->down_ev_len++] = ev; + } else { + for (int i = 0; i < ws->down_ev_len; i++) { + if (ws->down_ev[i] == ev) { + for (int j = i + 1; j < ws->down_ev_len; j++) ws->down_ev[j - 1] = ws->down_ev[j]; + ws->down_ev_len--; + break; + } + } + } + uint32_t em = (sym > 0x20 && sym <= 0x7E && (ws->held_mods & ~1u) == 0) + ? (sh ? 1u : 0u) + : (ws->held_mods | (sh ? 1u : 0u)); char cmd[64]; if (em != ws->held_mods) { snprintf(cmd, sizeof cmd, "input-mods %u 0 0 0", em); @@ -390,6 +480,7 @@ static void ws_handle_text(WsState *ws, const char *msg) { if (strcmp(type, "text") == 0) { char txt[1024] = {0}; if (json_get_str(msg, "s", txt, sizeof txt) && txt[0]) { + ws_note_key_activity(ws); remote_send_utf8_text(&ws->app->ctl, txt); } return; @@ -411,6 +502,10 @@ static void ws_handle_text(WsState *ws, const char *msg) { if (strcmp(type, "ctl") == 0) { char cstr[128] = {0}; if (json_get_str(msg, "cmd", cstr, sizeof cstr) && ctl_command_allowed(cstr)) { + if (strncmp(cstr, "osk ", 4) == 0) { + remote_osk_manual_override(ws->app); + ws->osk_suppressed = 1; + } ctl_fire(&ws->app->ctl, cstr); ws->last_status_json[0] = 0; ws_send_status(ws); @@ -602,7 +697,8 @@ static void ws_loop(RemoteApp *app, int fd) { } out: - if (ws.held_mods) ctl_fire(&app->ctl, "input-mods 0 0 0 0"); + ws_release_all(&ws); + remote_osk_suppress_release(app, &ws.osk_suppressed); if (tj) tjDestroy(tj); fb_reader_close(&fb); free(scratch); diff --git a/remote/web/index.html b/remote/web/index.html index a2304ed..5238de1 100644 --- a/remote/web/index.html +++ b/remote/web/index.html @@ -316,6 +316,7 @@ }; sock.onclose = async () => { if (ws !== sock) return; + releaseAllKeys(); setStatusDot('err', 'Disconnected'); await bootstrap(); }; @@ -379,6 +380,9 @@ stage.addEventListener('pointerdown', (e) => { if (e.target.closest('#pillWrap') || e.target.closest('#kbdBar') || e.target.closest('#loginMask')) return; + if (document.activeElement && document.activeElement !== document.body && document.activeElement !== kbdInput) { + document.activeElement.blur(); + } e.preventDefault(); stage.setPointerCapture(e.pointerId); pointers.set(e.pointerId, { x: e.clientX, y: e.clientY }); @@ -502,6 +506,103 @@ sendJson({ t: 'scroll', x: fx, y: fy, dx: Math.round(e.deltaX), dy: Math.round(e.deltaY) }); }, { passive: false }); + /* ---- Global desktop physical-keyboard forwarding ---- */ + const pressedKeys = new Map(); + + function eventToKeysym(e) { + switch (e.key) { + case 'Backspace': return 0xFF08; + case 'Tab': return 0xFF09; + case 'Enter': return e.code === 'NumpadEnter' ? 0xFF8D : 0xFF0D; + case 'Escape': return 0xFF1B; + case 'Insert': return 0xFF63; + case 'Delete': return 0xFFFF; + case 'Home': return 0xFF50; + case 'End': return 0xFF57; + case 'PageUp': return 0xFF55; + case 'PageDown': return 0xFF56; + case 'ArrowLeft': return 0xFF51; + case 'ArrowUp': return 0xFF52; + case 'ArrowRight': return 0xFF53; + case 'ArrowDown': return 0xFF54; + case 'Shift': return e.code === 'ShiftRight' ? 0xFFE2 : 0xFFE1; + case 'Control': return e.code === 'ControlRight' ? 0xFFE4 : 0xFFE3; + case 'Alt': return e.code === 'AltRight' ? 0xFFEA : 0xFFE9; + case 'AltGraph': return 0xFE03; + case 'Meta': return e.code === 'MetaRight' ? 0xFFEC : 0xFFEB; + case 'CapsLock': return 0xFFE5; + case 'NumLock': return 0xFF7F; + case 'ContextMenu': return 0xFF67; + case 'PrintScreen': return 0xFF61; + case 'Pause': return 0xFF13; + case 'AudioVolumeDown': return 0x1008FF11; + case 'AudioVolumeMute': return 0x1008FF12; + case 'AudioVolumeUp': return 0x1008FF13; + } + const fm = /^F([1-9]|1[0-2])$/.exec(e.key); + if (fm) return 0xFFBE + (Number(fm[1]) - 1); + if (e.key.length === 1) { + const cp = e.key.codePointAt(0); + if (cp >= 0x20 && cp <= 0x7E) return cp; + } + if ((e.ctrlKey || e.altKey || e.metaKey) && e.code) { + const km = /^Key([A-Z])$/.exec(e.code); + if (km) { + const ch = e.shiftKey ? km[1] : km[1].toLowerCase(); + return ch.charCodeAt(0); + } + const dm = /^Digit([0-9])$/.exec(e.code); + if (dm) return dm[1].charCodeAt(0); + } + return null; + } + + function releaseAllKeys() { + if (pressedKeys.size > 0) { + for (const sym of [...pressedKeys.values()].reverse()) { + sendJson({ t: 'key', k: sym, d: 0 }); + } + pressedKeys.clear(); + } + sendJson({ t: 'release' }); + } + + window.addEventListener('keydown', (e) => { + if (!ws || ws.readyState !== WebSocket.OPEN) return; + if (loginMask.classList.contains('open')) return; + if (e.target === passInput || e.target === briSlider || e.target === kbdInput) return; + if (e.isComposing || e.key === 'Unidentified' || e.key === 'Process' || e.key === 'Dead') return; + const sym = eventToKeysym(e); + if (sym !== null) { + e.preventDefault(); + const id = e.code || e.key; + if (e.repeat && pressedKeys.has(id)) return; + pressedKeys.set(id, sym); + sendJson({ t: 'key', k: sym, d: 1 }); + } else if (e.key.length === 1 && !e.ctrlKey && !e.altKey && !e.metaKey) { + e.preventDefault(); + sendJson({ t: 'text', s: e.key }); + } + }); + + window.addEventListener('keyup', (e) => { + if (!ws || ws.readyState !== WebSocket.OPEN) return; + if (loginMask.classList.contains('open')) return; + if (e.target === passInput || e.target === briSlider || e.target === kbdInput) return; + const id = e.code || e.key; + if (pressedKeys.has(id)) { + e.preventDefault(); + const sym = pressedKeys.get(id); + pressedKeys.delete(id); + sendJson({ t: 'key', k: sym, d: 0 }); + } + }); + + window.addEventListener('blur', releaseAllKeys); + document.addEventListener('visibilitychange', () => { + if (document.hidden) releaseAllKeys(); + }); + /* ---- Android Gboard sentinel-space keyboard bridge ---- */ function setKbOpen(open) { kbOpen = open; @@ -582,8 +683,10 @@ keyHandled = true; if (e.ctrlKey) sendJson({ t: 'key', k: 0xFFE3, d: 1 }); if (e.altKey) sendJson({ t: 'key', k: 0xFFE9, d: 1 }); + if (e.metaKey) sendJson({ t: 'key', k: 0xFFEB, d: 1 }); sendJson({ t: 'key', k: e.key.charCodeAt(0), d: 1 }); sendJson({ t: 'key', k: e.key.charCodeAt(0), d: 0 }); + if (e.metaKey) sendJson({ t: 'key', k: 0xFFEB, d: 0 }); if (e.altKey) sendJson({ t: 'key', k: 0xFFE9, d: 0 }); if (e.ctrlKey) sendJson({ t: 'key', k: 0xFFE3, d: 0 }); } @@ -614,6 +717,10 @@ }); /* ---- UI controls ---- */ + document.getElementById('pillWrap').addEventListener('click', (e) => { + const btn = e.target.closest('button'); + if (btn && btn !== kbBtn) btn.blur(); + }); menuBtn.addEventListener('click', () => drawer.classList.toggle('open')); modeBtn.addEventListener('click', () => { trackpadMode = !trackpadMode;