我正在尝试 Rust,到目前为止我真的很喜欢它。我正在开发一个需要从用户那里获取箭头键输入的工具。到目前为止,我已经完成了一些工作:如果我按住一个键一会儿,相关的函数就会被调用。然而,这远非一蹴而就。
到目前为止我得到了什么:
let mut stdout = io::stdout().into_raw_mode();
let mut stdin = termion::async_stdin();
// let mut stdin = io::stdin();
let mut it = stdin.keys(); //iterator object
loop {
//copied straight from GitLab: https://gitlab.redox-os.org/redox-os/termion/-/issues/168
let b = it.next();
match b {
Some(x) => match x {
Ok(k) => {
match k {
Key::Left => move_cursor(&mut cursor_char, -1, &enc_chars, &mpt, &status),
Key::Right => move_cursor(&mut cursor_char, 1, &enc_chars, &mpt, &status),
Key::Ctrl('c') => break,
_ => {}
}
},
_ => {}
},
None => {}
}
//this loop might do nothing if no recognized key was pressed.
}
我自己也不是很明白。我正在使用终端原始模式,如果这与它有任何关系。我看过rustyline crate,但这真的不好,因为它更像是一个交互式外壳,我只想检测按键。