From d8e5ea32626286d3aaa8d6d75f0789df2646f90c Mon Sep 17 00:00:00 2001
From: Ticki <Ticki@users.noreply.github.com>
Date: Thu, 10 Mar 2016 15:12:59 +0100
Subject: [PATCH] CTRL modified input

---
 examples/keys.rs |  1 +
 src/control.rs   |  3 +++
 src/input.rs     | 12 ++++++++++--
 3 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/examples/keys.rs b/examples/keys.rs
index 7619551b..87ef52fb 100644
--- a/examples/keys.rs
+++ b/examples/keys.rs
@@ -20,6 +20,7 @@ fn main() {
             Key::Char('q') => break,
             Key::Char(c) => println!("{}", c),
             Key::Alt(c) => println!("^{}", c),
+            Key::Ctrl(c) => println!("*{}", c),
             Key::Left => println!("←"),
             Key::Right => println!("→"),
             Key::Up => println!("↑"),
diff --git a/src/control.rs b/src/control.rs
index 7d5a09fd..7b65619d 100644
--- a/src/control.rs
+++ b/src/control.rs
@@ -43,6 +43,9 @@ pub trait TermWrite {
     fn hide_cursor(&mut self) -> IoResult<usize> {
         self.csi(b"?25l")
     }
+
+    // TODO insert mode
+
     /// Reset the rendition mode.
     ///
     /// This will reset both the current style and color.
diff --git a/src/input.rs b/src/input.rs
index b6fddbe3..b2f76711 100644
--- a/src/input.rs
+++ b/src/input.rs
@@ -17,10 +17,14 @@ pub enum Key {
     Up,
     /// Down arrow.
     Down,
-    /// Alt modified character.
-    Alt(char),
     /// Normal character.
     Char(char),
+    /// Alt modified character.
+    Alt(char),
+    /// Ctrl modified character.
+    ///
+    /// Note that certain keys may not be modifiable with `ctrl`, due to limitations of terminals.
+    Ctrl(char),
     /// Invalid character code.
     Invalid,
     // TODO handle errors better?
@@ -52,6 +56,10 @@ impl<I: Iterator<Item = Result<char, CharsError>>> Iterator for Keys<I> {
                 Some(Err(_)) | None => Key::Invalid,
             }),
             Some(Ok('\x7F')) => Some(Key::Backspace),
+            Some(Ok(c @ '\x10' ... '\x1A')) => Some(Key::Ctrl((c as u8 - 0x10 + b'p') as char)),
+            Some(Ok(c @ '\x01' ... '\x04')) => Some(Key::Ctrl((c as u8 - 0x1  + b'a') as char)),
+            Some(Ok(c @ '\x1C' ... '\x1F')) => Some(Key::Ctrl((c as u8 - 0x1C + b'4') as char)),
+            Some(Ok('\x06')) => Some(Key::Alt('f')),
             Some(Ok(c)) => Some(Key::Char(c)),
             None => None,
             Some(Err(_)) => Some(Key::Error),
-- 
GitLab