Skip to content

Commit

Permalink
fix(win-llhook): allow non-kanata injected events
Browse files Browse the repository at this point in the history
  • Loading branch information
jtroo committed Mar 19, 2024
1 parent 426a0c0 commit 4460c72
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/oskbd/windows/llhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use core::fmt;
use std::cell::Cell;
use std::io;
use std::sync::atomic::AtomicI64;
use std::sync::atomic::Ordering::SeqCst;
use std::{mem, ptr};

use winapi::ctypes::*;
Expand Down Expand Up @@ -72,7 +74,7 @@ impl Drop for KeyboardHook {
}

/// Key event received by the low level keyboard hook.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct InputEvent {
pub code: u32,

Expand Down Expand Up @@ -134,6 +136,8 @@ impl From<KeyEvent> for InputEvent {
}
}

pub static EVENTS_TO_IGNORE_COUNT: AtomicI64 = AtomicI64::new(0);

/// The actual WinAPI compatible callback.
unsafe extern "system" fn hook_proc(code: c_int, wparam: WPARAM, lparam: LPARAM) -> LRESULT {
let hook_lparam = &*(lparam as *const KBDLLHOOKSTRUCT);
Expand All @@ -148,7 +152,12 @@ unsafe extern "system" fn hook_proc(code: c_int, wparam: WPARAM, lparam: LPARAM)
// `SendInput()` internally calls the hook function. Filter out injected events
// to prevent recursion and potential stack overflows if our remapping logic
// sent the injected event.
if is_injected {
let ignore_count = EVENTS_TO_IGNORE_COUNT.load(SeqCst);
if is_injected && EVENTS_TO_IGNORE_COUNT.load(SeqCst) > 0 {
eprintln!("ignore count: {ignore_count}");
if EVENTS_TO_IGNORE_COUNT.fetch_sub(1, SeqCst) == 0 {
let _ = EVENTS_TO_IGNORE_COUNT.compare_exchange(-1, 0, SeqCst, SeqCst);
}
return CallNextHookEx(ptr::null_mut(), code, wparam, lparam);
}

Expand Down
7 changes: 7 additions & 0 deletions src/oskbd/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ fn send_key_sendinput(code: u16, is_key_up: bool) {
kb_input.dwFlags |= KEYEVENTF_KEYUP;
}

#[cfg(not(feature = "interception_driver"))]
{
// let mut events = EVENTS_TO_IGNORE.lock();
// events.push_back(InputEvent { code: u32::from(code), up: is_key_up });
EVENTS_TO_IGNORE_COUNT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
}

#[cfg(feature = "win_sendinput_send_scancodes")]
{
/*
Expand Down

0 comments on commit 4460c72

Please sign in to comment.