Index: code/branches/console/src/libraries/core/IOConsole.cc
===================================================================
--- code/branches/console/src/libraries/core/IOConsole.cc	(revision 5974)
+++ code/branches/console/src/libraries/core/IOConsole.cc	(revision 5975)
@@ -57,5 +57,5 @@
     IOConsole::IOConsole()
         : shell_(Shell::getInstance())
-        , bEscapeMode_(false)
+        , escapeMode_(None)
         , buffer_(Shell::getInstance().getInputBuffer())
     {
@@ -95,51 +95,58 @@
     void IOConsole::update(const Clock& time)
     {
-        unsigned char c;
+        unsigned char c = 0;
         while (read(STDIN_FILENO, &c, 1) == 1)
         {
-            if (this->bEscapeMode_)
+            if (this->escapeMode_ == First && c == '[')
+                this->escapeMode_ = Second;
+            // Get Alt+Tab combination when switching applications
+            else if (this->escapeMode_ == First && c == '\t')
             {
-                this->escapeSequence_ += c;
+                this->buffer_->buttonPressed(KeyEvent(KeyCode::Tab, '\t', KeyboardModifier::Alt));
+                this->escapeMode_ = None;
+            }
+            else if (this->escapeMode_ == Second)
+            {
                 bool endOfSeq = true;
-                if      (this->escapeSequence_ == "[A")
+                this->escapeMode_ = None;
+                if      (this->escapeSequence_ == "A")
                     this->buffer_->buttonPressed(KeyEvent(KeyCode::Up,       0, 0));
-                else if (this->escapeSequence_ == "[B")
+                else if (this->escapeSequence_ == "B")
                     this->buffer_->buttonPressed(KeyEvent(KeyCode::Down,     0, 0));
-                else if (this->escapeSequence_ == "[C")
+                else if (this->escapeSequence_ == "C")
                     this->buffer_->buttonPressed(KeyEvent(KeyCode::Right,    0, 0));
-                else if (this->escapeSequence_ == "[D")
+                else if (this->escapeSequence_ == "D")
                     this->buffer_->buttonPressed(KeyEvent(KeyCode::Left,     0, 0));
-                else if (this->escapeSequence_ == "[1~")
+                else if (this->escapeSequence_ == "1~")
                     this->buffer_->buttonPressed(KeyEvent(KeyCode::Home,     0, 0));
-                else if (this->escapeSequence_ == "[2~")
+                else if (this->escapeSequence_ == "2~")
                     this->buffer_->buttonPressed(KeyEvent(KeyCode::Insert,   0, 0));
-                else if (this->escapeSequence_ == "[3~")
+                else if (this->escapeSequence_ == "3~")
                     this->buffer_->buttonPressed(KeyEvent(KeyCode::Delete,   0, 0));
-                else if (this->escapeSequence_ == "[4~")
+                else if (this->escapeSequence_ == "4~")
                     this->buffer_->buttonPressed(KeyEvent(KeyCode::End,      0, 0));
-                else if (this->escapeSequence_ == "[5~")
+                else if (this->escapeSequence_ == "5~")
                     this->buffer_->buttonPressed(KeyEvent(KeyCode::PageUp,   0, 0));
-                else if (this->escapeSequence_ == "[6~")
+                else if (this->escapeSequence_ == "6~")
                     this->buffer_->buttonPressed(KeyEvent(KeyCode::PageDown, 0, 0));
-                // Get Alt+Tab combination when switching applications
-                else if (this->escapeSequence_ == "\t")
-                    this->buffer_->buttonPressed(KeyEvent(KeyCode::Tab, '\t', KeyboardModifier::Alt));
                 else if (this->escapeSequence_.size() > 4)
-                    endOfSeq = true; // Something went wrong, start over
+                    // User probably very quickly pressed ESC and [
+                    this->escapeMode_ = None;
                 else
-                    endOfSeq = false;
-
-                //this->buffer_->buttonPressed(KeyEvent(KeyCode::Escape,   0, 0));
-
-                if (endOfSeq)
-                    this->bEscapeMode_ = false;
+                    // Waiting for sequence to complete
+                    this->escapeMode_ = Second;
             }
-            else // not in an escape sequence
+            else // not in an escape sequence OR user might have pressed just ESC
             {
+                if (this->escapeMode_ == First)
+                {
+                    this->buffer_->buttonPressed(KeyEvent(KeyCode::Escape, 0, 0));
+                    this->escapeMode_ = None;
+                }
                 if (c == '\033')
-				{
-                    this->bEscapeMode_ = true;
-					this->escapeSequence_.clear();
-				}
+                {
+                    this->escapeMode_ = First;
+                    this->escapeSequence_.clear();
+                }
                 else
                 {
@@ -163,11 +170,15 @@
         }
 
-		// Print input line
-		this->printInputLine();
+        // If there is still an escape key pending (escape key ONLY), then
+        // it sure isn't an escape sequence here
+        this->buffer_->buttonPressed(KeyEvent(KeyCode::Escape, 0, 0));
+
+        // Print input line
+        this->printInputLine();
     }
 
     void IOConsole::printOutputLine(const std::string& line)
     {
-		COUT(0) << "print output" << std::endl;
+        COUT(0) << "print output" << std::endl;
         // Save cursor position
         std::cout << "\033[s";
@@ -206,5 +217,5 @@
         std::cout.flush();
 
-		this->printInputLine();
+        this->printInputLine();
     }
 
@@ -215,6 +226,6 @@
         // print status line
         //std::cout << std::fixed << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgFPS() << " fps, " << std::setprecision(2) << std::setw(5) << Game::getInstance().getAvgTickTime() << " ms avg ticktime # ";
-		// Show an arrow to indicate a command prompt
-		std::cout << ">";
+        // Show an arrow to indicate a command prompt
+        std::cout << ">";
         // save cursor position
         std::cout << "\033[s";
Index: code/branches/console/src/libraries/core/IOConsole.h
===================================================================
--- code/branches/console/src/libraries/core/IOConsole.h	(revision 5974)
+++ code/branches/console/src/libraries/core/IOConsole.h	(revision 5975)
@@ -53,4 +53,11 @@
 
     private:
+        enum EscapeMode
+        {
+            None,
+            First,
+            Second
+        };
+
         void setTerminalMode();
         static void resetTerminalMode();
@@ -68,5 +75,5 @@
 
         Shell&                  shell_;
-        bool                    bEscapeMode_;
+        EscapeMode              escapeMode_;
         std::string             escapeSequence_;
         InputBuffer*            buffer_;
