如何从stdin中读取一个字符而不必输入?

问题描述 投票:23回答:3

我想运行一个在stdin上阻塞的可执行文件,当按下一个键时,会立即打印相同的字符,而不必按Enter键。

如何从stdin读取一个字符而不必按Enter键?我从这个例子开始:

fn main() {
    println!("Type something!");

    let mut line = String::new();
    let input = std::io::stdin().read_line(&mut line).expect("Failed to read line");

    println!("{}", input);
}

我查看了API并尝试用read_line()替换bytes(),但我尝试的所有内容都要求我在读取之前按Enter键。

这个问题被要求用于C / C ++,但似乎没有标准的方法来做到这一点:Capture characters from standard input without waiting for enter to be pressed

考虑到它在C / C ++中并不简单,它在Rust中可能不可行。

rust
3个回答
12
投票

使用现在可用的'ncurses'库之一,例如this库。

在Cargo中添加依赖项

[dependencies]
ncurses = "5.86.0"

并包含在main.rs中:

extern crate ncurses;
use ncurses::*; // watch for globs

按照库中的示例初始化ncurses并等待单个字符输入,如下所示:

initscr();
/* Print to the back buffer. */
printw("Hello, world!");

/* Update the screen. */
refresh();

/* Wait for a key press. */
getch();

/* Terminate ncurses. */
endwin();

11
投票

虽然@ Jon使用ncurses的解决方案可行,但ncurses会按设计清除屏幕。我想出了这个解决方案,它使用termios crate为我的小项目学习Rust。想法是通过termios绑定访问ECHO来修改ICANONtcsetattr标志。

extern crate termios;
use std::io;
use std::io::Read;
use std::io::Write;
use termios::{Termios, TCSANOW, ECHO, ICANON, tcsetattr};

fn main() {
    let stdin = 0; // couldn't get std::os::unix::io::FromRawFd to work 
                   // on /dev/stdin or /dev/tty
    let termios = Termios::from_fd(stdin).unwrap();
    let mut new_termios = termios.clone();  // make a mutable copy of termios 
                                            // that we will modify
    new_termios.c_lflag &= !(ICANON | ECHO); // no echo and canonical mode
    tcsetattr(stdin, TCSANOW, &mut new_termios).unwrap();
    let stdout = io::stdout();
    let mut reader = io::stdin();
    let mut buffer = [0;1];  // read exactly one byte
    print!("Hit a key! ");
    stdout.lock().flush().unwrap();
    reader.read_exact(&mut buffer).unwrap();
    println!("You have hit: {:?}", buffer);
    tcsetattr(stdin, TCSANOW, & termios).unwrap();  // reset the stdin to 
                                                    // original termios data
}

读取单个字节的一个优点是捕获箭头键,ctrl等。不捕获扩展的F键(尽管ncurses可以捕获这些)。

此解决方案适用于类UNIX平台。我没有Windows的经验,但根据这个forum,在Windows中使用SetConsoleMode也许可以实现类似的东西。


1
投票

你也可以使用termion,但是你必须启用raw TTY mode来改变stdout的行为。请参阅下面的示例(使用Rust 1.34.0进行测试)。请注意,在内部,它还包装了termios UNIX API。

Cargo.toml

[dependencies]
termion = "1.5.2"

main.rs

use std::io;
use std::io::Write;
use std::thread;
use std::time;

use termion;
use termion::input::TermRead;
use termion::raw::IntoRawMode;

fn main() {
    // Set terminal to raw mode to allow reading stdin one key at a time
    let mut stdout = io::stdout().into_raw_mode().unwrap();

    // Use asynchronous stdin
    let mut stdin = termion::async_stdin().keys();

    loop {
        // Read input (if any)
        let input = stdin.next();

        // If a key was pressed
        if let Some(Ok(key)) = input {
            match key {
                // Exit if 'q' is pressed
                termion::event::Key::Char('q') => break,
                // Else print the pressed key
                _ => {
                    write!(
                        stdout,
                        "{}{}Key pressed: {:?}",
                        termion::clear::All,
                        termion::cursor::Goto(1, 1),
                        key
                    )
                    .unwrap();

                    stdout.lock().flush().unwrap();
                }
            }
        }
        thread::sleep(time::Duration::from_millis(50));
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.