#mouse-event #mouse #keyboard #wheel #uiohook #libuiohook

uiohook-rs

uiohook-rs is a Rust wrapper for the libuiohook, providing cross-platform keyboard and mouse hooking capabilities

5 releases

0.2.3 Aug 17, 2024
0.2.2 Aug 15, 2024
0.2.1 Aug 14, 2024
0.2.0 Aug 12, 2024
0.1.0 Aug 10, 2024

#746 in Hardware support

Download history 193/week @ 2024-08-07 337/week @ 2024-08-14 7/week @ 2024-08-21 5/week @ 2024-09-11 34/week @ 2024-09-25 14/week @ 2024-10-02

195 downloads per month

GPL-3.0 license

540KB
8K SLoC

C 7K SLoC // 0.2% comments Rust 1.5K SLoC // 0.1% comments

uiohook-rs

Crates.io

uiohook-rs is a Rust wrapper for the libuiohook, providing cross-platform keyboard and mouse hooking capabilities.

Features

  • Cross-platform support (Linux, macOS, Windows)
  • Low-level keyboard and mouse event handling
  • Easy-to-use Rust API

IMPORTANT: This crate has not been tested on MacOS and Windows yet, please report any issues you encounter (likely to be compilation issues related to dependencies).

Usage

Here's a basic example of how to use uiohook-rs:

use uiohook_rs::{EventHandler, Uiohook, UiohookEvent};

struct MyEventHandler;

impl EventHandler for MyEventHandler {
    fn handle_event(&self, event: &UiohookEvent) {
        println!("Event: {:?}", event);
    }
}

fn main() {
    let event_handler = MyEventHandler;
    let uiohook = Uiohook::new(event_handler);

    if let Err(e) = uiohook.run() {
        eprintln!("Error: {}", e);
    }
    
    // Stop the hook
    if let Err(e) = uiohook.stop() {
        eprintln!("Error: {}", e);
    }
}

Running the Demo

There are several example programs available under the examples/ directory, including demos for general event handling (demo.rs), pretty-printed output (pretty_demo.rs), and specific handlers for keyboard, mouse, and wheel events. Except for the pretty_demo.rs, all other examples use the minimal code to demonstrate the hook functionality.

To run the demo:

  1. Clone the repo:

    git clone https://github.com/efJerryYang/uiohook-rs.git
    cd uiohook-rs
    
  2. Run the demo:

    cargo run --example demo
    
  3. Press Ctrl-C to exit. See the output:

    Press Ctrl-C to exit
    MOVED    | Mouse             | X: 802   | Y: 644   | Button: 0    | Clicks: 0   
    PRESSED  | Caps Lock         | Code: 58    | Raw: 65509
    RELEASED | Caps Lock         | Code: 58    | Raw: 65509
    PRESSED  | Caps Lock         | Code: 58    | Raw: 65509
    RELEASED | Caps Lock         | Code: 58    | Raw: 65509
    PRESSED  | Left Shift        | Code: 42    | Raw: 65505
    PRESSED  | B                 | Code: 48    | Raw: 66   
    TYPED    | B                 | Code: 66    | Raw: 66   
    RELEASED | B                 | Code: 48    | Raw: 66   
    RELEASED | Left Shift        | Code: 42    | Raw: 65505
    PRESSED  | H                 | Code: 35    | Raw: 104  
    TYPED    | h                 | Code: 104   | Raw: 104  
    RELEASED | H                 | Code: 35    | Raw: 104  
    PRESSED  | Mouse             | X: 802   | Y: 644   | Button: 1    | Clicks: 1   
    RELEASED | Mouse             | X: 802   | Y: 644   | Button: 1    | Clicks: 1   
    CLICKED  | Mouse             | X: 802   | Y: 644   | Button: 1    | Clicks: 1   
    PRESSED  | Mouse             | X: 802   | Y: 644   | Button: 1    | Clicks: 1   
    DRAGGED  | Mouse             | X: 803   | Y: 644   | Button: 0    | Clicks: 1   
    ... (more DRAGGED events)
    DRAGGED  | Mouse             | X: 920   | Y: 630   | Button: 0    | Clicks: 0   
    DRAGGED  | Mouse             | X: 921   | Y: 630   | Button: 0    | Clicks: 0   
    RELEASED | Mouse             | X: 921   | Y: 630   | Button: 1    | Clicks: 0   
    PRESSED  | Left Control      | Code: 29    | Raw: 65507
    PRESSED  | C                 | Code: 46    | Raw: 99   
    TYPED    | c                 | Code: 99    | Raw: 99   
    Exiting...
    

License

GNU General Public License v3.0, see LICENSE.

Dependencies

~2–14MB
~134K SLoC