6 releases (breaking)

0.5.0 Jul 22, 2023
0.4.0 Nov 14, 2022
0.3.1 Dec 23, 2020
0.2.0 Jul 12, 2020
0.1.0 Jan 1, 2019

#1519 in Game dev

CC0 license

31KB
755 lines

buttons

Crates.io Docs.rs

A simple Rust crate for managing and querying input state.

Usage

With winit

(Enabling the winit feature.)

let mut event_loop = winit::event_loop::EventLoop::new();
let mut keyboard = buttons::winit::keyboard();
let mut mouse = buttons::winit::mouse();
let mut touch = buttons::winit::touch();

// Track input
event_loop.run(move |event, _, _| {
    keyboard.handle_event(&event);
    mouse.handle_event(&event);
    touch.handle_event(&event);

    // Check state
    if keyboard.pressed(VirtualKeyCode::Escape)
        || mouse.released(MouseButton::Right)
        || touch.first_touch().is_some()
    {
        // Do something
    }
});

lib.rs:

This is simple Rust crate for managing and querying input state.

It treats the mouse and keyboard as an immutable data structure that you can query to find which keys and buttons are pressed (or where the pointer is).

The core data structures of Mouse and Keyboard are generic, in theory supporting multiple windowing libraries. You can roll your own, or you can enable the winit which has factory methods to easily create a Mouse and Keyboard which work with the winit library.

As stated, the mouse and keyboard are immutable. To track input changes, each provide a begin_frame_input method which return an object you can make changes to for the frame.

Examples

let mut event_loop = winit::event_loop::EventLoop::new();
let mut keyboard = buttons::support::winit::keyboard();
let mut mouse = buttons::support::winit::mouse();
let mut touch = buttons::support::winit::touch();

// Track input
event_loop.run(move |event, _, _| {
    keyboard.handle_event(&event);
    mouse.handle_event(&event);
    touch.handle_event(&event);

    // Check state
    if keyboard.pressed(VirtualKeyCode::Escape)
        || mouse.released(MouseButton::Right)
        || touch.first_touch().is_some()
    {
        // Do something
    }
});

Dependencies

~2–12MB
~145K SLoC