use std::{
io,
num::NonZeroUsize,
ptr::{self, NonNull},
};
use winapi::{
shared::windef::HWND,
um::{
errhandlingapi::{GetLastError, SetLastError},
winuser::{GetWindowTextLengthW, GetWindowTextW},
},
};
use wineventhook::{raw_event, AccessibleObjectId, EventFilter, WindowEventHook};
#[tokio::main]
async fn main() {
let (event_tx, mut event_rx) = tokio::sync::mpsc::unbounded_channel();
let hook = WindowEventHook::hook(
EventFilter::default().event(raw_event::SYSTEM_FOREGROUND),
event_tx,
)
.await
.unwrap();
while let Some(event) = event_rx.recv().await {
if event.object_type() == AccessibleObjectId::Window {
let title = get_window_text(
event
.window_handle()
.map_or_else(ptr::null_mut, NonNull::as_ptr),
)
.unwrap();
println!("{:?}", title);
}
}
hook.unhook().await.unwrap();
}
fn get_window_text_length(window: HWND) -> io::Result