WPF

Pasting text into another Windows window: Win32 lessons from Voice Input

Habr C# (ru) pavel-logachev
WPF

Voice Input is an open-source C# app that turns speech into text and types it wherever the cursor sits. The hard part is not recognition but handing a string to a foreign window without stealing focus, losing the clipboard or racing a late Esc.

Key points

  • The target HWND and process ID are captured on the hotkey press, not when the model returns.
  • The recording overlay uses WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW and WS_EX_TRANSPARENT so it never takes focus.
  • Classic Edit and RichEdit fields get one EM_REPLACESEL message via SendMessageTimeout with SMTO_ABORTIFHUNG.
  • Everything else goes through the clipboard plus Ctrl+V, with a VoiceInput.ClipboardGuard marker deciding whether to restore.
  • Clipboard access retries up to eight times with 20 ms pauses because WPF throws ExternalException when another process holds it.
  • A generation counter stops a queued Esc from a previous dictation cancelling the next one.

Real-world use

  • Dictating into a chat app, email client, browser or code editor at the current cursor position
  • Typing directly into WinForms text fields that expose a classic Edit class name
  • Pasting into Electron, Office and browser inputs that have no standard edit control
  • Running an end-to-end dictation test against a real WinForms window on the desktop

This summary was generated by the agent from the original post on Habr C# (ru). The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message