#Tauri V1 - registering global shortcut from a global shortcut press freezes the app

6 messages · Page 1 of 1 (latest)

simple notch
#

Hello,
I've made the following function which is a simplification of my app problem :

fn hotkeys(app: tauri::AppHandle){
    println!("register Insert key");
    let mut shortcuts = app.global_shortcut_manager();
    shortcuts.register("Insert", {
        let app = app.clone();
        move || {
            let mut shortcuts = app.global_shortcut_manager();

            println!("register Esc key");
            shortcuts.register("Esc", || {}).expect("ERROR register Esc key");

            println!("After register Esc key");
        }
    }).expect("ERROR register Insert key");
}

The problem is that this script makes the app freeze when I press the "Insert" key.
Logs print "register Esc key" and then nothing. It doesn't reach "ERROR register Esc key" and "After register Esc key" neither. The window of my app gets stuck too.
May anyone explain me why this behaviour and give me a solution ?
Thanks !

lavish fog
#

Hmm, can you try it like this? ```rs
fn hotkeys(app: tauri::AppHandle){
println!("register Insert key");
let mut shortcuts = app.global_shortcut_manager();
shortcuts.register("Insert", {
let app = app.clone();
move || {
std::thread::spawn(move || {
let mut shortcuts = app.global_shortcut_manager();

        println!("register Esc key");
        shortcuts.register("Esc", || {}).expect("ERROR register Esc key");

        println!("After register Esc key");
      });
    }
}).expect("ERROR register Insert key");

}

#

If it still freezes i'd appreciate it if you could open a github issue for this instead since it seems like a bug then.

simple notch
#

I had to add "let app = app.clone();" before the line of "std::thread" but it works !
I understand that a new thread is required, but could you give me a quick explanation about why ? 🙂

lavish fog
#

Well, not 100% sure either. The event listeners run on the main thread so i assume the listener and the registration somehow deadlock each other in this scenario.

#

What makes me unsure as to what exactly happens is that iirc we still call the registration on the main thread internally so i don't really see how the spawned thread helps here. But really not familiar with that code base so idk