#console #winapi #windows #wincon #crossterm-winapi

win32console

Provides a wrapper to interact with the windows console from rust

11 releases

0.1.5 Nov 13, 2021
0.1.4 Mar 17, 2020
0.1.3 Feb 16, 2020

#60 in Windows APIs

Download history 19/week @ 2023-11-17 31/week @ 2023-11-24 55/week @ 2023-12-01 27/week @ 2023-12-08 8/week @ 2023-12-15 6/week @ 2023-12-22 1/week @ 2023-12-29 14/week @ 2024-01-05 27/week @ 2024-01-12 22/week @ 2024-01-19 31/week @ 2024-01-26 16/week @ 2024-02-02 72/week @ 2024-02-09 284/week @ 2024-02-16 174/week @ 2024-02-23 165/week @ 2024-03-01

698 downloads per month
Used in asuro-timer

MIT license

180KB
2.5K SLoC

Win32Console

Crates.io Docs.rs Licence

Expose functions to interact with the windows console from Rust.

See: https://docs.microsoft.com/en-us/windows/console/console-functions

Usage

Add this to your Cargo.toml:

[dependencies]
win32console = "0.1.5"

Example

use win32console::console::WinConsole;
use win32console::structs::input::*;

fn main() {
    // Virtual key codes
    // https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
    const ESCAPE : u16 = 0x1B;
    const BACKSPACE: u16 = 0x08;
    const ENTER : u16 = 0x0D;
    const SPACE : u16 = 0x20;

    loop{
        // Get the current input event
        if let KeyEvent(key) = WinConsole::input().read_single_input().unwrap(){
            // Only check for key down events
            if key.key_down{
                let char_value = key.u_char;
                // Write only if is alphanumeric or punctuation
                if char_value.is_ascii_alphanumeric() || char_value.is_ascii_punctuation(){
                    let mut value : [u8; 1] = [0];
                    char_value.encode_utf8(&mut value);
                    WinConsole::output().write_utf8(&value);
                }
                else{
                    match key.virtual_key_code {
                        ESCAPE => { break; },
                        ENTER => { WinConsole::output().write_utf8("\n".as_bytes()); }
                        SPACE => { WinConsole::output().write_utf8(" ".as_bytes()); },
                        BACKSPACE => { WinConsole::output().write_utf8(b"\x08 \x08"); },
                        _ => {}
                    }
                }
            }
        }
    }
}

Implementation

Here a list of the console methods implemented in this library.

And also this library provides functions as:

// Clears the screen
WinConsole::output().clear();

// Reads a 'String' from the console
WinConsole::input().read_string();

// Makes and tone sound
WinConsole::beep(u32, u32);

// Sets the foreground color
WinConsole::output().set_foreground_color(ConsoleColor);

// Sets the background color
WinConsole::output().set_background_color(ConsoleColor);

// Gets the foreground color
WinConsole::output().get_foreground_color();

// Gets the background color
WinConsole::output().get_background_color();

Dependencies

~225KB