tauri: [bug] use `enigo` in tauri cause app crashed

Describe the bug

I try to use enigo to do some keyboard event mock in my tauri app, and it will crash when the macOS api TISCopyCurrentKeyboardInputSource or TISGetInputSourceProperty execute in enigo.

I start an issue in enigo before, but we found the crash will only appear in my Tauri app.

Reproduction

  1. register a shortcut
import { isRegistered, register, unregister } from '@tauri-apps/api/globalShortcut';
export async function registerShortcut(shortcut: string, callback: (shortcut?: string) => void) {
  await register(shortcut, (shortcut) => {
    callback(shortcut);
  });
}

and there is a log in shell:

register Accelerator { id: Some(AcceleratorId(29420)), mods: ALT, key: KeyS }
  1. write a tauri command, and invoke in javascript
#[tauri::command]
async fn paste_content(window: tauri::Window) {
    let mut enigo = Enigo::new();
    let trusted = accessibility::query_accessibility_permissions();
    if trusted {
        enigo.key_down(Key::Meta);
        enigo.key_click(Key::Layout('v'));
        enigo.key_up(Key::Meta);

    }
}
invoke("paste_content").then(async _ => {
        
 })

I use accessibility to help me request the macOS accessibility permission, and it returns true. 3. the application crashed

Expected behavior

The app does not crash.

Platform and versions

Environment
  › OS: Mac OS 13.0.0 X64
  › Node.js: 16.14.0
  › npm: 8.3.1
  › pnpm: 7.9.5
  › yarn: 3.1.1
  › rustup: 1.25.2
  › rustc: 1.68.0
  › cargo: 1.68.0
  › Rust toolchain: stable-aarch64-apple-darwin 

Packages
  › @tauri-apps/cli [NPM]: 1.2.3
  › @tauri-apps/api [NPM]: 1.2.0
  › tauri [RUST]: 1.2.4,
  › tauri-build [RUST]: 1.2.1,
  › tao [RUST]: 0.15.8,
  › wry [RUST]: 0.23.4,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../dist
  › devPath: http://localhost:1420/
  › framework: React
  › bundler: Vite

App directory structure
  ├─ dist
  ├─ node_modules
  ├─ public
  ├─ .github
  ├─ src-tauri
  ├─ api
  ├─ .git
  ├─ .vscode
  ├─ assets
  └─ src

Stack trace

There is no output in my shell when it crashed, I try to debug it and set some breakpoints, I put the screenshots [here](https://github.com/enigo-rs/enigo/pull/161#issuecomment-1460236620)

Additional context

No response

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 6
  • Comments: 15 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Have the same problem, though the app doesn’t crash in my case

For me, this helped:

// this works, but blocks the main thread while sleeping
#[tauri::command]
fn this_works() {
    use enigo::Keyboard;

    let mut enigo = enigo::Enigo::new(&enigo::Settings::default()).unwrap();
    enigo.key(enigo::Key::Unicode('a'), enigo::Direction::Click).unwrap();
    std::thread::sleep(std::time::Duration::from_secs(5));
    enigo.key(enigo::Key::Unicode('b'), enigo::Direction::Click).unwrap();
}

#[tauri::command]
fn this_crashes() {
    std::thread::spawn(|| {
        use enigo::Keyboard;

        let mut enigo = enigo::Enigo::new(&enigo::Settings::default()).unwrap();
        enigo.key(enigo::Key::Unicode('a'), enigo::Direction::Click).unwrap();
        std::thread::sleep(std::time::Duration::from_secs(5));
        enigo.key(enigo::Key::Unicode('b'), enigo::Direction::Click).unwrap();
    });
}

// this works and doesn't block the main thread while sleeping
#[tauri::command]
fn this_also_works(app: AppHandle) {
    std::thread::spawn(move || {
        use enigo::Keyboard;

        app.run_on_main_thread(|| {
            let mut enigo = enigo::Enigo::new(&enigo::Settings::default()).unwrap();
            enigo.key(enigo::Key::Unicode('a'), enigo::Direction::Click).unwrap();
        }).unwrap();
        std::thread::sleep(std::time::Duration::from_secs(5));
        app.run_on_main_thread(|| {
            let mut enigo = enigo::Enigo::new(&enigo::Settings::default()).unwrap();
            enigo.key(enigo::Key::Unicode('b'), enigo::Direction::Click).unwrap();
        }).unwrap();
    });
}