Skip to content

Commit

Permalink
feat(windows): add pressed and released states (#35)
Browse files Browse the repository at this point in the history
continuation of: #34

ref: #9
  • Loading branch information
amrbashir authored Oct 19, 2023
1 parent 0e235a6 commit 53961a1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changes/down-up-states.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"global-hotkey": "patch"
---

Support Pressed and Released stats of the hotkey, you can check the newly added `state` field or using the `state()` method on the `GlobalHotKeyEvent`.
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ impl GlobalHotKeyEvent {
pub fn id(&self) -> u32 {
self.id
}
/// Returns the state of the associated [`HotKey`].
pub fn state(&self) -> HotKeyState {
self.state
}

/// Gets a reference to the event channel's [`GlobalHotKeyEventReceiver`]
/// which can be used to listen for global hotkey events.
Expand Down
55 changes: 28 additions & 27 deletions src/platform_impl/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,34 @@ use windows_sys::Win32::{
Foundation::{HWND, LPARAM, LRESULT, WPARAM},
UI::{
Input::KeyboardAndMouse::*,
Shell::{DefSubclassProc, SetWindowSubclass},
WindowsAndMessaging::{
CreateWindowExW, DefWindowProcW, RegisterClassW, CW_USEDEFAULT, HMENU, WM_HOTKEY,
WNDCLASSW, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW, WS_EX_TRANSPARENT,
WS_OVERLAPPED,
CreateWindowExW, DefWindowProcW, DestroyWindow, RegisterClassW, CW_USEDEFAULT, HMENU,
WM_HOTKEY, WNDCLASSW, WS_EX_LAYERED, WS_EX_NOACTIVATE, WS_EX_TOOLWINDOW,
WS_EX_TRANSPARENT, WS_OVERLAPPED,
},
},
};

use crate::{hotkey::HotKey, GlobalHotKeyEvent};

const GLOBAL_HOTKEY_SUBCLASS_ID: usize = 6001;

pub struct GlobalHotKeyManager {
hwnd: isize,
}

impl Drop for GlobalHotKeyManager {
fn drop(&mut self) {
unsafe { DestroyWindow(self.hwnd) };
}
}

impl GlobalHotKeyManager {
pub fn new() -> crate::Result<Self> {
let class_name = encode_wide("tray_icon_app");
unsafe {
let hinstance = get_instance_handle();

unsafe extern "system" fn call_default_window_proc(
hwnd: HWND,
msg: u32,
wparam: WPARAM,
lparam: LPARAM,
) -> LRESULT {
DefWindowProcW(hwnd, msg, wparam, lparam)
}

let wnd_class = WNDCLASSW {
lpfnWndProc: Some(call_default_window_proc),
lpfnWndProc: Some(global_hotkey_proc),
lpszClassName: class_name.as_ptr(),
hInstance: hinstance,
..std::mem::zeroed()
Expand Down Expand Up @@ -76,13 +70,6 @@ impl GlobalHotKeyManager {
return Err(crate::Error::OsError(std::io::Error::last_os_error()));
}

SetWindowSubclass(
hwnd,
Some(global_hotkey_subclass_proc),
GLOBAL_HOTKEY_SUBCLASS_ID,
0,
);

Ok(Self { hwnd })
}
}
Expand Down Expand Up @@ -130,22 +117,36 @@ impl GlobalHotKeyManager {
Ok(())
}
}
unsafe extern "system" fn global_hotkey_subclass_proc(
unsafe extern "system" fn global_hotkey_proc(
hwnd: HWND,
msg: u32,
wparam: WPARAM,
lparam: LPARAM,
_id: usize,
_subclass_input_ptr: usize,
) -> LRESULT {
if msg == WM_HOTKEY {
GlobalHotKeyEvent::send(GlobalHotKeyEvent {
id: wparam as _,
state: crate::HotKeyState::Pressed,
});
std::thread::spawn(move || loop {
let state = GetAsyncKeyState(HIWORD(lparam as u32) as i32);
if state == 0 {
GlobalHotKeyEvent::send(GlobalHotKeyEvent {
id: wparam as _,
state: crate::HotKeyState::Released,
});
break;
}
});
}

DefSubclassProc(hwnd, msg, wparam, lparam)
DefWindowProcW(hwnd, msg, wparam, lparam)
}

#[inline(always)]
#[allow(non_snake_case)]
const fn HIWORD(x: u32) -> u16 {
((x >> 16) & 0xFFFF) as u16
}

pub fn encode_wide<S: AsRef<std::ffi::OsStr>>(string: S) -> Vec<u16> {
Expand Down

0 comments on commit 53961a1

Please sign in to comment.