rdev icon indicating copy to clipboard operation
rdev copied to clipboard

tauri dev crashes whenever a key is pressed

Open jagzmz opened this issue 6 months ago • 12 comments
trafficstars

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_opener::init())
        .setup(|app| {

            // Start the input handler using our improved grabber
            let app_handle = app.handle().clone();

            let (tx, rx) = mpsc::channel();

            std::thread::spawn(move || {
                listen(move |event| {
                    tx.send(event).unwrap();
                })
                .unwrap();
            });

            std::thread::spawn(move || {
                let mut meta_pressed = false;
                
                while let Ok(event) = rx.recv() {
                    println!("Event: {:?}", event);
                    match event.event_type {
                        EventType::KeyPress(Key::MetaLeft) | EventType::KeyPress(Key::MetaRight) => {
                            meta_pressed = true;
                            println!("Meta key pressed");
                        }
                        EventType::KeyRelease(Key::MetaLeft) | EventType::KeyRelease(Key::MetaRight) => {
                            meta_pressed = false;
                            println!("Meta key released");
                        }
                        EventType::KeyPress(Key::KeyV) => {
                            println!("V key pressed");
                            if meta_pressed {
                                println!("Meta+V detected");
                                let window = app_handle.get_webview_window("main").unwrap();
                                let is_visible = window.is_visible().unwrap();
                                if is_visible {
                                    println!("Hiding window");
                                    window.hide().unwrap();
                                } else {
                                    println!("Showing window");
                                    window.show().unwrap();
                                    window.set_focus().unwrap();
                                }
                            }
                        }
                        _ => {}
                    }
                }
            });
            
            // println!("Initializing input handler...");
            
            // Now we directly use the grabber
            // input_handler::start_grabbing(app_handle);
            
            // Small delay to let the input thread initialize
            // std::thread::sleep(std::time::Duration::from_millis(500));
            // println!("Continuing with app startup after input thread initialized");

            Ok(())
        })
        .invoke_handler(tauri::generate_handler![greet, set_window_listening_state])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

As soon as i press any key on my MacOS keyboard, it exits with code 0 @Narsil , i am using latest from git url rdev = { git = "https://github.com/Narsil/rdev", branch = "main" }

jagzmz avatar May 21 '25 23:05 jagzmz

it works when i only run rust cargo run --manifest-path=src-tauri/Cargo.toml


Event: Event { time: SystemTime { tv_sec: 1747868655, tv_nsec: 709736000 }, name: None, event_type: MouseMove { x: 386.0546875, y: 671.4921875 } }
Event: Event { time: SystemTime { tv_sec: 1747868656, tv_nsec: 652507000 }, name: Some("s"), event_type: KeyPress(KeyS) }
sEvent: Event { time: SystemTime { tv_sec: 1747868656, tv_nsec: 756963000 }, name: None, event_type: KeyRelease(KeyS) }

jagzmz avatar May 21 '25 23:05 jagzmz

Does adding this:

#[cfg(target_os = "macos")]
set_is_main_thread(false);

Before the call to listen, help you?

AlexKnauth avatar May 22 '25 02:05 AlexKnauth

@jagzmz it's probably linked to the listener not being on the main thread.

I'm not sure how to fix it easily.

Also please, pin rdev to a specific revision in your Cargo.toml and not on main. Breaking main is totally normal for crates, as sometimes intermediary states are necessary for large changes that happen over a period of time.

Nothing should be horrifyingly broken, but still pinning a specific commit will save you some headache.

Narsil avatar May 22 '25 17:05 Narsil

I will check this out and get back to you in a day. Thanks!

jagzmz avatar May 22 '25 19:05 jagzmz

I encountered same issue too.

qzd1989 avatar May 23 '25 14:05 qzd1989

I will check this out and get back to you in a day. Thanks!

thank you. I'll wait

qzd1989 avatar May 23 '25 14:05 qzd1989

Does adding this:

#[cfg(target_os = "macos")] set_is_main_thread(false); Before the call to listen, help you?

where do you get this function? how to use it?

qzd1989 avatar May 23 '25 14:05 qzd1989

where do you get this function? how to use it?

The set_is_main_thread function was added in #147 to fix a crash of a tauri app using rdev running on Mac when a key is pressed.

The default value for is_main_thread is true, which is fine for most applications, but not fine for the way multi-threading works in tauri applications. If you're calling listen from a different thread from the main thread, as OP is, setting is_main_thread to false with set_is_main_thread(false), lets it know that it has to take measures to avoid calling a certain Mac function directly in that thread.

AlexKnauth avatar May 23 '25 21:05 AlexKnauth

where do you get this function? how to use it?

The set_is_main_thread function was added in #147 to fix a crash of a tauri app using rdev running on Mac when a key is pressed.

The default value for is_main_thread is true, which is fine for most applications, but not fine for the way multi-threading works in tauri applications. If you're calling listen from a different thread from the main thread, as OP is, setting is_main_thread to false with set_is_main_thread(false), lets it know that it has to take measures to avoid calling a certain Mac function directly in that thread.

Thank you so much — this is very helpful to me.

qzd1989 avatar May 24 '25 21:05 qzd1989

where do you get this function? how to use it?

The set_is_main_thread function was added in #147 to fix a crash of a tauri app using rdev running on Mac when a key is pressed.

The default value for is_main_thread is true, which is fine for most applications, but not fine for the way multi-threading works in tauri applications. If you're calling listen from a different thread from the main thread, as OP is, setting is_main_thread to false with set_is_main_thread(false), lets it know that it has to take measures to avoid calling a certain Mac function directly in that thread.

but I found rdev can't listen caps lock key on mac..

qzd1989 avatar May 25 '25 03:05 qzd1989

Does adding this:

#[cfg(target_os = "macos")] set_is_main_thread(false); Before the call to listen, help you?

@AlexKnauth This works.

@Narsil I don't think the updates on your main branch has landed on 0.5.3 because i wasn't able to see the function set_is_main_thread on that build. I am now using rdev = { git = "https://github.com/Narsil/rdev.git#c77b4e5456301cfa7f2226020f2a58f2f8d77c5d" }

jagzmz avatar May 25 '25 08:05 jagzmz

I tried when encountering this issue when working on https://github.com/moeru-ai/airi, it works. Or otherwise whenever I bring up the DevTools on macOS (of Safari) out, any next key press will cause the application fail silently without any backtrace, but the silent exit will not happen if DevTools wasn't opened, weird.

nekomeowww avatar Aug 03 '25 18:08 nekomeowww