Crate wintrack

Crate wintrack 

Source
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:

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§

IntegrityLevel
A process integrity level.
WindowEvent
A window event.
WindowRect
Rectangle of the window’s position on screen.
WindowSnapshot
Captures various properties of a window’s current state.

Enums§

TryHookError
Error returned by try_hook.
UnhookError
Error returned by unhook.
WindowEventKind
The kind of the event that occurred for a window.
WindowSnapshotFromHandleError
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§

WinProcessId
Windows process id.
WinThreadId
Windows thread id, notably not std::thread::ThreadId.
WindowEventCallback
A boxed closure/function pointer that provides WindowEvents.