### NonNull::from_ref Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Demonstrates converting a reference to a NonNull pointer. ```rust pub const fn from_ref(r: &T) -> NonNull ``` -------------------------------- ### NonNull::new Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Shows how to safely create a NonNull pointer using new, which returns an Option and handles null pointers. ```rust use std::ptr::NonNull; let mut x = 0u32; let ptr = NonNull::::new(&mut x as *mut _).expect("ptr is null!"); if let Some(ptr) = NonNull::::new(std::ptr::null_mut()) { unreachable!(); } ``` -------------------------------- ### NonNull::from_mut Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Demonstrates converting a mutable reference to a NonNull pointer. ```rust pub const fn from_mut(r: &mut T) -> NonNull ``` -------------------------------- ### Listen for All Window Events Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/lib.rs.html This example demonstrates how to set up a window event hook to listen for all events and print them to the console. It requires the tokio runtime for asynchronous operations. ```rust #![cfg(windows)] use wineventhook::{EventFilter, WindowEventHook}; #[tokio::main] async fn main() { // Create a new hook let (event_tx, mut event_rx) = tokio::sync::mpsc::unbounded_channel(); let hook = WindowEventHook::hook( EventFilter::default(), event_tx, ).await.unwrap(); // Wait and print events while let Some(event) = event_rx.recv().await { println!("{:#?}", event); } // Unhook the hook hook.unhook().await.unwrap(); } ``` -------------------------------- ### Listen for All Window Events Source: https://docs.rs/wineventhook/0.11.0/wineventhook/index.html This example demonstrates how to create a new window event hook, listen for all events using a default EventFilter, and print each received event to the console. It also shows how to unhook the event listener. ```rust use wineventhook::{EventFilter, WindowEventHook}; #[tokio::main] async fn main() { // Create a new hook let (event_tx, mut event_rx) = tokio::sync::mpsc::unbounded_channel(); let hook = WindowEventHook::hook( EventFilter::default(), event_tx, ).await.unwrap(); // Wait and print events while let Some(event) = event_rx.recv().await { println!("{:#?}", event); } // Unhook the hook hook.unhook().await.unwrap(); } ``` -------------------------------- ### SYSTEM_MOVESIZESTART Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_MOVESIZESTART.html Defines the SYSTEM_MOVESIZESTART constant with its integer value. This constant is used to identify the start of a window move or resize event. ```rust pub const SYSTEM_MOVESIZESTART: i32 = 0x000A; ``` -------------------------------- ### NonNull::to_raw_parts Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Shows how to decompose a pointer into its data pointer and metadata components, returning NonNull. This is a nightly-only experimental API. ```rust pub const fn to_raw_parts(self) -> (NonNull<()>, ::Metadata) ``` -------------------------------- ### NonNull::with_addr Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Creates a new NonNull pointer with a modified address while preserving the original provenance. This is a Strict Provenance API. ```rust pub fn with_addr(self, addr: NonZero) -> NonNull ``` -------------------------------- ### NonNull::addr Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Retrieves the address portion of a NonNull pointer. This is a Strict Provenance API. ```rust pub fn addr(self) -> NonZero ``` -------------------------------- ### NonNull::from_raw_parts Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Illustrates creating a NonNull pointer from raw parts, similar to std::ptr::from_raw_parts but returns NonNull. This is a nightly-only experimental API. ```rust pub const fn from_raw_parts( data_pointer: NonNull, metadata: ::Metadata, ) -> NonNull ``` -------------------------------- ### NonNull::expose_provenance Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Exposes the provenance part of a NonNull pointer for use in with_exposed_provenance. This is an Exposed Provenance API. ```rust pub fn expose_provenance(self) -> NonZero ``` -------------------------------- ### Get all system event range Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all_system.html Call this function to get the range of all system-level events. These events describe situations affecting all applications in the system. ```rust pub fn all_system() -> Range ``` -------------------------------- ### SYSTEM_START Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_START.html Defines the SYSTEM_START constant, representing the lowest system event value. This constant is used to identify system start events. ```rust pub const SYSTEM_START: i32 = 0x0001; ``` -------------------------------- ### write_bytes Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Invokes memset on the pointer, setting a specified number of bytes starting from the WindowHandle to a given value. ```APIDOC ## write_bytes ### Description Invokes memset on the specified pointer, setting `count * size_of::()` bytes of memory starting at `self` to `val`. ### Method `unsafe fn write_bytes(self, val: u8, count: usize)` ### Safety Concerns See `ptr::write_bytes` for safety concerns and examples. ``` -------------------------------- ### NonNull::map_addr Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Creates a new NonNull pointer by applying a function to its address, preserving provenance. This is a Strict Provenance API. ```rust pub fn map_addr( self, f: impl FnOnce(NonZero) -> NonZero, ) -> NonNull ``` -------------------------------- ### CONSOLE_START_APPLICATION Constant Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.CONSOLE_START_APPLICATION.html The CONSOLE_START_APPLICATION constant is an i32 value representing the event code for starting an application in the console. It is used within the wineventhook crate. ```rust pub const CONSOLE_START_APPLICATION: i32 = 0x4006; ``` -------------------------------- ### Default EventFilter Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.EventFilter.html Provides a default EventFilter configuration. This is useful for starting with a basic filter that can then be customized. ```rust fn default() -> Self ``` -------------------------------- ### SYSTEM_SCROLLINGSTART Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_SCROLLINGSTART.html Defines the SYSTEM_SCROLLINGSTART constant with its integer value. This constant is used to identify scrolling start events in the system. ```rust pub const SYSTEM_SCROLLINGSTART: i32 = 0x0012; ``` -------------------------------- ### NonNull::cast_slice Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Demonstrates casting a NonNull pointer to a slice. This is a nightly-only experimental API. ```rust #![feature(ptr_cast_slice)] use std::ptr::NonNull; // create a slice pointer when starting out with a pointer to the first element let mut x = [5, 6, 7]; let nonnull_pointer = NonNull::new(x.as_mut_ptr()).unwrap(); let slice = nonnull_pointer.cast_slice(3); assert_eq!(unsafe { slice.as_ref()[2] }, 7); ``` -------------------------------- ### Creating a Window Event Hook Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/hook.rs.html This function sets up a new window event hook. It configures flags based on the provided filter and starts a dedicated thread to run a message loop for receiving events. The hook is only created if SetWinEventHook succeeds. ```Rust pub async fn hook( filter: EventFilter, event_tx: tokio::sync::mpsc::UnboundedSender, ) -> Result { let (handle_tx, handle_rx) = tokio::sync::oneshot::channel(); let (abort_tx, abort_rx) = tokio::sync::oneshot::channel(); let event_thread = async_thread::Builder::new() .name("WindowEventHook EventLoop".to_string()) .spawn(move || { let mut flags = HookFlags::OUT_OF_CONTEXT; if filter.skip_own_process { flags |= HookFlags::SKIP_OWN_PROCESS; } if filter.skip_own_thread { flags |= HookFlags::SKIP_OWN_THREAD; } let hook = unsafe { SetWinEventHook( filter.events.0 as _, filter.events.1 as _, ptr::null_mut(), Some(win_event_hook_callback), filter.process_id, filter.thread_id, flags.bits(), ) }; let hook_result = if hook.is_null() { Err(io::Error::last_os_error()) } else { HOOK_EVENT_TX.with(|tx| { tx.set((event_tx, filter.predicate.get())) .map_err(|_| ()) .unwrap(); }); Ok(()) }; handle_tx.send(hook_result).unwrap(); run_dummy_message_loop(abort_rx).unwrap(); let unhook_result = unsafe { UnhookWinEvent(hook) }; if unhook_result == FALSE { Err(io::Error::last_os_error()) } else { Ok(()) } }) .unwrap(); handle_rx.await.unwrap().map(|()| Self { abort_tx, event_thread, }) } ``` -------------------------------- ### Getting Window Thread and Process IDs Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/hook.rs.html Helper function to retrieve the thread and process IDs associated with a raw window handle. ```rust fn from_handle(window: RawWindowHandle) -> Self { let mut process_id = MaybeUninit::uninit(); let thread_id = unsafe { GetWindowThreadProcessId(window, process_id.as_mut_ptr()) }; let process_id = unsafe { process_id.assume_init() }; Self::new( NonZeroU32::new(process_id).unwrap(), NonZeroU32::new(thread_id).unwrap(), ) } ``` -------------------------------- ### NonNull::new_unchecked Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Illustrates creating a NonNull pointer using new_unchecked. This function is unsafe and requires the provided pointer to be non-null. ```rust use std::ptr::NonNull; let mut x = 0u32; let ptr = unsafe { NonNull::new_unchecked(&mut x as *mut _) }; ``` -------------------------------- ### Get AIA Event Range Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all_aia.html Call this function to obtain the predefined range of event IDs used by the Accessibility Interoperability Alliance. No setup or imports are required beyond accessing the `wineventhook` crate. ```rust pub fn all_aia() -> Range ``` -------------------------------- ### SYSTEM_MINIMIZESTART Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_MINIMIZESTART.html Defines the SYSTEM_MINIMIZESTART constant with its integer value. ```rust pub const SYSTEM_MINIMIZESTART: i32 = 0x0016; ``` -------------------------------- ### Getting a Mutable Reference from NonNull Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Illustrates how to get a mutable reference to the value pointed to by a NonNull pointer using the `as_mut` method, allowing in-place modification. ```rust use std::ptr::NonNull; let mut x = 0u32; let mut ptr = NonNull::new(&mut x).expect("null pointer"); let x_ref = unsafe { ptr.as_mut() }; assert_eq!(*x_ref, 0); *x_ref += 2; assert_eq!(*x_ref, 2); ``` -------------------------------- ### Get Timestamp from WindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Returns the precise timestamp when the event occurred. ```rust pub fn timestamp(&self) -> Instant ``` -------------------------------- ### SYSTEM_CONTEXTHELPSTART Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_CONTEXTHELPSTART.html Defines the SYSTEM_CONTEXTHELPSTART constant with its integer value. This constant is used to identify a specific system event. ```rust pub const SYSTEM_CONTEXTHELPSTART: i32 = 0x000C; ``` -------------------------------- ### Get Thread ID from WindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Returns the identifier of the thread that generated the event. ```rust pub fn thread_id(&self) -> u32 ``` -------------------------------- ### SYSTEM_SWITCHSTART Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_SWITCHSTART.html Defines the SYSTEM_SWITCHSTART constant, representing the ALT+TAB window activation event. This constant is used within the WinEventProc callback. ```rust pub const SYSTEM_SWITCHSTART: i32 = 0x0014; ``` -------------------------------- ### SYSTEM_MENUSTART Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_MENUSTART.html Defines the SYSTEM_MENUSTART constant with its integer value. This constant is used to identify the event where a menu item on the menu bar is selected. ```rust pub const SYSTEM_MENUSTART: i32 = 0x0004; ``` -------------------------------- ### Get Object Type from WindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Identifies and returns the type of object associated with the event. ```rust pub fn object_type(&self) -> AccessibleObjectId ``` -------------------------------- ### write_bytes Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.HookHandle.html Fills a contiguous block of memory starting at the HookHandle with a specified byte value. ```APIDOC ## write_bytes ### Description Invokes memset on the specified pointer, setting `count * size_of::()` bytes of memory starting at `self` to `val`. ### Method `unsafe fn write_bytes(&self, val: u8, count: usize)` ### Safety Concerns See `ptr::write_bytes` for safety concerns and examples. ``` -------------------------------- ### Generic Implementations for WindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Demonstrates various blanket implementations available for WindowEvent, such as Any, Borrow, From, Into, ToOwned, TryFrom, and TryInto. ```rust impl Any for T where T: 'static + ?Sized, ``` ```rust fn type_id(&self) -> TypeId ``` ```rust impl Borrow for T where T: ?Sized, ``` ```rust fn borrow(&self) -> &T ``` ```rust impl BorrowMut for T where T: ?Sized, ``` ```rust fn borrow_mut(&mut self) -> &mut T ``` ```rust impl CloneToUninit for T where T: Clone, ``` ```rust unsafe fn clone_to_uninit(&self, dest: *mut u8) ``` ```rust impl From for T ``` ```rust fn from(t: T) -> T ``` ```rust impl Into for T where U: From, ``` ```rust fn into(self) -> U ``` ```rust impl ToOwned for T where T: Clone, ``` ```rust type Owned = T ``` ```rust fn to_owned(&self) -> T ``` ```rust fn clone_into(&self, target: &mut T) ``` ```rust impl TryFrom for T where U: Into, ``` ```rust type Error = Infallible ``` ```rust fn try_from(value: U) -> Result>::Error> ``` ```rust impl TryInto for T where U: TryFrom, ``` ```rust type Error = >::Error ``` ```rust fn try_into(self) -> Result>::Error> ``` -------------------------------- ### Get Event Type from WindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Determines and returns the specific type of window event that occurred. ```rust pub fn event_type(&self) -> WindowEventType ``` -------------------------------- ### SYSTEM_CAPTURESTART Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_CAPTURESTART.html Defines the SYSTEM_CAPTURESTART constant with its integer value. This event is sent by the system when a window receives mouse capture. ```rust pub const SYSTEM_CAPTURESTART: i32 = 0x0008; ``` -------------------------------- ### NonNull::as_ptr Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Acquires the underlying raw mutable pointer (*mut T) from a NonNull pointer. ```rust pub const fn as_ptr(self) -> *mut T ``` -------------------------------- ### OBJECT_START Constant Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.OBJECT_START.html The OBJECT_START constant is defined as an i32 with a value of 0x8000. It signifies the lowest possible value for an object event. ```APIDOC ## Constant OBJECT_START ### Description The lowest object event value. ### Value ```rust pub const OBJECT_START: i32 = 0x8000; ``` ``` -------------------------------- ### System Window State Event Constants Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Constants for system-sent events related to window minimization and desktop switching. ```rust pub const SYSTEM_MINIMIZESTART: i32 = 0x0016; pub const SYSTEM_MINIMIZEEND: i32 = 0x0017; pub const SYSTEM_DESKTOPSWITCH: i32 = 0x0020; ``` -------------------------------- ### Get Hook Handle from WindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Retrieves the handle to the shared event hook function associated with the WindowEvent. ```rust pub fn hook_handle(&self) -> HookHandle ``` -------------------------------- ### PartialEq Implementation for RawWindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.RawWindowEvent.html Defines how to compare two RawWindowEvent instances for equality. ```rust fn eq(&self, other: &RawWindowEvent) -> bool ``` ```rust fn ne(&self, other: &Rhs) -> bool ``` -------------------------------- ### Get Console Events Range Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Retrieves the range of all console-level events. These events indicate changes in console windows. ```rust /// Returns the range of all console-level events. /// These events indicate changes in console windows. #[must_use] pub fn all_console() -> Range { Range { start: CONSOLE_START, end: CONSOLE_END, } } ``` -------------------------------- ### SYSTEM_DRAGDROPSTART Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_DRAGDROPSTART.html Defines the SYSTEM_DRAGDROPSTART constant with its integer value. This event signals the initiation of a drag-and-drop operation. ```rust pub const SYSTEM_DRAGDROPSTART: i32 = 0x000E; ``` -------------------------------- ### Get Console Event Range Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all_console.html Returns the range of all console-level events. These events indicate changes in console windows. ```rust pub fn all_console() -> Range ``` -------------------------------- ### CloneToUninit Trait Implementation (Nightly) Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.RawWindowEvent.html An experimental nightly-only API for copying data to uninitialized memory. ```rust unsafe fn clone_to_uninit(&self, dest: *mut u8) ``` -------------------------------- ### OEM Defined Event Range Constants Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Constants defining the start and end of event values reserved for OEMs. ```rust pub const OEM_DEFINED_START: i32 = 0x0101; pub const OEM_DEFINED_END: i32 = 0x01FF; ``` -------------------------------- ### Get Window Handle from WindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Returns the handle to the window that generated the event. Returns None if no window is associated with the event. ```rust pub fn window_handle(&self) -> Option ``` -------------------------------- ### all_system Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all_system.html Returns the range of all system-level events. These events describe situations affecting all applications in the system. ```APIDOC ## all_system ### Description Returns the range of all system-level events. These events describe situations affecting all applications in the system. ### Signature ```rust pub fn all_system() -> Range ``` ``` -------------------------------- ### Get All UI Automation Event IDs Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all_uia_event.html Returns the range of all UI Automation event IDs. This function does not require any arguments. ```rust pub fn all_uia_event() -> Range ``` -------------------------------- ### Getting a Shared Reference from NonNull Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Shows how to obtain a shared reference to the value pointed to by a NonNull pointer using the `as_ref` method. ```rust use std::ptr::NonNull; let mut x = 0u32; let ptr = NonNull::new(&mut x as *mut _).expect("ptr is null!"); let ref_x = unsafe { ptr.as_ref() }; println!("{ref_x}"); ``` -------------------------------- ### SYSTEM_DIALOGSTART Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_DIALOGSTART.html Defines the SYSTEM_DIALOGSTART constant with a value of 0x0010. This constant is used to identify dialog box events. ```rust pub const SYSTEM_DIALOGSTART: i32 = 0x0010; ``` -------------------------------- ### Get Child ID from WindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Retrieves the child ID of the element that triggered the event. Returns None if the event was generated by the object itself. ```rust pub fn child_id(&self) -> Option ``` -------------------------------- ### Create WindowEvent from Raw Event Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Constructs a new WindowEvent instance from a raw event. This is the primary way to create a WindowEvent. ```rust pub fn from_raw(raw: RawWindowEvent) -> Self ``` -------------------------------- ### Conversions to NonNull Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Demonstrates how to safely convert references to NonNull pointers. ```APIDOC ## `from` Conversions ### `impl From<&T> for NonNull` #### `fn from(r: &T) -> NonNull` Converts a `&T` to a `NonNull`. This conversion is safe and infallible since references cannot be null. ### `impl From<&mut T> for NonNull` #### `fn from(r: &mut T) -> NonNull` Converts a `&mut T` to a `NonNull`. This conversion is safe and infallible since references cannot be null. ``` -------------------------------- ### NonNull Conversions Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.HookHandle.html Demonstrates how to convert references to NonNull pointers. ```APIDOC ## fn from(r: &T) -> NonNull ### Description Converts a `&T` to a `NonNull`. This conversion is safe and infallible since references cannot be null. ### Method `from` ### Parameters - **r** (`&T`) - Description: A reference to a type T. ### Response - **NonNull** - A NonNull pointer. ``` ```APIDOC ## fn from(r: &mut T) -> NonNull ### Description Converts a `&mut T` to a `NonNull`. This conversion is safe and infallible since references cannot be null. ### Method `from` ### Parameters - **r** (`&mut T`) - Description: A mutable reference to a type T. ### Response - **NonNull** - A NonNull pointer. ``` -------------------------------- ### Run Dummy Message Loop Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/message_loop.rs.html Executes a message loop that processes Windows messages and can be aborted via a oneshot receiver. It creates a message-only window and waits for messages or an abort signal. ```rust use std::{io, mem, ptr, time::Duration}; use tokio::sync::oneshot; use windows_sys::Win32::{ Foundation::{FALSE, HWND, POINT, WAIT_FAILED}, UI::WindowsAndMessaging::{ CreateWindowExA, DestroyWindow, HWND_MESSAGE, MSGWaitForMultipleObjects, PM_REMOVE, PeekMessageW, QS_ALLEVENTS, }, }; pub fn run_dummy_message_loop(mut abort_receiver: oneshot::Receiver<()>) -> io::Result<()> { let window = MessageOnlyWindow::new()?; let mut msg = MSG { hwnd: ptr::null_mut(), message: 0, wParam: 0, lParam: 0, time: 0, pt: POINT { x: 0, y: 0 }, }; while matches!( abort_receiver.try_recv(), Err(oneshot::error::TryRecvError::Empty) ) { while unsafe { PeekMessageW(&mut msg, ptr::null_mut(), 0, 0, PM_REMOVE) } != 0 {} if unsafe { MsgWaitForMultipleObjects( 0, ptr::null(), FALSE, Duration::from_secs(1).as_millis() as _, QS_ALLEVENTS, ) } == WAIT_FAILED { break; } } window.destroy() } #[derive(Debug, PartialEq, Eq, Hash)] pub struct MessageOnlyWindow(HWND); impl MessageOnlyWindow { pub fn new() -> io::Result { let handle = unsafe { CreateWindowExA( 0, c"STATIC".as_ptr().cast(), // pre-defined window class for buttons and other ui elements ptr::null_mut(), 0, 0, 0, 0, 0, HWND_MESSAGE, // creates a message-only window ptr::null_mut(), ptr::null_mut(), ptr::null_mut(), ) }; if handle.is_null() { return Err(io::Error::last_os_error()); } Ok(Self(handle.cast())) } pub fn handle(&self) -> HWND { self.0 } pub fn destroy(mut self) -> io::Result<()> { let result = unsafe { self.destroy_core() }; mem::forget(self); result } unsafe fn destroy_core(&mut self) -> io::Result<()> { let result = unsafe { DestroyWindow(self.handle()) }; if result == 0 { Err(io::Error::last_os_error()) } else { Ok(()) } } } impl Drop for MessageOnlyWindow { fn drop(&mut self) { let result = unsafe { self.destroy_core() }; debug_assert!( result.is_ok(), "MessageOnlyWindow::destroy() failed with {:?}", result.unwrap_err() ); } } ``` -------------------------------- ### Equality and Partial Ordering for NonNull Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Covers equality testing and partial ordering for NonNull pointers. ```APIDOC ## Equality and Partial Ordering ### `impl PartialEq for NonNull` #### `fn eq(&self, other: &NonNull) -> bool` Tests for equality between `self` and `other`. #### `fn ne(&self, other: &Rhs) -> bool` Tests for inequality. ### `impl PartialOrd for NonNull` #### `fn partial_cmp(&self, other: &NonNull) -> Option` Returns an ordering between `self` and `other` if one exists. #### `fn lt(&self, other: &Rhs) -> bool` Tests if `self` is less than `other`. #### `fn le(&self, other: &Rhs) -> bool` Tests if `self` is less than or equal to `other`. #### `fn gt(&self, other: &Rhs) -> bool` Tests if `self` is greater than `other`. #### `fn ge(&self, other: &Rhs) -> bool` Tests if `self` is greater than or equal to `other`. ``` -------------------------------- ### Incorrect NonNull::new_unchecked Usage Example Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Highlights an incorrect and unsafe usage of new_unchecked with a null pointer, which leads to undefined behavior. ```rust use std::ptr::NonNull; // NEVER DO THAT!!! This is undefined behavior. ⚠️ let ptr = unsafe { NonNull::::new_unchecked(std::ptr::null_mut()) }; ``` -------------------------------- ### UI Automation Event ID Range Constants Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Constants defining the start and end of event identifiers reserved for UI Automation. ```rust pub const UIA_EVENTID_START: i32 = 0x4E00; pub const UIA_EVENTID_END: i32 = 0x4EFF; ``` -------------------------------- ### SYSTEM_MENUPOPUPSTART Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.SYSTEM_MENUPOPUPSTART.html Defines the SYSTEM_MENUPOPUPSTART constant used to identify the display of a pop-up menu. ```rust pub const SYSTEM_MENUPOPUPSTART: i32 = 0x0006; ``` -------------------------------- ### Receive Object Create Event on Window Creation Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/lib.rs.html This test case verifies that an OBJECT_CREATE event is correctly received when a new window is created. It configures the hook to specifically listen for OBJECT_CREATE events and checks event details like window handle, thread ID, and object type. ```rust use std::{ptr::NonNull, time::Instant}; use windows_sys::Win32::System::Threading::GetCurrentThreadId; use crate::{ AccessibleObjectId, EventFilter, ObjectWindowEvent, WindowEventHook, WindowEventType, message_loop::MessageOnlyWindow, raw_event, }; #[tokio::test] async fn recv_object_create_on_window_create() { let (event_tx, mut event_rx) = tokio::sync::mpsc::unbounded_channel(); let hook = WindowEventHook::hook( EventFilter::default() .event(raw_event::OBJECT_CREATE) .skip_own_process(false) .skip_own_thread(false), event_tx, ) .await .unwrap(); let window = MessageOnlyWindow::new().unwrap(); let window_thread_id = unsafe { GetCurrentThreadId() }; while let Some(event) = event_rx.recv().await { if event.event_type() == WindowEventType::Object(ObjectWindowEvent::Create) && event.thread_id() == window_thread_id { assert_eq!(event.window_handle(), NonNull::new(window.handle())); assert_eq!(event.child_id(), None); assert_eq!(event.object_type(), AccessibleObjectId::Window); assert!(event.timestamp() <= Instant::now()); break; } } hook.unhook().await.unwrap(); } ``` -------------------------------- ### Get Object Events Range Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Retrieves the range of all object-level events. These events pertain to situations specific to objects within one application. ```rust /// Returns the range of all object-level events. /// These events pertain to situations specific to objects within one application. #[must_use] pub fn all_object() -> Range { Range { start: OBJECT_START, end: OBJECT_END, } } ``` -------------------------------- ### WindowEvent timestamp Getter Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/event.rs.html Calculates and returns the event's timestamp as an `Instant`. It uses `GetTickCount` to determine the time since system startup and converts the raw millisecond timestamp accordingly. ```rust /// Returns the timestamp of the event. #[must_use] pub fn timestamp(&self) -> Instant { let time_since_system_start = Duration::from_millis(u64::from(unsafe { GetTickCount() })); let event_time_relative_to_system_start = ``` -------------------------------- ### Get All WinEvents Range Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Retrieves the range of all possible event values from MIN to MAX. This function is useful for iterating through all defined event IDs. ```rust /// Returns the range of all event values. #[must_use] pub fn all() -> Range { Range { start: MIN, end: MAX, } } ``` -------------------------------- ### UI Automation Property ID Range Constants Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Constants defining the start and end of property-changed event identifiers reserved for UI Automation. ```rust pub const UIA_PROPID_START: i32 = 0x7500; pub const UIA_PROPID_END: i32 = 0x75FF; ``` -------------------------------- ### CONSOLE_START Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.CONSOLE_START.html Defines the CONSOLE_START constant with its integer value. This constant is used to identify console startup events. ```rust pub const CONSOLE_START: i32 = 0x4001; ``` -------------------------------- ### copy_from Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Copies a specified number of bytes from a source pointer to the WindowHandle. The source and destination memory regions may overlap. ```APIDOC ## copy_from ### Description Copies `count * size_of::()` bytes from `src` to `self`. The source and destination may overlap. ### Method `unsafe fn copy_from(self, src: NonNull, count: usize)` ### Notes This method has the opposite argument order of `ptr::copy`. ### Safety Concerns See `ptr::copy` for safety concerns and examples. ``` -------------------------------- ### Get AIA Events Range Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Retrieves the range of all events reserved for use by the Accessibility Interoperability Alliance (AIA). This range is standardized for industry-wide use. ```rust /// Returns the range of all events reserved for use by the Accessibility Interoperability Alliance (AIA). #[must_use] pub fn all_aia() -> Range { Range { start: AIA_START, end: AIA_END, } } ``` -------------------------------- ### PartialEq Implementation for WindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Enables comparison for equality between WindowEvent instances. ```rust fn eq(&self, other: &WindowEvent) -> bool ``` ```rust fn ne(&self, other: &Rhs) -> bool ``` -------------------------------- ### Include All Processes Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.EventFilter.html Configures the filter to capture events from all processes. Note that to receive events from the current process, `skip_own_process` must be explicitly set to false. ```rust pub fn all_processes(self) -> Self ``` -------------------------------- ### Get Range of UI Automation Property-Changed Event IDs Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all_uia_property_change.html Returns the range of all UI Automation property-changed event IDs. This function does not require any arguments. ```rust pub fn all_uia_property_change() -> Range ``` -------------------------------- ### Get UI Automation Events Range Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Retrieves the range of all UI Automation event IDs. This range covers general events related to UI Automation. ```rust /// Returns the range of all UI Automation event IDs. #[must_use] pub fn all_uia_event() -> Range { Range { start: UIA_EVENTID_START, end: UIA_EVENTID_END, } } ``` -------------------------------- ### Hashing for NonNull Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Explains how NonNull pointers can be hashed. ```APIDOC ## Hashing ### `impl Hash for NonNull` #### `fn hash(&self, state: &mut H)` Feeds this value into the given `Hasher`. #### `fn hash_slice(data: &[Self], state: &mut H)` Feeds a slice of this type into the given `Hasher`. ``` -------------------------------- ### StructuralPartialEq Implementation for WindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.WindowEvent.html Enables structural equality comparisons for WindowEvent. ```rust impl StructuralPartialEq for WindowEvent ``` -------------------------------- ### Get Range of All Event Values Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all.html Call the `all` function to retrieve the inclusive range of all possible event identifiers. This is useful for iterating through all events or validating event IDs. ```rust use wineventhook::raw_event::all; let event_range = all(); println!("All events range from {:?} to {:?}", event_range.start(), event_range.end()); ``` -------------------------------- ### Any Trait Implementation Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.RawWindowEvent.html Provides runtime type information for any type that implements the 'static trait. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### UIA_PROPID_START Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.UIA_PROPID_START.html Defines the lowest event value reserved for UI Automation property-changed event identifiers. This constant is an i32 with a value of 0x7500. ```rust pub const UIA_PROPID_START: i32 = 0x7500; ``` -------------------------------- ### Get ATOM Event Range Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all_atom.html Returns the range of reserved event IDs for ATOM events. These IDs are allocated at runtime through the UI Automation extensibility API and should not be used for any other purpose. ```rust pub fn all_atom() -> Range ``` -------------------------------- ### System Window Switch Event Constants Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Constants for system-sent window switching events, typically related to ALT+TAB. ```rust pub const SYSTEM_SWITCHSTART: i32 = 0x0014; pub const SYSTEM_SWITCHEND: i32 = 0x0015; ``` -------------------------------- ### Get ATOM Events Range Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Retrieves the range of events reserved for ATOM. This range is for event IDs allocated at runtime through the UI Automation extensibility API and should not be used for other purposes. The GlobalAddAtom function is recommended for allocation. ```rust /// Returns the range of reserved for ATOM events. /// The ATOM range is reserved for event IDs that are allocated at runtime through the UI Automation extensibility API. /// Do not use the values from the ATOM range for any other purpose. /// Using the [`GlobalAddAtom`](https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-globaladdatoma) function with a string GUID is the recommended method of allocating WinEvents from the ATOM range. #[must_use] pub fn all_atom() -> Range { Range { start: ATOM_START, end: ATOM_END, } } ``` -------------------------------- ### OEM_DEFINED_START Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.OEM_DEFINED_START.html Defines the OEM_DEFINED_START constant with a value of 0x0101. This is the lowest event value reserved for OEMs. ```rust pub const OEM_DEFINED_START: i32 = 0x0101; ``` -------------------------------- ### clone_from Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Performs copy-assignment from a source NonNull pointer to the current one. ```APIDOC ## fn clone_from(&mut self, source: &Self) Performs copy-assignment from `source`. ### Description Performs copy-assignment from a source NonNull pointer to the current one. This operation is available for types that implement the `Clone` trait. ### Method `clone_from` ### Parameters - **source** (`&Self`): A reference to the source NonNull pointer to copy from. ### Notes Read more about the `Clone` trait. ``` -------------------------------- ### Get OEM Defined Events Range Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Retrieves the range of OEM reserved events. This range is open to anyone needing to use WinEvents as a communication mechanism, with developers encouraged to publish their event definitions to avoid collisions. ```rust /// Returns the range of OEM reserved events. /// The OEM reserved range is open to anyone who needs to use WinEvents as a communication mechanism. /// Developers should define and publish event definitions along with their parameters (or also with associated object types) for event processing so that accidental collisions of event IDs can be avoided. /// The IAccessible2 specification uses part of the OEM reserved range. #[must_use] pub fn all_oem_defined() -> Range { Range { start: OEM_DEFINED_START, end: OEM_DEFINED_END, } } ``` -------------------------------- ### Ordering for NonNull Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Details the ordering capabilities of NonNull pointers. ```APIDOC ## Ordering ### `impl Ord for NonNull` #### `fn cmp(&self, other: &NonNull) -> Ordering` Compares `self` and `other` and returns an `Ordering`. #### `fn max(self, other: Self) -> Self` Compares and returns the maximum of two values. #### `fn min(self, other: Self) -> Self` Compares and returns the minimum of two values. #### `fn clamp(self, min: Self, max: Self) -> Self` Restricts a value to a certain interval. ``` -------------------------------- ### Drag and Drop Event Types in Rust Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/event.rs.html This snippet outlines event types related to drag-and-drop operations, including starting a drag, canceling it, completing it, and entering or leaving a drop target. These are crucial for implementing custom drag-and-drop functionality. ```rust /// The user started to drag an element. DragStart = raw_event::OBJECT_DRAGSTART, /// The user has ended a drag operation before dropping the dragged element on a drop target. DragCancel = raw_event::OBJECT_DRAGCANCEL, /// The user dropped an element on a drop target. DragComplete = raw_event::OBJECT_DRAG_COMPLETE, /// The user dragged an element into a drop target's boundary. DragEnter = raw_event::OBJECT_DRAGENTER, /// The user dragged an element out of a drop target's boundary. DragLeave = raw_event::OBJECT_DRAGLEAVE, /// The user dropped an element on a drop target. DragDropped = raw_event::OBJECT_DRAGDROPPED ``` -------------------------------- ### WinEvent Range Constants Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/raw_event.rs.html Defines the start and end points for various ranges of WinEvent IDs, including system, OEM, console, UI Automation, object, ATOM, and AIA events. These ranges help categorize and manage event IDs. ```rust /// The highest object event value. pub const OBJECT_END: i32 = 0x80FF; /// The lowest event value reserved for custom events allocated at runtime. pub const ATOM_START: i32 = 0xC000; /// The lowest event value specified by the Accessibility Interoperability Alliance (AIA) for use across the industry. pub const AIA_START: i32 = 0xA000; /// The highest event value specified by the Accessibility Interoperability Alliance (AIA) for use across the industry. pub const AIA_END: i32 = 0xAFFF; /// The highest event value reserved for custom events allocated at runtime. pub const ATOM_END: i32 = 0xFFFF; /// The highest possible event value. pub const MAX: i32 = 0x7FFF_FFFF; ``` -------------------------------- ### Hash Implementation for RawWindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.RawWindowEvent.html Enables hashing of RawWindowEvent instances, useful for collections like HashMaps. ```rust fn hash<__H: Hasher>(&self, state: &mut __H) ``` ```rust fn hash_slice(data: &[Self], state: &mut H) where H: Hasher, Self: Sized, ``` -------------------------------- ### Clone Implementation for RawWindowEvent Source: https://docs.rs/wineventhook/0.11.0/wineventhook/struct.RawWindowEvent.html Provides methods to create a duplicate of a RawWindowEvent or copy data from one to another. ```rust fn clone(&self) -> RawWindowEvent ``` ```rust fn clone_from(&mut self, source: &Self) ``` -------------------------------- ### Multiple Hooks Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/lib.rs.html This test demonstrates the ability to create and manage multiple window event hooks concurrently. It sets up two hooks, generates events, and then unhooks both. ```rust let (event_tx, _event_rx) = tokio::sync::mpsc::unbounded_channel(); let hook1 = WindowEventHook::hook(EventFilter::default(), event_tx.clone()) .await .unwrap(); let hook2 = WindowEventHook::hook(EventFilter::default(), event_tx) .await .unwrap(); // generate some events for _ in 0..100 { MessageOnlyWindow::new().unwrap().destroy().unwrap(); } hook1.unhook().await.unwrap(); hook2.unhook().await.unwrap(); ``` -------------------------------- ### all_atom Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all_atom.html Returns the range of reserved ATOM event IDs. These IDs are allocated at runtime through the UI Automation extensibility API and should not be used for any other purpose. The recommended method for allocating WinEvents from the ATOM range is using the `GlobalAddAtom` function with a string GUID. ```APIDOC ## Function all_atom ### Description Returns the range of reserved for ATOM events. The ATOM range is reserved for event IDs that are allocated at runtime through the UI Automation extensibility API. Do not use the values from the ATOM range for any other purpose. Using the `GlobalAddAtom` function with a string GUID is the recommended method of allocating WinEvents from the ATOM range. ### Signature ```rust pub fn all_atom() -> Range ``` ``` -------------------------------- ### OBJECT_START Constant Definition Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/constant.OBJECT_START.html Defines the OBJECT_START constant, which signifies the lowest possible value for an object event. ```rust pub const OBJECT_START: i32 = 0x8000; ``` -------------------------------- ### Checking Pointer Alignment with is_aligned_to (Nightly) Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Demonstrates the nightly-only `is_aligned_to` method for checking if a pointer is aligned to a specific byte boundary. This function panics if the alignment is not a power-of-two. ```rust #![feature(pointer_is_aligned_to)] // On some platforms, the alignment of i32 is less than 4. #[repr(align(4))] struct AlignedI32(i32); let data = AlignedI32(42); let ptr = &data as *const AlignedI32; assert!(ptr.is_aligned_to(1)); assert!(ptr.is_aligned_to(2)); assert!(ptr.is_aligned_to(4)); assert!(ptr.wrapping_byte_add(2).is_aligned_to(2)); assert!(!ptr.wrapping_byte_add(2).is_aligned_to(4)); assert_ne!(ptr.is_aligned_to(8), ptr.wrapping_add(1).is_aligned_to(8)) ``` -------------------------------- ### copy_to Source: https://docs.rs/wineventhook/0.11.0/wineventhook/type.WindowHandle.html Copies a specified number of bytes from the WindowHandle to a destination pointer. The source and destination memory regions may overlap. ```APIDOC ## copy_to ### Description Copies `count * size_of::()` bytes from `self` to `dest`. The source and destination may overlap. ### Method `unsafe fn copy_to(self, dest: NonNull, count: usize)` ### Notes This method has the same argument order as `ptr::copy`. ### Safety Concerns See `ptr::copy` for safety concerns and examples. ``` -------------------------------- ### all_oem_defined Function Signature Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all_oem_defined.html This is the function signature for all_oem_defined. It returns a Range representing the OEM reserved events. ```rust pub fn all_oem_defined() -> Range ``` -------------------------------- ### WindowEvent from_raw Constructor Source: https://docs.rs/wineventhook/0.11.0/src/wineventhook/event.rs.html A constructor function to create a `WindowEvent` from a `RawWindowEvent`. This is the primary way to convert raw API events into the library's processed event type. ```rust impl WindowEvent { /// Creates a new [`WindowEvent`] from a raw event. #[must_use] pub fn from_raw(raw: RawWindowEvent) -> Self { Self { raw } } // ... ``` -------------------------------- ### all Source: https://docs.rs/wineventhook/0.11.0/wineventhook/raw_event/fn.all.html Returns the range of all event values. ```APIDOC ## all ### Description Returns the range of all event values. ### Signature ```rust pub fn all() -> Range ``` ### Returns A `Range` representing all possible event values. ```