#windows #input #simulation #keyboard #mouse

winput

A high-level interface to Windows’ input system

6 releases

0.2.3 Apr 2, 2021
0.2.2 Mar 29, 2021
0.2.1 Feb 2, 2021
0.2.0 Jan 26, 2021
0.1.1 Jan 4, 2021

#26 in Windows APIs

Download history 12/week @ 2020-12-29 23/week @ 2021-01-05 12/week @ 2021-01-12 8/week @ 2021-01-19 19/week @ 2021-01-26 23/week @ 2021-02-02 7/week @ 2021-02-09 21/week @ 2021-02-16 3/week @ 2021-02-23 4/week @ 2021-03-02 5/week @ 2021-03-09 1/week @ 2021-03-16 19/week @ 2021-03-23 40/week @ 2021-03-30 20/week @ 2021-04-06

59 downloads per month
Used in auto_activity

MIT license

71KB
948 lines

Documentation Crates.io

winput is a high-level interface to Windows' input system.

Target

This crate aims to be low-level and straightforward enough to be used as a backend for other, more general crates of the genre. For this purpose, the "minimal" feature disables most of the stuff that is not really part of Windows' input system (things like Keylike, for example, that are mostly there for convenience).

Features

  • minimal: This feature disables the Keylike structure as well as some shortcut functions. This feature has been made for people that want to use the straightforward api winput provides.
  • message_loop: This feature enables the message_loop module that gives a way to globally retreive keyboard and mouse events from Windows' message system.

What is left to do?

winput does not currently support any devices other than the mouse and the keyboard. I haven't really looked into how those work so if you know anything, feel free to submit an issue or a pull request!

Examples

The Keylike structure allows you to synthesize keystrokes on objects that can be used as keys.

use winput::{Vk, Button};

// Synthesize keystrokes from a Virtual-Key Code
winput::press(Vk::Shift);    // press the shift key
winput::send(Vk::A);         // press then release the A key
winput::release(Vk::Shift);  // release the shift key

// Synthesize keystrokes from characters
winput::send('F');
winput::send('O');
winput::send('O');

// Synthesize keystrokes from mouse buttons
winput::send(Button::Left);

// You can synthesize keystrokes for the characters of a string
winput::send_str("Hello, world!");

The Mouse structure can be used to manipulate the mouse.

use winput::Mouse;

// Retrieve the position of the mouse.
let (x, y) = Mouse::position();

// Set the mouse position
//  ... in screen coordinates
Mouse::set_position(10, 10);
//  ... in normalized absolute coordinates
Mouse::move_absolute(0.5, 0.5);
//  ... relatively to the current cursor's position
Mouse::move_relative(100, 50);

// Rotate the mouse wheel (vertically)
Mouse::scroll(1.5);
//  ... or horizontally
Mouse::scrollh(-1.5);

For more complicated input patterns, the Input structure can be used.

use winput::{Input, Vk, Action, MouseMotion};

// There is multiple ways to create an `Input`:
let inputs = [
    // ... from a character
    Input::from_char('a', Action::Press).unwrap(),
    // ... from a Virtual-Key Code
    Input::from_vk(Vk::A, Action::Release),
    // ... from a mouse motion
    Input::from_motion(MouseMotion::Relative { x: 100, y: 100 }),

    // additional constructors are available
];

let number_of_inputs_inserted = winput::send_inputs(&inputs);

assert_eq!(number_of_inputs_inserted, 3);

With the message_loop feature (enabled by default), keyboard keystrokes and mouse inputs can be retreived.

use winput::{Vk, Action};
use winput::message_loop;

let receiver = message_loop::start().unwrap();

loop {
    match receiver.next_event() {
        message_loop::Event::Keyboard {
            vk,
            action: Action::Press,
            ..
        } => {
            if vk == Vk::Escape {
                break;
            } else {
                println!("{:?} was pressed!", vk);
            }
        },
        _ => (),
    }
}

Dependencies

~27KB