Skip to main content

Overview

Text operation IPC methods handle inserting, replacing, and managing text in external applications. These methods communicate with the text handler service to send text to the last active window.

Methods

replaceText

Replaces the selected text in the target application with the provided text.
text
string
required
The text to insert into the target application
Returns: void (one-way message) Implementation: frontend/electron/src/ipc/text-handlers.ts:6
// Replace selected text with AI-generated content
const generatedText = "Hello, world!";
window.electron.replaceText(generatedText);

Behavior

  1. Hides the main menu window
  2. Waits 100ms for window transition
  3. Sends text to the last active window using the configured output mode
  4. Updates clipboard state tracking

Output Modes

The text insertion method depends on the current textOutputMode setting:
  • paste - Uses clipboard paste (Ctrl+V)
  • typewriter - Simulates human typing with realistic delays
  • typewriter-leetcode - Typing mode optimized for code editors with auto-indent handling
Set the output mode using setTextOutputMode().

acceptSuggestion

Accepts an inline suggestion and inserts it into the target application.
text
string
required
The suggestion text to accept and insert
Returns: void (one-way message) Implementation: frontend/electron/src/ipc/text-handlers.ts:24
// Accept a suggestion from the ghost text overlay
const suggestion = "const result = data.map(item => item.value);";
window.electron.acceptSuggestion(suggestion);

Behavior

  1. Hides the suggestion window
  2. Waits 100ms for window transition
  3. Sends text to the last active window
  4. Updates clipboard state tracking

dismissSuggestion

Dismisses the current suggestion without inserting any text. Parameters: None Returns: void (one-way message) Implementation: frontend/electron/src/ipc/text-handlers.ts:42
// User rejected the suggestion
window.electron.dismissSuggestion();

Behavior

Simply hides the suggestion window without any text manipulation.

Text Output Modes

getTextOutputMode

Retrieves the current text output mode. Parameters: None Returns: Promise<"paste" | "typewriter" | "typewriter-leetcode"> Implementation: frontend/electron/src/ipc/settings-handlers.ts:27
const mode = await window.electron.getTextOutputMode();
console.log("Current mode:", mode); // "paste", "typewriter", or "typewriter-leetcode"

setTextOutputMode

Sets the text output mode for text insertion operations.
mode
'paste' | 'typewriter' | 'typewriter-leetcode'
required
The text output mode to use
  • paste - Fast clipboard-based insertion
  • typewriter - Human-like typing with realistic delays and occasional typos
  • typewriter-leetcode - Code editor optimized typing with auto-indent handling
Returns: void (one-way message) Implementation: frontend/electron/src/ipc/settings-handlers.ts:29
// Use paste mode for fastest insertion
window.electron.setTextOutputMode("paste");

// Use typewriter mode for human-like typing
window.electron.setTextOutputMode("typewriter");

// Use leetcode mode for code editors
window.electron.setTextOutputMode("typewriter-leetcode");

Typewriter Mode Details

The typewriter modes simulate human typing behavior to avoid detection by anti-cheating systems.

Features

  • Realistic delays - Variable delay between keystrokes (20-100ms)
  • Typos - 3% chance of typing a neighboring key, then correcting it
  • Punctuation pauses - Longer delays after punctuation marks (150-400ms)
  • Word boundaries - Slight pause after spaces (30-100ms)

Simple Typewriter

Used for basic text editors like Notepad:
window.electron.setTextOutputMode("typewriter");
window.electron.replaceText("This will be typed character by character");
Source: frontend/electron/src/services/text-handler.ts:153

LeetCode Typewriter

Optimized for code editors with auto-indent:
window.electron.setTextOutputMode("typewriter-leetcode");
window.electron.replaceText(`function solve(nums) {
  return nums.filter(x => x > 0);
}`);
This mode:
  1. Types each line separately
  2. Clears auto-indented whitespace before typing
  3. Preserves exact indentation from source code
Source: frontend/electron/src/services/text-handler.ts:218

Event Listeners

onShowMenu

Called when the main menu should be displayed with selected text.
callback
(text: string) => void
required
Function to call when the menu is shown
  • text - The currently selected text in the target application
Returns: () => void - Cleanup function to remove the listener Implementation: frontend/electron/src/preload.ts:4
useEffect(() => {
  const cleanup = window.electron.onShowMenu((selectedText: string) => {
    setContext(selectedText);
    setVisible(true);
  });

  return cleanup; // Remove listener on unmount
}, []);

onShowSuggestion

Called when an inline suggestion should be displayed.
callback
(data: { context: string }) => void
required
Function to call when a suggestion is available
  • data.context - The context text around the cursor position
Returns: () => void - Cleanup function to remove the listener Implementation: frontend/electron/src/preload.ts:17
useEffect(() => {
  const cleanup = window.electron.onShowSuggestion((data) => {
    console.log("Context:", data.context);
    // Fetch AI suggestion based on context
  });

  return cleanup;
}, []);

Build docs developers (and LLMs) love