Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions electron/electron-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,21 @@ interface Window {
message?: string;
error?: string;
}>;
getKeystrokeTelemetry: (videoPath?: string) => Promise<{
success: boolean;
samples: KeystrokeSample[];
message?: string;
error?: string;
}>;
setKeystrokeTelemetry: (
videoPath: string | undefined,
samples: KeystrokeSample[],
) => Promise<{
success: boolean;
samples: KeystrokeSample[];
message?: string;
error?: string;
}>;
getSystemCursorAssets: () => Promise<{
success: boolean;
cursors: Record<string, SystemCursorAsset>;
Expand Down Expand Up @@ -631,15 +646,25 @@ interface Window {
success: boolean;
trusted: boolean;
prompted: boolean;
clientName?: string;
error?: string;
}>;
requestKeystrokeCapturePermission: () => Promise<{
success: boolean;
trusted: boolean;
tapOk?: boolean;
clientName?: string;
error?: string;
}>;
stopKeystrokeTap: () => Promise<{ success: boolean }>;
getScreenRecordingPermissionStatus: () => Promise<{
success: boolean;
status: string;
error?: string;
}>;
openScreenRecordingPreferences: () => Promise<{ success: boolean; error?: string }>;
openAccessibilityPreferences: () => Promise<{ success: boolean; error?: string }>;
openInputMonitoringPreferences: () => Promise<{ success: boolean; error?: string }>;
saveExportedVideo: (
videoData: ArrayBuffer,
fileName: string,
Expand Down Expand Up @@ -969,6 +994,17 @@ interface CursorTelemetryPoint {
| "not-allowed";
}

interface KeystrokeSample {
timeMs: number;
key: string;
code: string;
ctrl: boolean;
alt: boolean;
shift: boolean;
meta: boolean;
repeat?: boolean;
}

interface SystemCursorAsset {
dataUrl: string;
hotspotX: number;
Expand Down
14 changes: 14 additions & 0 deletions electron/ipc/cursor/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
hasLoggedInteractionHookFailure,
interactionCaptureCleanup,
isCursorCaptureActive,
isKeystrokeCaptureEnabled,
lastLeftClick,
setHasLoggedInteractionHookFailure,
setInteractionCaptureCleanup,
Expand All @@ -24,6 +25,7 @@ import {
isCursorCapturePaused,
pushCursorSample,
} from "./telemetry";
import { recordKeystrokeFromHookEvent } from "./keystrokes";

const nodeRequire = createRequire(import.meta.url);

Expand Down Expand Up @@ -289,10 +291,20 @@ export async function startInteractionCapture() {
setLinuxCursorScreenPoint({ x: point.x, y: point.y, updatedAt: Date.now() });
};

const onKeyDown = (event: HookMouseEvent) => {
if (process.platform !== "linux" || !isKeystrokeCaptureEnabled) {
return;
}
recordKeystrokeFromHookEvent(event);
};

hook.on("mousedown", onMouseDown);
hook.on("mouseup", onMouseUp);
if (process.platform === "linux") {
hook.on("mousemove", onMouseMove);
if (isKeystrokeCaptureEnabled) {
hook.on("keydown", onKeyDown);
}
}

setInteractionCaptureCleanup(() => {
Expand All @@ -302,12 +314,14 @@ export async function startInteractionCapture() {
hook.off("mouseup", onMouseUp);
if (process.platform === "linux") {
hook.off("mousemove", onMouseMove);
hook.off("keydown", onKeyDown);
}
} else if (typeof hook.removeListener === "function") {
hook.removeListener("mousedown", onMouseDown);
hook.removeListener("mouseup", onMouseUp);
if (process.platform === "linux") {
hook.removeListener("mousemove", onMouseMove);
hook.removeListener("keydown", onKeyDown);
}
}
} catch {
Expand Down
68 changes: 68 additions & 0 deletions electron/ipc/cursor/keystrokes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { describe, expect, it } from "vitest";
import { normalizeKeystrokeSamples, writeKeystrokeTelemetry } from "./keystrokes";
import { beforeEach, vi } from "vitest";

const { writeFile, rm } = vi.hoisted(() => ({
writeFile: vi.fn(),
rm: vi.fn(),
}));

vi.mock("node:fs/promises", () => ({
default: {
writeFile,
rm,
},
}));

vi.mock("electron", () => ({
app: {
getPath: vi.fn(() => "/tmp"),
},
}));

vi.mock("../utils", () => ({
getKeystrokePathForVideo: vi.fn(() => "/tmp/recording.keys.json"),
}));

vi.mock("./telemetry", () => ({
getCursorCaptureElapsedMs: vi.fn(() => 0),
isCursorCapturePaused: vi.fn(() => false),
}));

vi.mock("../state", () => ({
activeKeystrokeSamples: [],
pendingKeystrokeSamples: [],
isCursorCaptureActive: false,
isKeystrokeCaptureEnabled: false,
setActiveKeystrokeSamples: vi.fn(),
setIsKeystrokeCaptureEnabled: vi.fn(),
setPendingKeystrokeSamples: vi.fn(),
}));

vi.mock("../../appSettingsStore", () => ({
readAppSetting: vi.fn(() => false),
}));

describe("keystroke sidecar", () => {
beforeEach(() => {
writeFile.mockReset();
rm.mockReset();
});

it("writes normalized samples", async () => {
const samples = await writeKeystrokeTelemetry("/tmp/recording.mp4", [
{ timeMs: 12, key: "Enter", ctrl: false },
]);
expect(samples[0]?.key).toBe("Enter");
expect(writeFile).toHaveBeenCalled();
});

it("removes the sidecar when empty", async () => {
await writeKeystrokeTelemetry("/tmp/recording.mp4", []);
expect(rm).toHaveBeenCalledWith("/tmp/recording.keys.json", { force: true });
});

it("normalizes unknown payloads", () => {
expect(normalizeKeystrokeSamples("nope")).toEqual([]);
});
});
Loading