      TTYChar *ptr;
      int fgcolor, w, n;
      unsigned int cc;
-@@ -560,7 +560,7 @@
+@@ -560,7 +574,7 @@
  
  static void term_flush(QEditScreen *s)
  {

     int x2 = x1 + w;
     int y2 = y1 + h;
     int x, y;
@@ -443,7 +443,7 @@
         return NULL;
     font->ascent = 0;
     font->descent = 1;

     int ch;
     QEEvent ev1, *ev = &ev1;
 
@@ -312,6 +312,7 @@
             ts->input_param = 0;
         } else if (ch == 'O') {
             ts->input_state = IS_ESC2;
+            ts->input_param = 0;
         } else {
             ch = KEY_META(ch);
             ts->input_state = IS_NORM;
@@ -347,13 +348,26 @@
         break;
     case IS_ESC2:
         /* xterm fn */
-        ts->input_state = IS_NORM;
-        if (ch >= 'P' && ch <= 'S') {
-            ch = KEY_F1 + ch - 'P';
-        the_end:
-            ev->key_event.type = QE_KEY_EVENT;
-            ev->key_event.key = ch;
-            qe_handle_event(ev);
+        if (!isdigit(ch)) {
+            ts->input_state = IS_NORM;
+            if (ts->input_param == 5 && ch >= 'A' && ch <= 'D') {
+                /* CTRL-arrows variant */
+                ch += 'a' - 'A';
+                ch = KEY_ESC1(ch);
+                goto the_end;
+            } else if (ch >= 'A' && ch <= 'D') {
+                /* arrows variant */
+                ch = KEY_ESC1(ch);
+                goto the_end;
+            } else if (ch >= 'P' && ch <= 'S') {
+                ch = KEY_F1 + ch - 'P';
+            the_end:
+                ev->key_event.type = QE_KEY_EVENT;
+                ev->key_event.key = ch;
+                qe_handle_event(ev);
+            }
+        } else {
+        	ts->input_param = ts->input_param * 10 + ch - '0';
         }
         break;
     }
@@ -399,7 +413,7 @@
 static void term_fill_rectangle(QEditScreen *s,
                                 int x1, int y1, int w, int h, QEColor color)
 {
