mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-09 19:00:46 +00:00
Add key_codes example
Prints the code on key press. Useful tool.
This commit is contained in:
parent
9ab4848180
commit
ca09885978
@ -40,3 +40,7 @@ path = "examples/mutation.rs"
|
||||
[[example]]
|
||||
name = "edit"
|
||||
path = "examples/edit.rs"
|
||||
|
||||
[[example]]
|
||||
name = "key_codes"
|
||||
path = "examples/key_codes.rs"
|
||||
|
48
examples/key_codes.rs
Normal file
48
examples/key_codes.rs
Normal file
@ -0,0 +1,48 @@
|
||||
extern crate cursive;
|
||||
|
||||
use cursive::Cursive;
|
||||
|
||||
use cursive::view::{View,BoxView};
|
||||
use cursive::printer::Printer;
|
||||
use cursive::event::EventResult;
|
||||
|
||||
fn main() {
|
||||
let mut siv = Cursive::new();
|
||||
|
||||
siv.add_layer(BoxView::new((10,4), KeyCodeView::new(4)));
|
||||
|
||||
siv.run();
|
||||
}
|
||||
|
||||
struct KeyCodeView {
|
||||
history: Vec<i32>,
|
||||
size: usize,
|
||||
}
|
||||
|
||||
impl KeyCodeView {
|
||||
fn new(size: usize) -> Self {
|
||||
KeyCodeView {
|
||||
history: Vec::new(),
|
||||
size: size,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl View for KeyCodeView {
|
||||
fn draw(&mut self, printer: &Printer, _: bool) {
|
||||
for (y,n) in self.history.iter().enumerate() {
|
||||
printer.print((0,y), &format!("{}", n));
|
||||
}
|
||||
}
|
||||
|
||||
fn on_key_event(&mut self, ch: i32) -> EventResult {
|
||||
self.history.push(ch);
|
||||
|
||||
while self.history.len() > self.size {
|
||||
self.history.remove(0);
|
||||
}
|
||||
|
||||
EventResult::Consumed(None)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user