fra*_*pps 6 keyboard keyboard-events rust rust-cargo
我想检测 Rust 中的 keydown 事件,然后检查是否按下了组合键,以便基于此进行进一步的操作。所以在我的 Rust 应用程序中基本上支持键盘快捷键。
我看过一些板条箱,例如ncurses,但它们不符合我的要求......
Ejd*_*ien 20
如果您不需要 Windows 支持,那么最好是termion。
它是一个用于操作终端的库。您可以在其中检测按键事件甚至键盘快捷键。而且它真的很轻!只有22.78 kB(从 1.5.5 版开始)。
这是我整理的一个快速程序,用于展示一些快捷方式。
将此代码添加到main.rs,添加termion = "1.5.5"到Cargo.toml并以cargo run!
use std::io::{stdin, stdout, Write};
use termion::event::Key;
use termion::input::TermRead;
use termion::raw::IntoRawMode;
fn main() {
let stdin = stdin();
//setting up stdout and going into raw mode
let mut stdout = stdout().into_raw_mode().unwrap();
//printing welcoming message, clearing the screen and going to left top corner with the cursor
write!(stdout, r#"{}{}ctrl + q to exit, ctrl + h to print "Hello world!", alt + t to print "termion is cool""#, termion::cursor::Goto(1, 1), termion::clear::All)
.unwrap();
stdout.flush().unwrap();
//detecting keydown events
for c in stdin.keys() {
//clearing the screen and going to top left corner
write!(
stdout,
"{}{}",
termion::cursor::Goto(1, 1),
termion::clear::All
)
.unwrap();
//i reckon this speaks for itself
match c.unwrap() {
Key::Ctrl('h') => println!("Hello world!"),
Key::Ctrl('q') => break,
Key::Alt('t') => println!("termion is cool"),
_ => (),
}
stdout.flush().unwrap();
}
}
Run Code Online (Sandbox Code Playgroud)
如果您需要支持 Windows 和所有其他平台,那么您可以使用crossterm。这是一个相当不错的库,比 termion 重得多。它是98.06 kB(从 0.16.0 版本开始)。
这是与上面相同的程序,但使用 crossterm 编写。
将此代码添加到main.rs,添加crossterm = "0.16.0"到Cargo.toml并尝试使用cargo run!
//importing in execute! macro
#[macro_use]
extern crate crossterm;
use crossterm::cursor;
use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers};
use crossterm::style::Print;
use crossterm::terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType};
use std::io::{stdout, Write};
fn main() {
let mut stdout = stdout();
//going into raw mode
enable_raw_mode().unwrap();
//clearing the screen, going to top left corner and printing welcoming message
execute!(stdout, Clear(ClearType::All), cursor::MoveTo(0, 0), Print(r#"ctrl + q to exit, ctrl + h to print "Hello world", alt + t to print "crossterm is cool""#))
.unwrap();
//key detection
loop {
//going to top left corner
execute!(stdout, cursor::MoveTo(0, 0)).unwrap();
//matching the key
match read().unwrap() {
//i think this speaks for itself
Event::Key(KeyEvent {
code: KeyCode::Char('h'),
modifiers: KeyModifiers::CONTROL,
//clearing the screen and printing our message
}) => execute!(stdout, Clear(ClearType::All), Print("Hello world!")).unwrap(),
Event::Key(KeyEvent {
code: KeyCode::Char('t'),
modifiers: KeyModifiers::ALT,
}) => execute!(stdout, Clear(ClearType::All), Print("crossterm is cool")).unwrap(),
Event::Key(KeyEvent {
code: KeyCode::Char('q'),
modifiers: KeyModifiers::CONTROL,
}) => break,
_ => (),
}
}
//disabling raw mode
disable_raw_mode().unwrap();
}
Run Code Online (Sandbox Code Playgroud)
我不会撒谎,这比termion解决方案更难阅读,但它做同样的工作。我以前没有经验,crossterm所以这段代码实际上可能不是最好的,但它很不错。
正在寻找一种仅检测按键而没有任何修饰符 ( Shift, Control, Alt) 的方法?检查这个简化的代码:
//-- code --
let no_modifiers = KeyModifiers::empty();
loop {
//--code--
match read().unwrap() {
Event::Key(KeyEvent {
code: KeyCode::Char('a'),
modifiers: no_modifiers,
}) => //--code--
}
//--code--
}
//--code--
Run Code Online (Sandbox Code Playgroud)
这里的重要部分是no_modifiers变量的创建。由于我们不想使用任何修饰符,因此创建KeyModifiers没有任何修饰符的实例的唯一方法是调用empty()不包含任何修饰符的函数。我们需要这个变量是因为我们不能在模式中调用函数(在这种情况下是结构初始化)。
小智 7
您可以使用控制台作为简单的跨平台解决方案。
use console::Term;
fn main() {
let stdout = Term::buffered_stdout();
'game_loop: loop {
if let Ok(character) = stdout.read_char() {
match character {
'w' => todo!("Up"),
'a' => todo!("Left"),
's' => todo!("Down"),
'd' => todo!("Right"),
_ => break 'game_loop,
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码片段显示了匹配平台常见移动字符的基本示例。
| 归档时间: |
|
| 查看次数: |
6334 次 |
| 最近记录: |