diff --git a/client/ScreenManager.cpp b/client/ScreenManager.cpp index 06adbc0..d5e5ae5 100644 --- a/client/ScreenManager.cpp +++ b/client/ScreenManager.cpp @@ -673,6 +673,9 @@ BOOL CScreenManager::OnReconnect() if (!m_bIsWorking) { return FALSE; } + // 断线期间主控端的 keyup 可能已经丢了;重连前先把按着的键松开, + // 否则远程要等到用户再按一次同一个修饰键才恢复。 + ReleaseInjectedKeys(); auto duration = GetTickCount64() - m_nReconnectTime; if (duration <= 3000) Sleep(3000 - duration); @@ -841,6 +844,8 @@ VOID CScreenManager::SendBitMapInfo() CScreenManager::~CScreenManager() { Mprintf("ScreenManager 析构函数\n"); + // 会话结束:主控端不会再送来 keyup,把还按着的键松开。 + ReleaseInjectedKeys(); if (!m_Signature.empty()) UninitFileUpload(); m_bIsWorking = FALSE; @@ -1841,6 +1846,69 @@ bool IsExtendedKey(WPARAM vKey) } } +// 辅助判断是否为修饰键(VK_SHIFT/CONTROL/MENU 以及左右专有码、Win 键) +static bool IsModifierKeyVK(WORD vk) +{ + switch (vk) { + case VK_SHIFT: + case VK_LSHIFT: + case VK_RSHIFT: + case VK_CONTROL: + case VK_LCONTROL: + case VK_RCONTROL: + case VK_MENU: + case VK_LMENU: + case VK_RMENU: + case VK_LWIN: + case VK_RWIN: + return true; + default: + return false; + } +} + +// 注入按键的身份:VK 在低 16 位,扫描码在 16..23,扩展位在 bit24。左右 +// Shift/Ctrl/Alt 的 VK 完全相同,只有扫描码(Shift)或扩展位(右 Ctrl/Alt) +// 能区分;补发 keyup 必须复用按下时的身份,否则右 Shift 抬起的是左 Shift, +// 远程右侧修饰键仍然保持按下。 +static DWORD MakeInjectedKeyId(WORD vk, WORD scan, bool extended) +{ + return (DWORD)vk | ((DWORD)(scan & 0xFF) << 16) | (extended ? (1UL << 24) : 0); +} + +void CScreenManager::TrackInjectedKey(DWORD keyId, bool down) +{ + std::lock_guard lock(m_InjectedKeysMutex); + if (down) m_InjectedKeys.insert(keyId); + else m_InjectedKeys.erase(keyId); +} + +// 补发所有未收到 keyup 的按键:先普通键再修饰键。主控端丢 keyup 或直接断开时 +// 调用,避免远程系统把 Ctrl/Shift/Alt 一直当成按下("打字很怪"的根因)。 +void CScreenManager::ReleaseInjectedKeys() +{ + std::set keys; + { + std::lock_guard lock(m_InjectedKeysMutex); + keys.swap(m_InjectedKeys); + } + if (keys.empty()) return; + Mprintf("ScreenManager: 补发 %zu 个按键的 keyup\n", keys.size()); + INPUT k_input = { 0 }; + k_input.type = INPUT_KEYBOARD; + for (int pass = 0; pass < 2; ++pass) { + for (DWORD id : keys) { + WORD vk = (WORD)(id & 0xFFFF); + if (IsModifierKeyVK(vk) != (pass == 1)) continue; + k_input.ki.wVk = vk; + k_input.ki.wScan = (WORD)((id >> 16) & 0xFF); + k_input.ki.dwFlags = KEYEVENTF_KEYUP; + if (id & (1UL << 24)) k_input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; + SendInput(1, &k_input, sizeof(INPUT)); + } + } +} + VOID CScreenManager::ProcessCommand(LPBYTE szBuffer, ULONG ulLength) { // 记录大小判定:现代控制端(本服务端)统一发 48 字节 MSG64;28 字节 MSG32 仅为兼容 @@ -2353,7 +2421,9 @@ VOID CScreenManager::ProcessCommand(LPBYTE szBuffer, ULONG ulLength) k_input.ki.dwFlags = 0; if ((Msg->lParam >> 24) & 1) // 从 lParam bit24 取扩展键标志 k_input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; - SendInput(1, &k_input, sizeof(INPUT)); + if (SendInput(1, &k_input, sizeof(INPUT)) == 1) + TrackInjectedKey(MakeInjectedKeyId((WORD)Msg->wParam, k_input.ki.wScan, + (k_input.ki.dwFlags & KEYEVENTF_EXTENDEDKEY) != 0), true); break; } case WM_KEYUP: @@ -2365,7 +2435,9 @@ VOID CScreenManager::ProcessCommand(LPBYTE szBuffer, ULONG ulLength) k_input.ki.dwFlags = KEYEVENTF_KEYUP; if ((Msg->lParam >> 24) & 1) // 从 lParam bit24 取扩展键标志 k_input.ki.dwFlags |= KEYEVENTF_EXTENDEDKEY; - SendInput(1, &k_input, sizeof(INPUT)); + if (SendInput(1, &k_input, sizeof(INPUT)) == 1) + TrackInjectedKey(MakeInjectedKeyId((WORD)Msg->wParam, k_input.ki.wScan, + (k_input.ki.dwFlags & KEYEVENTF_EXTENDEDKEY) != 0), false); break; } } diff --git a/client/ScreenManager.h b/client/ScreenManager.h index b9b3f5b..609649e 100644 --- a/client/ScreenManager.h +++ b/client/ScreenManager.h @@ -19,6 +19,8 @@ bool IsWindows8orHigher(); #include "ScreenSpy.h" #include "ScreenCapture.h" +#include +#include // WASAPI 前向声明 (COM 接口) struct IMMDevice; @@ -61,6 +63,13 @@ public: VOID SendNextScreen(const char* szBuffer, ULONG ulNextSendLength); VOID ProcessCommand(LPBYTE szBuffer, ULONG ulLength); + // 已注入、尚未收到 keyup 的按键。主控端丢 keyup(浏览器失焦/断线、控制端 + // 切换窗口)时远程系统会把 Ctrl/Shift/Alt 一直当成按下,表现为"打字很怪"。 + // 会话结束或重连时在这里补发 keyup 兜底。 + // keyId 编码 VK+扫描码+扩展位(见 MakeInjectedKeyId):左右 Shift/Ctrl/Alt + // 的 VK 相同,必须按注入时的同一身份补发,否则抬起的是另一边的键。 + void TrackInjectedKey(DWORD keyId, bool down); + void ReleaseInjectedKeys(); UserParam *m_pUserParam = NULL; INT_PTR m_ptrUser; HDESK g_hDesk; @@ -118,6 +127,8 @@ public: POINT m_rclickPoint; // 右键点击坐标 HWND m_rclickWnd; // 右键窗口 int m_nSwitchWindowIndex = 0; // 切换窗口索引 + std::set m_InjectedKeys; // 注入后未收到 keyup 的键(VK+扫描码+扩展位) + std::mutex m_InjectedKeysMutex; // ProcessCommand 与析构/重连可能并发 // ========== 系统音频捕获 (WASAPI Loopback) ========== volatile BOOL m_bAudioThreadRunning = FALSE;// 音频线程运行标志(独立于视频线程) diff --git a/linux/ScreenHandler.h b/linux/ScreenHandler.h index 21ab694..f1dc420 100644 --- a/linux/ScreenHandler.h +++ b/linux/ScreenHandler.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -420,6 +421,27 @@ public: m_captureThread.join(); } } + // 会话结束:控制端不会再来 keyup,把还按着的键松开(修饰键最后放), + // 否则 X 服务器一直认为 Ctrl/Shift/Alt 按下,表现为"远程打字很怪"。 + if (m_inputDisplay && m_x11.pXTestFakeKeyEvent) { + std::set keys, modifierKeys; + { + std::lock_guard lock(m_injectedKeysMutex); + keys.swap(m_injectedKeys); + modifierKeys.swap(m_injectedModifierKeys); + } + for (auto& kc : modifierKeys) keys.erase(kc); + bool any = false; + for (unsigned int keycode : keys) { + m_x11.pXTestFakeKeyEvent(m_inputDisplay, keycode, False, 0); + any = true; + } + for (unsigned int keycode : modifierKeys) { + m_x11.pXTestFakeKeyEvent(m_inputDisplay, keycode, False, 0); + any = true; + } + if (any) m_x11.pXFlush(m_inputDisplay); + } if (m_inputDisplay && m_x11.pXCloseDisplay) { m_x11.pXCloseDisplay(m_inputDisplay); m_inputDisplay = nullptr; @@ -866,7 +888,8 @@ public: if (keysym) { unsigned int keycode = m_x11.pXKeysymToKeycode(m_inputDisplay, keysym); if (keycode) { - m_x11.pXTestFakeKeyEvent(m_inputDisplay, keycode, True, 0); + if (m_x11.pXTestFakeKeyEvent(m_inputDisplay, keycode, True, 0)) + TrackInjectedKey(keycode, IsModifierVK(vk), true); m_x11.pXFlush(m_inputDisplay); } } @@ -880,7 +903,8 @@ public: if (keysym) { unsigned int keycode = m_x11.pXKeysymToKeycode(m_inputDisplay, keysym); if (keycode) { - m_x11.pXTestFakeKeyEvent(m_inputDisplay, keycode, False, 0); + if (m_x11.pXTestFakeKeyEvent(m_inputDisplay, keycode, False, 0)) + TrackInjectedKey(keycode, false, false); m_x11.pXFlush(m_inputDisplay); } } @@ -889,7 +913,30 @@ public: } } + // 注入后尚未收到 keyup 的按键(X keycode)。控制端丢 keyup 或直接断开时 + // 在析构里补发 keyup,避免 X 服务器一直认为修饰键按下。 + static bool IsModifierVK(unsigned int vk) + { + // Shift / Ctrl / Alt / LWin / RWin + return vk == 16 || vk == 17 || vk == 18 || vk == 91 || vk == 92; + } + + void TrackInjectedKey(unsigned int keycode, bool isModifier, bool down) + { + std::lock_guard lock(m_injectedKeysMutex); + if (down) { + m_injectedKeys.insert(keycode); + if (isModifier) m_injectedModifierKeys.insert(keycode); + } else { + m_injectedKeys.erase(keycode); + m_injectedModifierKeys.erase(keycode); + } + } + private: + std::set m_injectedKeys; // 注入后未收到 keyup 的 X keycode + std::set m_injectedModifierKeys; // 其中属于修饰键的部分(最后释放) + std::mutex m_injectedKeysMutex; // OnReceive 回调与析构可能并发 IOCPClient* m_client; std::atomic m_running; std::atomic m_destroyed; // Flag to prevent Start() after destruction begins diff --git a/server/web/index.html b/server/web/index.html index 65a1ded..599b9ad 100644 --- a/server/web/index.html +++ b/server/web/index.html @@ -2794,6 +2794,10 @@ ws.onclose = () => { stopPingInterval(); updateWsStatus('disconnected'); + // 连接已断,补发 keyup 已不可能。把此刻仍按着的键移到 stale 集合, + // 重连并确认屏幕恢复后再补发;直接清空会使断线期间抬起的键永远 + // 留在远程按下状态。 + stashRemoteKeys(); stopAllAudio(); audioFormat = null; // WS 断开:关闭所有墙解码器释放 GPU 资源,保留 tile 布局。 @@ -2906,6 +2910,9 @@ if (msg.ok) { // Resolution may not be available yet (first connection) if (msg.width && msg.height) { + // 屏幕会话已确认在线(缓存会话),先补发断线期间遗留 + // 的 keyup,再进入正常控制。 + releaseStaleRemoteKeys(); updateScreenStatus('connected'); isTopDown = msg.top_down === true; isAndroidRemote = msg.client_type === 'APK'; @@ -3000,6 +3007,9 @@ if (!isFocus) break; updateScreenStatus('connected'); + // 冷启动重连时 connect_result 还没有尺寸,等这里(TOKEN_BITMAPINFO + // 已到、屏幕子连接已绑定)再补发断线前遗留的 keyup。 + releaseStaleRemoteKeys(); isTopDown = msg.top_down === true; if (msg.client_type !== undefined) { isAndroidRemote = msg.client_type === 'APK'; @@ -4947,6 +4957,8 @@ if (pageId !== 'screen-page') { const sp = document.getElementById('screen-page'); if (sp && sp.classList.contains('active')) { + // 离开远程页面:远程随后的 keyup 不会再到达,补发按着的键。 + releaseRemoteKeys(); if (document.fullscreenElement || document.webkitFullscreenElement) { const exit = document.exitFullscreen || document.webkitExitFullscreen; if (exit) exit.call(document).catch(() => {}); @@ -5025,6 +5037,8 @@ } function confirmLogout() { document.getElementById('logout-confirm-modal').classList.remove('active'); + // 立即断开前先补发 keyup(socket 一关就没机会了)。 + releaseRemoteKeys(); // 彻底清理屏幕墙(tile/session/订阅引用)并清除持久化布局。 wallTeardown(true); // Close and reconnect WebSocket to get new challenge @@ -5063,6 +5077,9 @@ let appDialogResolve = null; function openAppDialog(opts) { + // 弹窗抢走键盘焦点,之后收不到 keyup;先把远程按着的键补发。 + // socket 已断时保留 stale,等重连确认屏幕后补发。 + releaseRemoteKeysIfOnline(); // A pending dialog is superseded (e.g. an async error arrives while // a confirm is open): resolve it as cancelled so no caller hangs. if (appDialogResolve) { @@ -5476,6 +5493,7 @@ const sid = String(id); // 同页直接切设备(正常 UI 不会发生):先释放旧 focus 引用。 if (currentDevice && String(currentDevice.id) !== sid) { + releaseRemoteKeys(); // 旧设备上按着的键要在换设备前补发抬起 releaseStream(currentDevice.id, 'focus'); } currentDevice = dev; @@ -5726,6 +5744,104 @@ // Control mode state (mouse/keyboard control) let controlEnabled = false; + // ===== 远程按键状态跟踪 ===== + // 浏览器并不保证每个 keydown 都有对应的 keyup 送到页面:切窗口/切标签、 + // 地址栏或查找栏抢走焦点、浏览器快捷键(Ctrl+T/W/F)、断线时都会丢。 + // 丢掉一个 Ctrl 的 keyup,远程就"一直按着 Ctrl",表现为打字结果全成了 + // 组合键——再按一下 Ctrl 才恢复。这里记下已经下发的按下键,在失焦/断开/ + // 关闭控制时补发 keyup;下一次按键或点击时再按事件里的修饰键状态校正一次。 + const remoteDownKeys = new Set(); + // 断线(ws.onclose)那一刻仍按着的键:socket 已经关了,补发不出去,只能 + // 先搬到一边;重连并确认屏幕恢复(connect_result 带尺寸 / 收到 + // resolution_changed)后再补发 keyup,否则断线期间抬起的键会一直按在远程。 + const staleRemoteKeys = new Set(); + let staleRemoteKeysDevice = undefined; // stale 键属于哪台设备 + const MODIFIER_KEYS = [[16, 'shiftKey'], [17, 'ctrlKey'], [18, 'altKey']]; + + function isModifierKey(keyCode) { + return keyCode === 16 || keyCode === 17 || keyCode === 18; + } + + function trackRemoteKey(keyCode, isDown) { + if (isDown) remoteDownKeys.add(keyCode); + else remoteDownKeys.delete(keyCode); + } + + // 补发一组 keyup(先普通键,再修饰键)。 + function sendRemoteKeyups(keys) { + if (!keys.length) return; + keys.sort((a, b) => (isModifierKey(a) ? 1 : 0) - (isModifierKey(b) ? 1 : 0)); + for (const keyCode of keys) { + // 不走 sendKey:关闭控制/离开页面时 controlEnabled 已是 false, + // 但补发的 keyup 仍必须发出去(ws 已关则由 sendKeyRaw 丢弃)。 + sendKeyRaw(keyCode, false); + } + } + + // 补发所有未收到 keyup 的按键,并清空跟踪集合。断线前遗留的键一并补发。 + function releaseRemoteKeys() { + // 遗留的键只有在还是同一台设备时才补发:切到别的设备上补发会把用户 + // 正按着的修饰键误松开。 + const dev = currentDeviceId(); + if (staleRemoteKeys.size && dev !== undefined && + String(dev) === String(staleRemoteKeysDevice)) { + for (const keyCode of staleRemoteKeys) remoteDownKeys.add(keyCode); + } + staleRemoteKeys.clear(); + staleRemoteKeysDevice = undefined; + if (!remoteDownKeys.size) return; + const keys = Array.from(remoteDownKeys); + remoteDownKeys.clear(); + sendRemoteKeyups(keys); + } + + // 只补发断线前遗留的键,不动重连后新按下的键(那些可能还真的按着)。 + function releaseStaleRemoteKeys() { + if (!staleRemoteKeys.size) return; + const dev = currentDeviceId(); + const sameDevice = dev !== undefined && + String(dev) === String(staleRemoteKeysDevice); + const keys = Array.from(staleRemoteKeys); + staleRemoteKeys.clear(); + staleRemoteKeysDevice = undefined; + if (!sameDevice) return; // 已经不在那台设备上了:不能发给别人 + sendRemoteKeyups(keys); + } + + // socket 已断时补发不出去:这时保留 stale,等重连确认屏幕恢复后再补发。 + function releaseRemoteKeysIfOnline() { + if (!ws || ws.readyState !== WebSocket.OPEN) return; + releaseRemoteKeys(); + } + + // 断线瞬间把仍在按着的键挪到 stale 集合(socket 已关,补发不出去)。 + function stashRemoteKeys() { + skipNextCtrlVKeyup = false; // V 的物理 keyup 也收不到了,标记作废 + if (!remoteDownKeys.size) return; + const dev = currentDeviceId(); + if (dev === undefined) { + remoteDownKeys.clear(); + return; + } + if (staleRemoteKeys.size && String(staleRemoteKeysDevice) !== String(dev)) { + staleRemoteKeys.clear(); // 上一台设备遗留的键已无处可补 + } + for (const keyCode of remoteDownKeys) staleRemoteKeys.add(keyCode); + remoteDownKeys.clear(); + staleRemoteKeysDevice = dev; + } + + // 浏览器事件里的修饰键状态才是真实状态。若远程还按着、事件却说已经松开, + // 说明对应的 keyup 丢了,就地补发,不必再让用户按一次 Ctrl 才恢复。 + function releaseStaleModifiers(e) { + if (!controlEnabled) return; + for (const [keyCode, flag] of MODIFIER_KEYS) { + if (remoteDownKeys.has(keyCode) && !e[flag]) { + sendKeyRaw(keyCode, false); + } + } + } + // Remote cursor mapping (Windows cursor index -> CSS cursor) // Index matches CursorInfo.h: IDC_APPSTARTING(0) to IDC_WAIT(15), 254=custom, 255=unsupported // Custom I-beam cursor with white fill and black stroke for visibility on any background @@ -6118,6 +6234,9 @@ function toggleControl() { controlEnabled = !controlEnabled; + // 关闭控制:远程之后收不到任何 keyup,按着的 Ctrl/Shift/Alt 必须现在补发, + // 否则就会"打字很怪、再按一下 Ctrl 才好"。socket 已断时保留下来等重连补发。 + if (!controlEnabled) releaseRemoteKeysIfOnline(); // Update floating toolbar buttons const btnMouse = document.getElementById('btn-mouse'); btnMouse.classList.toggle('active', controlEnabled); @@ -6476,9 +6595,15 @@ if (!controlEnabled) return; // Control mode required // Filter Windows keys (handled locally, not sent to remote) if (keyCode === 91 || keyCode === 92) return; // VK_LWIN, VK_RWIN + if (sendKeyPacket(keyCode, isDown, altKey)) trackRemoteKey(keyCode, isDown); + } + + function sendKeyPacket(keyCode, isDown, altKey) { if (ws && ws.readyState === WebSocket.OPEN && token) { ws.send(JSON.stringify({ cmd: 'key', token, id: currentDeviceId(), keyCode, down: isDown, alt: !!altKey })); + return true; } + return false; } // 本地剪贴板 → 远程:读浏览器剪贴板,先 clipboard_set 到远程,再合成一次完整 Ctrl+V。 @@ -6506,9 +6631,7 @@ // 直接注入单个按键到远程(不经过 sendKey 的 controlEnabled 门槛)。 // "I" 组合框和 1/2/3…/中 同属输入辅助栏,不应要求先开鼠标控制。 function sendKeyRaw(keyCode, isDown) { - if (ws && ws.readyState === WebSocket.OPEN && token) { - ws.send(JSON.stringify({ cmd: 'key', token, id: currentDeviceId(), keyCode, down: isDown, alt: false })); - } + if (sendKeyPacket(keyCode, isDown, false)) trackRemoteKey(keyCode, isDown); } // "I" 手机输入法组合框:一次性把文本经剪贴板桥发到远程光标处。 @@ -7190,6 +7313,7 @@ canvas.addEventListener('mousedown', function(e) { e.preventDefault(); + releaseStaleModifiers(e); const pos = getMousePos(e); sendMouse('down', pos.x, pos.y, e.button); }); @@ -7226,6 +7350,7 @@ canvas.addEventListener('wheel', function(e) { e.preventDefault(); + releaseStaleModifiers(e); const pos = getMousePos(e); sendMouse('wheel', pos.x, pos.y, 0, e.deltaY, e.deltaMode); }, { passive: false }); @@ -7237,6 +7362,12 @@ if (!document.getElementById('screen-page').classList.contains('active')) return; // Keys belong to an open in-app dialog (buttons are not INPUT/TEXTAREA). if (document.getElementById('app-dialog').classList.contains('active')) return; + + // 上一次的 keyup 可能被浏览器吞掉了:按事件里的真实修饰键状态先校正。 + // 必须放在输入框判断之前——失焦丢 keyup 后点进输入框继续打字时, + // keydown 会提前 return,校正就永远不生效。 + releaseStaleModifiers(e); + if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return; // F11 belongs to the local fullscreen toggle UNLESS we're actively // controlling the remote — see the F11 handler above. Skip here @@ -7266,8 +7397,15 @@ document.addEventListener('keyup', function(e) { if (!document.getElementById('screen-page').classList.contains('active')) return; - if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') return; if (e.key === 'F11' && !controlEnabled) return; + // 焦点跑到输入框时本地按键不再转发;但按住时在画布按下的键,它的 + // keyup 必须补发,否则远程一直按着(典型:按住 Ctrl 再点输入框)。 + if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA') { + // Ctrl+V 桥的抑制标记若在这里错过,会一直留着吞掉下一次 V 的抬起。 + if (e.keyCode === 86 && skipNextCtrlVKeyup) skipNextCtrlVKeyup = false; + if (remoteDownKeys.has(e.keyCode)) sendKeyRaw(e.keyCode, false); + return; + } // 由 pasteToRemote 合成的完整 Ctrl+V 按下:物理 keyup 不再转发,避免 down/up 乱序 if (e.keyCode === 86 && skipNextCtrlVKeyup) { skipNextCtrlVKeyup = false; @@ -7278,6 +7416,26 @@ sendKey(e.keyCode, false, e.altKey); }); + // 失焦(切窗口、点地址栏/查找栏、开开发者工具)或标签页隐藏/关闭时, + // 浏览器不再派发 keyup;不补发的话远程会一直按着最后按下的修饰键。 + // 此时如果 socket 已断则补发不出去:保留 stale 等重连后再补。 + window.addEventListener('blur', function() { + // 物理 keyup 不会再来,Ctrl+V 桥的一次性标记也作废,否则会吞掉 + // 重连后下一次 V 的抬起,远程 V 一直按下。 + skipNextCtrlVKeyup = false; + if (controlEnabled) releaseRemoteKeysIfOnline(); + }); + document.addEventListener('visibilitychange', function() { + if (document.hidden) { + skipNextCtrlVKeyup = false; + releaseRemoteKeysIfOnline(); + } + }); + window.addEventListener('pagehide', function() { + skipNextCtrlVKeyup = false; + releaseRemoteKeysIfOnline(); + }); + // 字符 → Win32 VK 映射表(提到 listener 外避免每次输入重建)。 // US 键盘上"不需要 Shift"的 OEM 符号 ASCII 码 ≠ VK 码,必须显式映射。 const SHIFT_SYMBOLS = '~!@#$%^&*()_+{}|:"<>?'; @@ -7327,6 +7485,10 @@ }); function disconnect() { + // 先把还在"按下"状态的键补发 keyup:断开后远程再也收不到 keyup, + // 修饰键会一直按着(ws/currentDevice 必须还活着,故放在最前面)。 + releaseRemoteKeys(); + skipNextCtrlVKeyup = false; // 会话级标记,不能跨设备残留 // Reset throughput / resolution read-outs currentWidth = 0; currentHeight = 0; bwBytesAccum = 0; bwBytesPerSec = 0; @@ -7547,9 +7709,9 @@ // Input shortcut buttons event handlers // Shortcuts only visible when keyboard is open, so no extra check needed function sendShortcutKey(keyCode, isDown) { - if (ws && ws.readyState === WebSocket.OPEN && token) { - ws.send(JSON.stringify({ cmd: 'key', token, id: currentDeviceId(), keyCode, down: isDown, alt: false })); - } + // 与输入辅助栏同一条通道:统一走 sendKeyRaw(含按下状态跟踪); + // 成对发出,正常不会留下未抬起的键。 + sendKeyRaw(keyCode, isDown); } const shortcutBtns = document.querySelectorAll('.shortcut-btn'); shortcutBtns.forEach(function(btn) {