Fix: Release remotely injected keys the controller never lifted

A browser or native controller can lose a keyup (window blur, tab switch,
browser shortcut, in-app dialog, disconnect). The remote OS then keeps
Ctrl/Shift/Alt down, later keystrokes turn into shortcuts, and only
pressing the modifier again clears it.

The web console now tracks the keys it sent down and releases them on
blur, visibilitychange, pagehide, dialog, control off, device switch and
disconnect; the next keydown/mousedown/wheel reconciles tracked modifiers
against the event's real modifier state. Keys held when the socket drops
are stashed and released once the screen session is confirmed after
reconnect, tagged with the device so they never reach another host. The
Ctrl+V bridge flag is reset wherever its physical keyup can be lost.

The Windows client tracks injected keys by VK + scan code + extended bit
so right-hand modifiers release the key that was pressed, and releases
them on session teardown and reconnect; the Linux agent does the same
on teardown with XTestFakeKeyEvent.

Verified with a 20-scenario headless Chrome harness, group filter
regressions, node --check, go build/vet/test, a Debug client build and
the WSL Linux build. Not exercised against a real controller and host;
the macOS agent is unchanged.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-09 18:11:37 +01:00
1 parent 6fd6e9cbaa
commit 5bb24d714a
4 files changed
+303 -11

No files matched your search

+74 -2
View File
@@ -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<std::mutex> lock(m_InjectedKeysMutex);
if (down) m_InjectedKeys.insert(keyId);
else m_InjectedKeys.erase(keyId);
}
// 补发所有未收到 keyup 的按键:先普通键再修饰键。主控端丢 keyup 或直接断开时
// 调用,避免远程系统把 Ctrl/Shift/Alt 一直当成按下("打字很怪"的根因)。
void CScreenManager::ReleaseInjectedKeys()
{
std::set<DWORD> keys;
{
std::lock_guard<std::mutex> 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;
}
}