Expand description
A library for monitoring window related events on Windows.
This crate allows you to set a callback that will be called for common window events.
The callback receives a WindowEvent, which includes the event kind and
a snapshot of the window’s state at the time of the event.
This library allows you to listen for the following events:
- Foreground (active) window changed
- Window title or name changed
- Window became visible (unminimized / moved onscreen)
- Window became hidden (minimized / moved offscreen)
- New window was created
- Window was destroyed or closed
- Window was moved or resized
The snapshot contains fields including title, rect, executable, and some lower level information.
§Usage
First, call try_hook, which spawns a thread that sets a hook & listens for events.
wintrack::try_hook().expect("no hook should be set yet");Then, define a callback that will be called for each event.
wintrack::set_callback(Box::new(|evt| {
// ignore events from zero-sized windows or windows with no title
if evt.snapshot.rect.size() != (0, 0) && !evt.snapshot.title.is_empty() {
dbg!(evt.snapshot);
}
}));At the end of your program, optionally unhook the hook.
wintrack::unhook().expect("should have set hook earlier")For an example of using a channel to collect events or listen for events in another location of your program,
see the example in the documentation for set_callback.
Structs§
- Integrity
Level - A process integrity level.
- Window
Event - A window event.
- Window
Rect - Rectangle of the window’s position on screen.
- Window
Snapshot - Captures various properties of a window’s current state.
Enums§
- TryHook
Error - Error returned by
try_hook. - Unhook
Error - Error returned by
unhook. - Window
Event Kind - The kind of the event that occurred for a window.
- Window
Snapshot From Handle Error - Error returned by
WindowSnapshot::from_hwnd.
Functions§
- remove_
callback - Removes & returns the currently set callback if it exists.
- set_
callback - Sets the callback called when a window event occurs.
- try_
hook - Attempts to install a hook for monitoring window events.
- unhook
- Removes window event monitoring hook.
Type Aliases§
- WinProcess
Id - Windows process id.
- WinThread
Id - Windows thread id, notably not
std::thread::ThreadId. - Window
Event Callback - A boxed closure/function pointer that provides
WindowEvents.