You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[global-shortcut][v2] How to register a shortcut using tauri_plugin_global_shortcut #965
During the migration process, I found that the register function in the tauri_plugin_global_shortcut plug-in does not accept the handler parameter. How can I register a shortcut key?
Hi, I'm on v2.0, beta 0 for@tauri-apps/plugin-global-shortcut. When running an application with the below main(), I do see the "Setting up global shortcut" message printed, but I do not see the "Shortcut" println coming up on every hotkey-ish type of key press, and I do not (nor do I see the Ctrl-N println for that matter).
I'm running a NextJS/React app, if that is relevant. JavaScript registrations using register still work, so I could fall back to that if necessary.
What am I missing? Is there an additional init() like there is for other plugins?
emm, with all due respect, this design seems a bit counter-intuitive. Is this part of the api likely to change in the future?
Anything specific that triggers you? Or even better, any ideas how to improve it? Afaik we don't really have any changes planned, but we still can do it while it's still in beta.
@FabianLars@amrbashir Sorry, I just saw your reply, Thank you for your replies. What I am building now is an application that can run in the background, that is, it will still run in the background after closing the last window. This application allows users to customize a series of global shortcut keys that need to be bound. A rust function to perform some system-level operations, such as opening/closing the system proxy, so I cannot complete the shortcut key binding operation in the js code. However, based on the current interface design, I want to implement this in rust It seems to be very difficult to customize the shortcut key function. In my opinion, it would be great if the rust api of the shortcut key plug-in can be as concise and easy to use as the js api, just like the v1 version, at least js and rust can look at the code logic. It is a unified way of use.
The use cases I am talking about should be more common usage in desktop software, not special cases of individual software, so I hope you can take its application scenarios into consideration when designing the API.
I noticed that there is still an exposed register function, but it does not accept the handler parameter. I am confused about what this function is used for now.
I noticed that there is still an exposed register function, but it does not accept the handler parameter. I am confused about what this function is used for now.
The function is used to register a shortcut that will be catched by the handler passed to tauri_plugin_global_shortcut::Builder::with_handler
I noticed that there is still an exposed register function, but it does not accept the handler parameter. I am confused about what this function is used for now.
The function is used to register a shortcut that will be catched by the handler passed to tauri_plugin_global_shortcut::Builder::with_handler
So did you forget to call the register function in your example?
Thanks @amrbashir - getting closer :) If I try doing a doing an app.emit or app.emit_to from within the "with_handler", I get the following error, which is hitting the boundary of my meager (but growing) Rust knowledge. Can you give me an idea of what I'm doing wrong? Thanks!
@amrbashir - My Hero, thanks! I imagine doing an "emit" from the handler would be a pretty common use case, it may be worthwhile including that in the updated example, if you haven't already.
Activity
docs(global-shortcut): update examples in README.md closes #965
amrbashir commentedon Feb 19, 2024
I have updated the example, check it out here https://github.com/tauri-apps/plugins-workspace/tree/v2/plugins/global-shortcut#usage
MystiPanda commentedon Feb 19, 2024
emm, with all due respect, this design seems a bit counter-intuitive. Is this part of the api likely to change in the future?
jasonterando commentedon Feb 19, 2024
Hi, I'm on v2.0, beta 0 for@tauri-apps/plugin-global-shortcut. When running an application with the below main(), I do see the "Setting up global shortcut" message printed, but I do not see the "Shortcut" println coming up on every hotkey-ish type of key press, and I do not (nor do I see the Ctrl-N println for that matter).
I'm running a NextJS/React app, if that is relevant. JavaScript registrations using
registerstill work, so I could fall back to that if necessary.What am I missing? Is there an additional init() like there is for other plugins?
Thanks!
FabianLars commentedon Feb 19, 2024
Anything specific that triggers you? Or even better, any ideas how to improve it? Afaik we don't really have any changes planned, but we still can do it while it's still in beta.
amrbashir commentedon Feb 19, 2024
@jasonterando I have updated the example, I forgot to add the registering code
fn main() { tauri::Builder::default() .invoke_handler(tauri::generate_handler![open_workbook, save_workbook, run_request, cancel_request, clear_cached_authorization]) .plugin(tauri_plugin_fs::init()) .plugin(tauri_plugin_dialog::init()) .setup(|app| { #[cfg(desktop)] { println!("Setting up global shortcut"); - use tauri_plugin_global_shortcut::{Shortcut, Code, Modifiers}; + use tauri_plugin_global_shortcut::{Shortcut, Code, Modifiers, GlobalShortcutExt}; let ctrl_n_shortcut = Shortcut::new(Some(Modifiers::CONTROL), Code::KeyN); app.handle().plugin( tauri_plugin_global_shortcut::Builder::with_handler(move |app, shortcut| { println!("{:?}", shortcut); if shortcut == &ctrl_n_shortcut { println!("Ctrl-N Detected!"); app.emit_to(EventTarget::any(), "NEW", "").unwrap() } }) .build() )?; + app.global_shorcut().register(ctrl_n_shortcut )?; } Ok(()) }) .run(tauri::generate_context!()) .expect("error running app"); }amrbashir commentedon Feb 19, 2024
As fabian said, we are open to suggestions and nothing is set to stone just yet
MystiPanda commentedon Feb 19, 2024
@FabianLars @amrbashir Sorry, I just saw your reply, Thank you for your replies. What I am building now is an application that can run in the background, that is, it will still run in the background after closing the last window. This application allows users to customize a series of global shortcut keys that need to be bound. A rust function to perform some system-level operations, such as opening/closing the system proxy, so I cannot complete the shortcut key binding operation in the js code. However, based on the current interface design, I want to implement this in rust It seems to be very difficult to customize the shortcut key function. In my opinion, it would be great if the rust api of the shortcut key plug-in can be as concise and easy to use as the js api, just like the v1 version, at least js and rust can look at the code logic. It is a unified way of use.
The use cases I am talking about should be more common usage in desktop software, not special cases of individual software, so I hope you can take its application scenarios into consideration when designing the API.
amrbashir commentedon Feb 19, 2024
I think we can do something like this (while keeping the current API as well)
MystiPanda commentedon Feb 19, 2024
I noticed that there is still an exposed
registerfunction, but it does not accept the handler parameter. I am confused about what this function is used for now.MystiPanda commentedon Feb 19, 2024
This looks good for me.
amrbashir commentedon Feb 19, 2024
The function is used to register a shortcut that will be catched by the handler passed to
tauri_plugin_global_shortcut::Builder::with_handlerMystiPanda commentedon Feb 19, 2024
So did you forget to call the register function in your example?
amrbashir commentedon Feb 19, 2024
yeah, I updated the example.
MystiPanda commentedon Feb 19, 2024
Oh, I just saw your reply above. I have no other questions. The regiater_with_handler proposal you mentioned is great.
Thank you for your patience, and thank you for your hard work:)
jasonterando commentedon Feb 19, 2024
Thanks @amrbashir - getting closer :) If I try doing a doing an app.emit or app.emit_to from within the "with_handler", I get the following error, which is hitting the boundary of my meager (but growing) Rust knowledge. Can you give me an idea of what I'm doing wrong? Thanks!
amrbashir commentedon Feb 19, 2024
Appis notSendorSyncand you need to get a handle to it instead.setup(|app| { #[cfg(desktop)] { use tauri_plugin_global_shortcut::{Code, GlobalShortcutExt, Modifiers, Shortcut}; let ctrl_n_shortcut = Shortcut::new(Some(Modifiers::CONTROL), Code::KeyN); + let handle = app.handle().clone(); app.handle().plugin( tauri_plugin_global_shortcut::Builder::with_handler(move |_app, shortcut| { println!("{:?}", shortcut); if shortcut == &ctrl_n_shortcut { println!("Ctrl-N Detected!"); - app.emit("new", "").unwrap() + handle.emit("new", "").unwrap() // app.emit_to(EventTarget::any(), "new", "").unwrap() } }) .build(), )?; app.global_shortcut().register(ctrl_n_shortcut)?; }feat(global-shortcut): add `GlobalShortcutExt::register_with_handler`
amrbashir commentedon Feb 19, 2024
PR for the new API #969
jasonterando commentedon Feb 19, 2024
@amrbashir - My Hero, thanks! I imagine doing an "emit" from the handler would be a pretty common use case, it may be worthwhile including that in the updated example, if you haven't already.
amrbashir commentedon Feb 19, 2024
will do
docs(global-shortcut): update examples in README.md closes tauri-apps…
refactor!(global-shortcut): better APIs DX (#969)
refactor!(global-shortcut): better APIs DX (#969)