fix(desktop): 文字注入崩溃——粘贴键用物理键码避开主线程限制的布局查询
enigo 的 Key::Unicode('v') 会调 TSMGetInputSourceProperty 查键盘布局,该 API 必须主线程执行;注入跑在 tokio 后台线程上,macOS 15/26 收紧主线程校验后触发断言崩溃(松开说话、识别成功后注入即崩)。改用 kVK_ANSI_V 物理键码 Key::Other(9),走 CGEvent 无需布局查询,与键盘布局无关。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -57,10 +57,18 @@ pub fn inject_text(text: &str) -> Result<(), String> {
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
let modifier = Key::Control;
|
||||
|
||||
// 粘贴键 V:
|
||||
// macOS 用物理键码 9(kVK_ANSI_V),直接走 CGEvent,避开 enigo 对
|
||||
// Key::Unicode 的键盘布局查询——那条路径调 TSMGetInputSourceProperty,
|
||||
// 该 API 必须在主线程执行,在 tokio 后台线程上调用会触发断言崩溃
|
||||
// (macOS 15/26 收紧了主线程校验)。物理键码与布局无关,粘贴恒为此键位。
|
||||
#[cfg(target_os = "macos")]
|
||||
let paste_key = Key::Other(9);
|
||||
#[cfg(not(target_os = "macos"))]
|
||||
let paste_key = Key::Unicode('v');
|
||||
|
||||
enigo.key(modifier, Direction::Press).map_err(|e| e.to_string())?;
|
||||
enigo
|
||||
.key(Key::Unicode('v'), Direction::Click)
|
||||
.map_err(|e| e.to_string())?;
|
||||
enigo.key(paste_key, Direction::Click).map_err(|e| e.to_string())?;
|
||||
enigo
|
||||
.key(modifier, Direction::Release)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
Reference in New Issue
Block a user