Skip to content

feat: keyboard type --strategy pid — background Unicode input - #2

Open
maxesisnclaw wants to merge 1 commit into
andelf:mainfrom
maxesisnclaw:feat/keyboard-type-pid
Open

feat: keyboard type --strategy pid — background Unicode input#2
maxesisnclaw wants to merge 1 commit into
andelf:mainfrom
maxesisnclaw:feat/keyboard-type-pid

Conversation

@maxesisnclaw

Copy link
Copy Markdown

Motivation. press --strategy pid already delivers single keys to a background app via CGEventPostToPid. keyboard type was missing the same path; it only had the global HID route, which usually requires foregrounding the target before typing — disruptive for automation that wants to stay invisible.

Implementation. Adds input::type_text_bg(pid, &str) — a direct mirror of type_text substituting CGEvent::post_to_pid for CGEvent::post. Same 20-UTF-16-codeunit chunking, same per-chunk sleeps; pacing identical to the global path so timing-sensitive apps see no difference.

CLI: keyboard type <TEXT> --strategy hid|pid. Default hid (backward-compatible). Reuses the existing PressStrategy enum (consistent with the press subcommand). --strategy pid requires --app / --pid at the top level.

Validated on TextEdit with the app fully occluded behind another window. Unicode (including CJK characters) delivered; target never activated.

AI assistance. Implementation drafted with Claude (Opus 4.7) assistance, reviewed and tested by hand.


Companion PR: #1 (click-xy / dblclick-xy). These two land independently; this one is a smaller diff and can be reviewed first if preferred.

Adds 'type_text_bg(pid, text)' in src/input.rs — mirrors 'type_text'
but uses CGEventPostToPid instead of global HID, so Unicode strings
are delivered to the target process's first responder without activation
or focus steal.  Same chunking (20 UTF-16 code units) and pacing as the
global path; Unicode works because CGEventKeyboardSetUnicodeString feeds
UTF-16 directly, bypassing the keycode table.

CLI surface (src/main.rs):
  - 'keyboard type' subcommand grows a '--strategy <hid|pid>' flag
    (reusing the existing PressStrategy enum for symmetry with 'press').
  - Default remains 'hid' for backward compat; 'pid' requires '--app'
    or '--pid' at the top level.
  - Updated 'Keyboard' command docstring; the subcommand is no longer
    purely global when --strategy pid is in use.

Motivation: ETF dry-run automation against custom-rendered trading
clients needs to type 3-5 strings per session (codes, prices, qty).
The previous flow had to activate the target app before each typed
string, which is intrusive and visibly steals focus from the user's
foreground work.  With --strategy pid the entire session runs while
another app stays frontmost.

Verified on TextEdit: 'hello from bg path 中文测试' delivered fully
background, Brave Browser stayed frontmost throughout.

Implementation drafted with AI assistance, manually reviewed and tested.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant