HotkeyManager

Struct HotkeyManager 

Source
pub struct HotkeyManager<T: 'static> { /* private fields */ }

Implementations§

Source§

impl<T: 'static> HotkeyManager<T>

Source

pub fn set_no_repeat(&mut self, no_repeat: bool)

Enable or disable the automatically applied ModKey::NoRepeat modifier. By default, this option is set to true which causes all hotkey registration calls to add the NoRepeat modifier, thereby disabling automatic retriggers of hotkeys when holding down the keys.

When this option is disabled, the ModKey::NoRepeat can still be manually added while registering hotkeys.

Note: Setting this flag doesn’t change previously registered hotkeys. It only applies to registrations performed after calling this function.

Examples found in repository?
examples/no_repeat.rs (line 15)
6fn main() {
7    // Create a HotkeyManager.
8    // By default, the hotkey registration will add the NoRepeat modifier. This causes the callback
9    // to only be triggered once, when the combination is held down.
10    let mut hkm = windows_hotkeys::threadsafe::HotkeyManager::new();
11
12    // Disable automatically applying the NoRepeat modifier. After this call, all registrations
13    // will trigger repeatedly when the hotkey is held down. This behavior can be manually changed
14    // for each registration by adding the `ModKey::NoRepeat` modifier.
15    hkm.set_no_repeat(false);
16
17    // Register a system-wide hotkey with the main key `A` and the modifier key `ALT`
18    //
19    // NOTE: This will trigger multiple times when the combination is held down
20    hkm.register(VKey::A, &[ModKey::Alt], || {
21        println!("Hotkey ALT + A was pressed");
22    })
23    .unwrap();
24
25    // Register a system-wide hotkey with the main key `B` and multiple modifier keys
26    // (`CTRL` + `ALT`)
27    //
28    // NOTE: This will only be triggered once and not repeatedly when being held down, since the
29    //       NoRepeat modifier is added manually.
30    hkm.register(
31        VKey::B,
32        &[ModKey::Ctrl, ModKey::Alt, ModKey::NoRepeat],
33        || {
34            println!("Hotkey CTRL + ALT + B was pressed");
35        },
36    )
37    .unwrap();
38
39    // Register a system-wide hotkey for `ALT` + `B` with extra keys `Left` + `Right`. This will
40    // trigger only if the `Left` + `Right` keys are also pressed during `ALT` + `B`. So just
41    // pressing `ALT` + `B` alone won't execute the closure
42    //
43    // NOTE: This will trigger multiple times when the combination is held down
44    hkm.register_extrakeys(VKey::B, &[ModKey::Alt], &[VKey::Left, VKey::Right], || {
45        println!("Hotkey ALT + B + Left + Right was pressed");
46    })
47    .unwrap();
48
49    // Run the event handler in a blocking loop. This will block forever and execute the set
50    // callbacks when registered hotkeys are detected
51    hkm.event_loop();
52}

Trait Implementations§

Source§

impl<T> Drop for HotkeyManager<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: 'static + Send> HotkeyManagerImpl<T> for HotkeyManager<T>

Source§

fn new() -> Self

Source§

fn register( &mut self, key: VKey, key_modifiers: &[ModKey], callback: impl Fn() -> T + Send + 'static, ) -> Result<HotkeyId, HkError>

Same as register_extrakeys but without extra keys. Read more
Source§

fn register_extrakeys( &mut self, key: VKey, key_modifiers: &[ModKey], extra_keys: &[VKey], callback: impl Fn() -> T + Send + 'static, ) -> Result<HotkeyId, HkError>

Register a new hotkey with additional required extra keys. Read more
Source§

fn unregister(&mut self, id: HotkeyId) -> Result<(), HkError>

Unregister a hotkey. This will prevent the hotkey from being triggered in the future. Read more
Source§

fn unregister_all(&mut self) -> Result<(), HkError>

Unregister all registered hotkeys. This will be called automatically when dropping the HotkeyManager instance. Read more
Source§

fn handle_hotkey(&self) -> Option<T>

Wait for a single a hotkey event and execute the callback if all keys match. This returns the callback result if it was not interrupted. The function call will block until a hotkey is triggered or it is interrupted. Read more
Source§

fn event_loop(&self)

Run the event loop, listening for hotkeys. This will run indefinitely until interrupted and execute any hotkeys registered before.
Source§

fn interrupt_handle(&self) -> InterruptHandle

Get an InterruptHandle for this HotkeyManager that can be used to interrupt the event loop.

Auto Trait Implementations§

§

impl<T> Freeze for HotkeyManager<T>

§

impl<T> !RefUnwindSafe for HotkeyManager<T>

§

impl<T> Send for HotkeyManager<T>
where T: Send,

§

impl<T> Sync for HotkeyManager<T>
where T: Sync + Send,

§

impl<T> Unpin for HotkeyManager<T>
where T: Unpin,

§

impl<T> !UnwindSafe for HotkeyManager<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.