Repository navigation
feat(recording): show pressed keys on screen while a take is live #1633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
8833736
d263732
f7bac5e
0bef5ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { BrowserWindow, type Rectangle } from "electron"; | ||
| import { createKeystrokeOverlayWindow } from "./windows"; | ||
|
|
||
| /** | ||
| * Lifecycle of the keystroke overlay: one window, alive only while a take is live, | ||
| * bounded to the display being recorded. The window is destroyed on hide rather than | ||
| * cached — a hidden window would otherwise linger as the app's last window and block | ||
| * quit on Windows, the same way the countdown overlay's lingering is special-cased in | ||
| * `main.ts`. A take recreates it lazily, and the first badges may land during the | ||
| * ~0.2 s the renderer needs to load: acceptable for a hint overlay. | ||
| */ | ||
| let overlayWindow: BrowserWindow | null = null; | ||
|
|
||
| async function ensureOverlayWindow(): Promise<BrowserWindow | null> { | ||
| if (overlayWindow && !overlayWindow.isDestroyed()) { | ||
| return overlayWindow; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
| overlayWindow = createKeystrokeOverlayWindow(); | ||
| if (overlayWindow.isDestroyed()) { | ||
| overlayWindow = null; | ||
| return null; | ||
| } | ||
| // Wait for the first frame before showing, else Chromium flashes a black | ||
| // rectangle because it has not rendered any pixels yet. | ||
| if (overlayWindow.webContents.isLoading()) { | ||
| await new Promise<void>((resolve) => { | ||
| overlayWindow?.once("ready-to-show", resolve); | ||
| }); | ||
| if (!overlayWindow || overlayWindow.isDestroyed()) { | ||
| return null; | ||
| } | ||
| } | ||
| return overlayWindow; | ||
| } | ||
|
|
||
| export async function showKeystrokeOverlay(bounds: Rectangle): Promise<void> { | ||
| const win = await ensureOverlayWindow(); | ||
| if (!win || win.isDestroyed()) { | ||
| return; | ||
| } | ||
|
|
||
| win.setBounds(bounds); | ||
| if (!win.isVisible()) { | ||
| win.showInactive(); | ||
| } | ||
| } | ||
|
|
||
| export function hideKeystrokeOverlay(): void { | ||
| const win = overlayWindow; | ||
| overlayWindow = null; | ||
| if (win && !win.isDestroyed()) { | ||
| win.destroy(); | ||
| } | ||
| } | ||
|
|
||
| /** Forwarded live keydowns (`windowsNativeRecordingSession` → `handlers`). */ | ||
| export function broadcastKeystroke(vk: number): void { | ||
| const win = overlayWindow; | ||
| if (win && !win.isDestroyed() && win.isVisible()) { | ||
| win.webContents.send("keystroke-overlay-key", vk); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,18 @@ static std::atomic<int> g_leftUpCount{0}; | |
| static std::atomic<bool> g_stop{false}; | ||
| static std::mutex g_stdoutMtx; | ||
|
|
||
| // ───────────────────────────────────────────────────────────────────────────── | ||
| // Global keyboard-hook state: keydowns the on-screen keystroke overlay shows. | ||
| // The hook only queues — an LL hook must not block, so the sampling loop drains | ||
| // the queue and writes the JSONL lines. ponytail: the repeat filter is one | ||
| // atomic, so alternating two held keys re-queues a repeat of the first one; | ||
| // a per-key down set is the upgrade if that ever matters. | ||
| // ───────────────────────────────────────────────────────────────────────────── | ||
| static HHOOK g_keyHook = nullptr; | ||
| static std::mutex g_keyMtx; | ||
| static std::vector<DWORD> g_keyQueue; | ||
| static std::atomic<DWORD> g_lastKeyDown{0}; | ||
|
|
||
| static LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) { | ||
| if (nCode >= 0) { | ||
| if (wParam == WM_LBUTTONDOWN) g_leftDownCount.fetch_add(1, std::memory_order_relaxed); | ||
|
|
@@ -36,6 +48,23 @@ static LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lPara | |
| return CallNextHookEx(g_mouseHook, nCode, wParam, lParam); | ||
| } | ||
|
|
||
| static LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { | ||
| if (nCode >= 0) { | ||
| const KBDLLHOOKSTRUCT* info = reinterpret_cast<KBDLLHOOKSTRUCT*>(lParam); | ||
| if (info && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { | ||
| // The LL hook keeps firing while a key is held; the overlay wants one | ||
| // badge per press, so only a change of key passes the filter. | ||
| if (g_lastKeyDown.exchange(info->vkCode, std::memory_order_relaxed) != info->vkCode) { | ||
| std::lock_guard<std::mutex> lock(g_keyMtx); | ||
| g_keyQueue.push_back(info->vkCode); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Map side-specific modifier codes to the generic modifier labels. The hook forwards the physical virtual-key code unchanged. Left and right Ctrl, Alt, and Shift therefore arrive as Normalize each side-specific code to its generic modifier (
|
||
| } | ||
| } else if (info && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)) { | ||
| g_lastKeyDown.store(0, std::memory_order_relaxed); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve modifier state in the event stream. Win-down, A-down, Win-up and Win-down, Win-up, A-down produce the same transmitted keydowns. The correct badges differ:
|
||
| } | ||
| } | ||
| return CallNextHookEx(g_keyHook, nCode, wParam, lParam); | ||
| } | ||
|
|
||
| // ───────────────────────────────────────────────────────────────────────────── | ||
| // Utilities | ||
| // ───────────────────────────────────────────────────────────────────────────── | ||
|
|
@@ -322,6 +351,22 @@ static void runSamplingLoop(int intervalMs, HWND targetWindow, const CLSID& pngC | |
| const int downCount = g_leftDownCount.exchange(0, std::memory_order_relaxed); | ||
| const int upCount = g_leftUpCount.exchange(0, std::memory_order_relaxed); | ||
|
|
||
| // Drain the keyboard hook's queue: one JSONL line per keydown, numeric | ||
| // fields only, so nothing needs escaping. Keyups carry no information the | ||
| // overlay shows, so they are never emitted. | ||
| std::vector<DWORD> keys; | ||
| { | ||
| std::lock_guard<std::mutex> lock(g_keyMtx); | ||
| keys.swap(g_keyQueue); | ||
| } | ||
| for (const DWORD vk : keys) { | ||
| char keyBuf[96]; | ||
| std::snprintf(keyBuf, sizeof(keyBuf), | ||
| "{\"type\":\"key\",\"timestampMs\":%" PRId64 ",\"vk\":%lu}", | ||
| nowMs(), static_cast<unsigned long>(vk)); | ||
| writeJsonLine(keyBuf); | ||
| } | ||
|
|
||
| CURSORINFO ci{}; | ||
| ci.cbSize = sizeof(ci); | ||
| if (!GetCursorInfo(&ci)) { | ||
|
|
@@ -474,6 +519,13 @@ int main(int argc, char* argv[]) { | |
| return 1; | ||
| } | ||
|
|
||
| // The keyboard hook feeds the keystroke overlay only, so its failure costs | ||
| // nothing the recording needs — warn and keep going rather than aborting. | ||
| g_keyHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, GetModuleHandle(nullptr), 0); | ||
| if (!g_keyHook) { | ||
| std::cerr << "SetWindowsHookEx(WH_KEYBOARD_LL) failed; no keystroke overlay" << std::endl; | ||
| } | ||
|
|
||
| // Prime GetAsyncKeyState so the first poll doesn't return stale "since-last-call" bits | ||
| GetAsyncKeyState(VK_LBUTTON); | ||
|
|
||
|
|
@@ -499,6 +551,7 @@ int main(int argc, char* argv[]) { | |
| g_stop.store(true, std::memory_order_relaxed); | ||
| if (sampler.joinable()) sampler.join(); | ||
| UnhookWindowsHookEx(g_mouseHook); | ||
| if (g_keyHook) UnhookWindowsHookEx(g_keyHook); | ||
| Gdiplus::GdiplusShutdown(gdipToken); | ||
| return 0; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.