shortcut.rs 768 B

12345678910111213141516171819202122232425
  1. //! Add global shortcuts to your app while a component is active
  2. //!
  3. //! This demo shows how to add a global shortcut to your app that toggles a signal. You could use this to implement
  4. //! a raycast-type app, or to add a global shortcut to your app that toggles a component on and off.
  5. //!
  6. //! These are *global* shortcuts, so they will work even if your app is not in focus.
  7. use dioxus::desktop::{use_global_shortcut, HotKeyState};
  8. use dioxus::prelude::*;
  9. fn main() {
  10. dioxus::LaunchBuilder::desktop().launch(app);
  11. }
  12. fn app() -> Element {
  13. let mut toggled = use_signal(|| false);
  14. _ = use_global_shortcut("ctrl+s", move |state| {
  15. if state == HotKeyState::Pressed {
  16. toggled.toggle();
  17. }
  18. });
  19. rsx!("toggle: {toggled}")
  20. }