embedded-graphics-keyboard 0.2.0

no_std on-screen keyboard and text field widgets for embedded-graphics, driven by buttons or touch
Documentation

embedded-graphics-keyboard

no_std on-screen keyboard and text field widgets for embedded-graphics, driven by navigation buttons and/or a touch screen.

QWERTY keyboard

Features

  • QWERTY layout with a digits and symbols page, Shift (one-shot, double press for caps lock) and Space, Backspace, Back and Enter keys.
  • Custom layouts: any number of pages, rows and keys, defined in consts.
  • Fits any screen: every letter key has the same size, computed from the given bounds; the bottom action row spans the full width. Keyboard::keys_bounds returns the area really covered by the keys, to align other widgets on them.
  • 5-button navigation (Up, Down, Left, Right, Validate) and touch input, both usable at the same time.
  • Text field with a title above its frame, a blinking cursor and horizontal scrolling of long values.
  • Fully styleable: one KeyStyle (background, border, corner radius and font) per key state, FrameStyle for the frames. Icons through an icon font.
  • Font agnostic: works with MonoTextStyle, and with embedded-graphics-glyphix fonts behind the glyphix feature. Implement FontStyle for anything else.
  • Checked up front: Keyboard::new fails if a label does not fit inside its key, instead of drawing garbage.
  • Partial redraw: Keyboard::redraw only sends the keys that changed (usually two) to the display, which matters on SPI/I2C screens.
  • No allocation, no unsafe.
Digits & symbols Caps lock, light theme
800x480, framed keyboard, outlined keys 320x240

Usage

[dependencies]
embedded-graphics-keyboard = "0.1"
use embedded_graphics::{
    mono_font::{ascii::FONT_10X20, MonoTextStyle},
    pixelcolor::Rgb565,
    prelude::*,
    primitives::Rectangle,
};
use embedded_graphics_keyboard::{
    FrameStyle, KeyEvent, KeyStyle, Keyboard, KeyboardConfig, KeyboardTheme, NavButton,
    TextField, TextFieldStyle,
};

fn run<D: DrawTarget<Color = Rgb565>>(display: &mut D) -> Result<(), D::Error> {
    let white_text = MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE);
    let black_text = MonoTextStyle::new(&FONT_10X20, Rgb565::BLACK);

    // One style per key state, the others derived from the idle one.
    let idle = KeyStyle::new(white_text)
        .with_fill(Rgb565::CSS_DARK_SLATE_GRAY)
        .with_border(Rgb565::CSS_GRAY, 1)
        .with_corner_radius(8);
    let theme = KeyboardTheme {
        idle,
        selected: idle.with_fill(Rgb565::CSS_DODGER_BLUE).with_font(black_text),
        pressed: idle.with_fill(Rgb565::WHITE).with_font(black_text),
    };

    let area = Rectangle::new(Point::new(4, 76), Size::new(472, 240));
    let mut keyboard = Keyboard::new(KeyboardConfig::new(area, theme))
        .expect("every label fits inside its key");

    // Text field with a title, aligned on the keys.
    let field_style = TextFieldStyle::new(white_text, white_text).with_frame(
        FrameStyle::new()
            .with_fill(Rgb565::BLACK)
            .with_border(Rgb565::YELLOW, 2)
            .with_corner_radius(8),
    );
    let keys = keyboard.keys_bounds();
    let field_bounds = Rectangle::new(
        Point::new(keys.top_left.x, 4),
        Size::new(keys.size.width, field_style.height(true)),
    );

    let mut text = String::new();
    // In your event loop: feed button presses or touches...
    if let Some(event) = keyboard.handle_button(NavButton::Validate) {
        match event {
            KeyEvent::Char(c) => text.push(c),
            KeyEvent::Backspace => drop(text.pop()),
            KeyEvent::Enter => { /* submit */ }
            KeyEvent::Back => { /* cancel */ }
            KeyEvent::CursorLeft | KeyEvent::CursorRight => { /* see with_field_navigation */ }
        }
    }
    keyboard.release();

    // ...then draw.
    TextField::new(field_bounds, &text, &field_style)
        .with_title("Wi-Fi password")
        .draw(display)?;
    // Only the keys that changed since the last call are sent to the display.
    keyboard.redraw(display, Rgb565::BLACK)
}

See examples/simulator.rs for a complete interactive demo, and examples/screenshots.rs for the configurations shown above.

cargo run --example simulator            # 480x320
cargo run --example simulator -- 800 480 # any screen size

The examples need SDL2 (libsdl2-dev on Debian/Ubuntu).

For real hardware, examples/stm32f767zi runs the keyboard on a NUCLEO-F767ZI with an ILI9488 SPI display (mipidsi, Embassy) and five push buttons.

Custom layouts

use embedded_graphics_keyboard::{KeyAction, Layout, Page};

const KEYPAD: Layout = Layout::new(&[Page::new(
    "123",
    &[
        &KeyAction::chars(b"123"),
        &KeyAction::chars(b"456"),
        &KeyAction::chars(b"789"),
        &[KeyAction::Backspace, KeyAction::Char('0'), KeyAction::Enter],
    ],
)]);

Pass it with KeyboardConfig::with_layout, and change the control key labels with KeyboardConfig::with_labels.

Cargo features

  • glyphix: FontStyle implementation for embedded_graphics_glyphix::GlyphixStyle.

Minimum supported Rust version

Rust 1.85 (edition 2024).

License

Licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.