-
Notifications
You must be signed in to change notification settings - Fork 72
Implement keyboard event hook on Windows #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
estroBiologist
wants to merge
11
commits into
RustAudio:master
Choose a base branch
from
estroBiologist:keyboard-hook
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+153
−1
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
472dc94
attempt to implement JUCE keyboard hook system
2c490ce
maybe this'll work idk
85f314b
dtor test
48c3665
cleanup
0025f71
Use wnd_proc instead of wnd_proc_inner inside keyboard hook
36989ff
Replace `dtor` with HashSet-based approach
4be2fec
Use W versions of Win32 functions
ed97533
Actually clear hook wrapper (whoops)
48259cf
Combine OPEN_WINDOWS and HOOK into HOOK_STATE RwLock
f082632
Forgot a word whoops
bf5cf53
Drop read lock before calling wnd_proc
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| use std::{ | ||
| collections::HashSet, | ||
| ffi::c_int, | ||
| ptr, | ||
| sync::{LazyLock, RwLock}, | ||
| }; | ||
|
|
||
| use winapi::{ | ||
| shared::{ | ||
| minwindef::{LPARAM, WPARAM}, | ||
| windef::{HHOOK, HWND, POINT}, | ||
| }, | ||
| um::{ | ||
| libloaderapi::GetModuleHandleW, | ||
| processthreadsapi::GetCurrentThreadId, | ||
| winuser::{ | ||
| CallNextHookEx, SetWindowsHookExW, UnhookWindowsHookEx, HC_ACTION, MSG, PM_REMOVE, | ||
| WH_GETMESSAGE, WM_CHAR, WM_KEYDOWN, WM_KEYUP, WM_SYSCHAR, WM_SYSKEYDOWN, WM_SYSKEYUP, WM_USER, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| use crate::win::wnd_proc; | ||
|
|
||
| // track all windows opened by this instance of baseview | ||
| // we use an RwLock here since the vast majority of uses (event interceptions) | ||
| // will only need to read from the HashSet | ||
| static HOOK_STATE: LazyLock<RwLock<KeyboardHookState>> = LazyLock::new(|| RwLock::default()); | ||
|
|
||
| pub(crate) struct KeyboardHookHandle(HWNDWrapper); | ||
|
|
||
| #[derive(Default)] | ||
| struct KeyboardHookState { | ||
| hook: Option<HHOOK>, | ||
| open_windows: HashSet<HWNDWrapper>, | ||
| } | ||
|
|
||
| #[derive(Hash, PartialEq, Eq, Clone, Copy)] | ||
| struct HWNDWrapper(HWND); | ||
|
|
||
| // SAFETY: it's a pointer behind an RwLock. we'll live | ||
| unsafe impl Send for KeyboardHookState {} | ||
| unsafe impl Sync for KeyboardHookState {} | ||
|
|
||
| // SAFETY: we never access the underlying HWND ourselves, just use it as a HashSet entry | ||
| unsafe impl Send for HWNDWrapper {} | ||
| unsafe impl Sync for HWNDWrapper {} | ||
|
|
||
| impl Drop for KeyboardHookHandle { | ||
| fn drop(&mut self) { | ||
| deinit_keyboard_hook(self.0); | ||
| } | ||
| } | ||
|
|
||
| // initialize keyboard hook | ||
| // some DAWs (particularly Ableton) intercept incoming keyboard messages, | ||
| // but we're naughty so we intercept them right back | ||
| pub(crate) fn init_keyboard_hook(hwnd: HWND) -> KeyboardHookHandle { | ||
| let state = &mut *HOOK_STATE.write().unwrap(); | ||
|
|
||
| // register hwnd to global window set | ||
| state.open_windows.insert(HWNDWrapper(hwnd)); | ||
|
|
||
| if state.hook.is_some() { | ||
| // keyboard hook already exists, just return handle | ||
| KeyboardHookHandle(HWNDWrapper(hwnd)) | ||
| } else { | ||
| // keyboard hook doesn't exist (no windows open before this), create it | ||
| let new_hook = unsafe { | ||
| SetWindowsHookExW( | ||
| WH_GETMESSAGE, | ||
| Some(keyboard_hook_callback), | ||
| GetModuleHandleW(ptr::null()), | ||
| GetCurrentThreadId(), | ||
| ) | ||
| }; | ||
|
|
||
| state.hook = Some(new_hook); | ||
|
|
||
| KeyboardHookHandle(HWNDWrapper(hwnd)) | ||
| } | ||
| } | ||
|
|
||
| fn deinit_keyboard_hook(hwnd: HWNDWrapper) { | ||
| let state = &mut *HOOK_STATE.write().unwrap(); | ||
|
|
||
| state.open_windows.remove(&hwnd); | ||
|
|
||
| if state.open_windows.is_empty() { | ||
| if let Some(hhook) = state.hook { | ||
| unsafe { | ||
| UnhookWindowsHookEx(hhook); | ||
| } | ||
|
|
||
| state.hook = None; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| unsafe extern "system" fn keyboard_hook_callback( | ||
| n_code: c_int, wparam: WPARAM, lparam: LPARAM, | ||
| ) -> isize { | ||
| let msg = lparam as *mut MSG; | ||
|
|
||
| if n_code == HC_ACTION && wparam == PM_REMOVE as usize && offer_message_to_baseview(msg) { | ||
| *msg = MSG { | ||
| hwnd: ptr::null_mut(), | ||
| message: WM_USER, | ||
| wParam: 0, | ||
| lParam: 0, | ||
| time: 0, | ||
| pt: POINT { x: 0, y: 0 }, | ||
| }; | ||
|
|
||
| 0 | ||
| } else { | ||
| CallNextHookEx(ptr::null_mut(), n_code, wparam, lparam) | ||
| } | ||
| } | ||
|
|
||
| // check if `msg` is a keyboard message addressed to a window | ||
| // in KeyboardHookState::open_windows, and intercept it if so | ||
| unsafe fn offer_message_to_baseview(msg: *mut MSG) -> bool { | ||
| let msg = &*msg; | ||
|
|
||
| // if this isn't a keyboard message, ignore it | ||
| match msg.message { | ||
| WM_KEYDOWN | WM_SYSKEYDOWN | WM_KEYUP | WM_SYSKEYUP | WM_CHAR | WM_SYSCHAR => {} | ||
|
|
||
| _ => return false, | ||
| } | ||
|
|
||
| // check if this is one of our windows. if so, intercept it | ||
| if HOOK_STATE.read().unwrap().open_windows.contains(&HWNDWrapper(msg.hwnd)) { | ||
| let _ = wnd_proc(msg.hwnd, msg.message, msg.wParam, msg.lParam); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| false | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| mod cursor; | ||
| mod drop_target; | ||
| mod hook; | ||
| mod keyboard; | ||
| mod window; | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.