From 37e54b35983299b7324ada19b8600269e16995ac Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Thu, 28 May 2015 13:49:13 -0700 Subject: [PATCH] Add input support for Esc --- src/event.rs | 5 +++++ src/lib.rs | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/event.rs b/src/event.rs index 229bef0..482e9f8 100644 --- a/src/event.rs +++ b/src/event.rs @@ -39,6 +39,9 @@ pub enum Key { Del, /// Insert key Ins, + /// Escape key. Often buffered by the terminal, + /// may appear with a delay or along with the next key. + Esc, F1, F2, F3, @@ -63,6 +66,7 @@ impl Key { 9 => Key::Tab, // Treat Return and the numpad Enter the same 10 | ncurses::KEY_ENTER => Key::Enter, + 27 => Key::Esc, 127 => Key::Backspace, 330 => Key::Del, 331 => Key::Ins, @@ -112,6 +116,7 @@ impl fmt::Display for Key { Key::Enter => "Enter", Key::Tab => "Tab", Key::Ins => "Ins", + Key::Esc => "Esc", Key::F1 => "F1", Key::F2 => "F2", Key::F3 => "F3", diff --git a/src/lib.rs b/src/lib.rs index 29028c0..72e8378 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,6 +66,8 @@ pub struct Cursive { impl Cursive { /// Creates a new Cursive root, and initialize ncurses. 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::initscr(); ncurses::keypad(ncurses::stdscr, true); @@ -199,7 +201,7 @@ impl Cursive { } fn poll_event() -> Event { - let ch = ncurses::getch(); + let ch: i32 = ncurses::getch(); // Is it a UTF-8 starting point? if 32 <= ch && ch < 0x100 && ch != 127 { @@ -226,14 +228,12 @@ impl Cursive { // (Is this getting repetitive? :p) self.draw(); - // Blocks until the user press a key. - // TODO: Add a timeout? Animations? + // Wait for next event. + // (If set_fps was called, this returns -1 now and then) 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) { + // If the event was ignored, it is our turn to play with it. EventResult::Ignored => self.on_event(event), EventResult::Consumed(None) => (), EventResult::Consumed(Some(cb)) => cb(self),