keyboard 0.1.0

A small, well-documented library for reading keyboard input on Windows.
docs.rs failed to build keyboard-0.1.0
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.

keyboard.rs

Crates.io License Platform

The simplest possible way to read the keyboard on Windows.

No windows, no focus, no message loops - just three functions and one enum.

Installation

From crates.io

Add keyboard to your project using Cargo:

cargo add keyboard

Or add it manually to your Cargo.toml:

[dependencies]
keyboard = "0.1.0"

Replace 0.1.0 with the latest published version.

From GitHub

You can also install the library directly from GitHub:

cargo add --git https://github.com/enesx32/keyboard keyboard

Or add it manually to your Cargo.toml:

[dependencies]
keyboard = { git = "https://github.com/enesx32/keyboard" }

The whole API

use keyboard::{Key, pressed_keys, read_char, wait_for_key};

// Is a key held down right now?
if Key::Space.is_pressed() {
    println!("jumping!");
}

// What keys are held down right now?
for key in pressed_keys() {
    println!("{key:?} is down");
}

// What character am I typing? ('\0' if nothing printable)
// Shift and Caps Lock are applied for you.
let c = read_char();

if c != '\0' {
    print!("{c}");
}

// Wait for the user to press any key.
let key = wait_for_key();
println!("You pressed {key}");

That's it!

You want... Use...
Is one key down? Key::Space.is_pressed()
All keys down right now? pressed_keys()
The character being typed? read_char()
Wait for the user to press a key? wait_for_key()
Turn a key into text? Key::G.to_char()

Platform support

Only Windows is implemented (keyboard::windows).

Other platforms get a clear compile error instead of silently doing nothing.

Running the example

Clone the repository:

git clone https://github.com/enesx32/keyboard.git
cd keyboard

Run the example:

cargo run --example readout

Hold down a few keys and watch the console.

Layout note

Key::to_char() and read_char() map to a standard US (ANSI) keyboard layout.

License

Licensed under either of the following, at your option:

  • MIT License
  • Apache License, Version 2.0

See LICENSE-MIT and LICENSE-APACHE for details.