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

+49 -2
View File
@@ -10,6 +10,7 @@
#include <sys/stat.h>
#include <thread>
#include <mutex>
#include <set>
#include <atomic>
#include <vector>
#include <cstring>
@@ -420,6 +421,27 @@ public:
m_captureThread.join();
}
}
// 会话结束:控制端不会再来 keyup,把还按着的键松开(修饰键最后放),
// 否则 X 服务器一直认为 Ctrl/Shift/Alt 按下,表现为"远程打字很怪"。
if (m_inputDisplay && m_x11.pXTestFakeKeyEvent) {
std::set<unsigned int> keys, modifierKeys;
{
std::lock_guard<std::mutex> 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<std::mutex> 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<unsigned int> m_injectedKeys; // 注入后未收到 keyup 的 X keycode
std::set<unsigned int> m_injectedModifierKeys; // 其中属于修饰键的部分(最后释放)
std::mutex m_injectedKeysMutex; // OnReceive 回调与析构可能并发
IOCPClient* m_client;
std::atomic<bool> m_running;
std::atomic<bool> m_destroyed; // Flag to prevent Start() after destruction begins