feat: keyboard type --strategy pid — background Unicode input - #2
Open
maxesisnclaw wants to merge 1 commit into
Open
feat: keyboard type --strategy pid — background Unicode input#2maxesisnclaw wants to merge 1 commit into
maxesisnclaw wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation.
press --strategy pidalready delivers single keys to a background app viaCGEventPostToPid.keyboard typewas 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 oftype_textsubstitutingCGEvent::post_to_pidforCGEvent::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. Defaulthid(backward-compatible). Reuses the existingPressStrategyenum (consistent with thepresssubcommand).--strategy pidrequires--app/--pidat 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.