Add input support for Esc

This commit is contained in:
Alexandre Bury 2015-05-28 13:49:13 -07:00
parent 6c69453639
commit 37e54b3598
2 changed files with 11 additions and 6 deletions

View File

@ -39,6 +39,9 @@ pub enum Key {
Del, Del,
/// Insert key /// Insert key
Ins, Ins,
/// Escape key. Often buffered by the terminal,
/// may appear with a delay or along with the next key.
Esc,
F1, F1,
F2, F2,
F3, F3,
@ -63,6 +66,7 @@ impl Key {
9 => Key::Tab, 9 => Key::Tab,
// Treat Return and the numpad Enter the same // Treat Return and the numpad Enter the same
10 | ncurses::KEY_ENTER => Key::Enter, 10 | ncurses::KEY_ENTER => Key::Enter,
27 => Key::Esc,
127 => Key::Backspace, 127 => Key::Backspace,
330 => Key::Del, 330 => Key::Del,
331 => Key::Ins, 331 => Key::Ins,
@ -112,6 +116,7 @@ impl fmt::Display for Key {
Key::Enter => "Enter", Key::Enter => "Enter",
Key::Tab => "Tab", Key::Tab => "Tab",
Key::Ins => "Ins", Key::Ins => "Ins",
Key::Esc => "Esc",
Key::F1 => "F1", Key::F1 => "F1",
Key::F2 => "F2", Key::F2 => "F2",
Key::F3 => "F3", Key::F3 => "F3",

View File

@ -66,6 +66,8 @@ pub struct Cursive {
impl Cursive { impl Cursive {
/// Creates a new Cursive root, and initialize ncurses. /// Creates a new Cursive root, and initialize ncurses.
pub fn new() -> Self { pub fn new() -> Self {
// Default delay is way too long. 25 is imperceptible yet works fine.
std::env::set_var("ESCDELAY", "25");
ncurses::setlocale(ncurses::LcCategory::all, ""); ncurses::setlocale(ncurses::LcCategory::all, "");
ncurses::initscr(); ncurses::initscr();
ncurses::keypad(ncurses::stdscr, true); ncurses::keypad(ncurses::stdscr, true);
@ -199,7 +201,7 @@ impl Cursive {
} }
fn poll_event() -> Event { fn poll_event() -> Event {
let ch = ncurses::getch(); let ch: i32 = ncurses::getch();
// Is it a UTF-8 starting point? // Is it a UTF-8 starting point?
if 32 <= ch && ch < 0x100 && ch != 127 { if 32 <= ch && ch < 0x100 && ch != 127 {
@ -226,14 +228,12 @@ impl Cursive {
// (Is this getting repetitive? :p) // (Is this getting repetitive? :p)
self.draw(); self.draw();
// Blocks until the user press a key. // Wait for next event.
// TODO: Add a timeout? Animations? // (If set_fps was called, this returns -1 now and then)
let event = Cursive::poll_event(); let event = Cursive::poll_event();
// Make an event out of it.
// If the event was ignored, it is our turn to play with it.
match self.screen_mut().on_event(event) { match self.screen_mut().on_event(event) {
// If the event was ignored, it is our turn to play with it.
EventResult::Ignored => self.on_event(event), EventResult::Ignored => self.on_event(event),
EventResult::Consumed(None) => (), EventResult::Consumed(None) => (),
EventResult::Consumed(Some(cb)) => cb(self), EventResult::Consumed(Some(cb)) => cb(self),