Skip to content

Commit

Permalink
update the hotkey library (#27) (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jehendeoff authored Apr 23, 2023
1 parent 788c054 commit c019cfb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
4 changes: 2 additions & 2 deletions apex-input/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ edition = "2021"

[dependencies]
anyhow = { version = "1.0.45", optional = true }
tauri-hotkey = { version = "0.1.2", optional = true }
global-hotkey = { version = "0.2.0", optional = true }
tokio = { version = "1.13.0", features = ["sync"], optional = true }

[features]
default = []
hotkeys = ["tauri-hotkey", "anyhow", "tokio"]
hotkeys = ["global-hotkey", "anyhow", "tokio"]
39 changes: 17 additions & 22 deletions apex-input/src/hotkey.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
use crate::Command;
use anyhow::Result;
use tauri_hotkey::{Hotkey, HotkeyManager, Key, Modifier};
use global_hotkey::{GlobalHotKeyManager, hotkey::{HotKey, Modifiers, Code}, GlobalHotKeyEvent};
use tokio::sync::broadcast;

pub struct InputManager {
_hkm: HotkeyManager,
_hkm: GlobalHotKeyManager,
}

impl InputManager {
pub fn new(sender: broadcast::Sender<Command>) -> Result<Self> {
let mut hkm = HotkeyManager::new();
let hkm = GlobalHotKeyManager::new().unwrap();

let modifiers = vec![Modifier::ALT, Modifier::SHIFT];
let modifiers = Some(Modifiers::ALT | Modifiers::SHIFT);

let sender2 = sender.clone();
let hotkey_previous = HotKey::new (modifiers, Code::KeyA);
let hotkey_next = HotKey::new (modifiers, Code::KeyD);

hkm.register(
Hotkey {
modifiers: modifiers.clone(),
keys: vec![Key::A],
},
move || {
hkm.register(hotkey_previous).unwrap();
hkm.register(hotkey_next).unwrap();

let hotkey_handler = move|event: GlobalHotKeyEvent| {
if event.id == hotkey_previous.id() {
sender
.send(Command::PreviousSource)
.expect("Failed to send command!");
},
)?;
hkm.register(
Hotkey {
modifiers,
keys: vec![Key::D],
},
move || {
sender2
}else{
sender
.send(Command::NextSource)
.expect("Failed to send command!");
},
)?;
}
};

GlobalHotKeyEvent::set_event_handler(Some(hotkey_handler));

Ok(Self { _hkm: hkm })
}
Expand Down

0 comments on commit c019cfb

Please sign in to comment.