Commits

Henning Schröder committed f0eee27

keyboard navigation in lineedit

  • Participants
  • Parent commits 646ea86

Comments (0)

Files changed (3)

         
         if keys in umlauts:
             return False, umlauts[keys]
-
+        if keys == "\x7f":
+            return True, "backspace"
         if keys.startswith(chr(27)):
             xkeys = keymap.get(keys[1:], None)
             if xkeys:
     c.text(1,1, "hello, world!")
     i = 0
     while True:
-        keys = c.readkeys()
-        if keys:
-            if keys in keymap:
-                keys = keymap[keys]
+        extended, keys = c.xreadkeys()
+        if keys or extended:
             i += 1
-            c.text(i%23, 1, "%r  " % keys)
+            c.text(1, i, "%r  %s   " % (keys, extended))
     c.close()
 
         if isinstance(event, TInvokeEvent):
             event.slot(*event.args)
 
-    
+
     def eventFilter(self, obj, event):
         pass
     
     Key_Return = "\n"
     Key_Enter = "\n"
     Key_Space = " "
+    Key_Backspace = "backspace"
+    Key_Left = "left"
+    Key_Right = "right"
 
 
 
 class TKeyEvent(TEvent):
 
 
-    def __init__(self, key, modifiers=None):
+    def __init__(self, key, text=None, modifiers=None):
         super(TKeyEvent, self).__init__(TEventTypes.KeyPress)
         self.key = key
+        self.text = text
         self.modifiers = modifiers
 
 
     def poll(self):
         extended, keys = self.xreadkeys()
         if keys:
-            #Console.instance.text(1,1, "poll %r    " % keys)
-            self.focusWidget().postEvent(TKeyEvent(keys))
+            if extended:
+                event = TKeyEvent(key=keys)
+            else:
+                event = TKeyEvent(key=keys, text=keys)
+            self.focusWidget().postEvent(event)
 
     
     def setFocusWidget(self, widget):
     
     def paintEvent(self, event):
         painter = TPainter(self)
-        text = self.text()[self._cursor_offset:]
-        painter.drawText(self.pos(), text)
+        text = self.text()[self._cursor_offset:self.width()]
+        display_text = text.ljust(self.width())
+        painter.drawText(self.pos(), display_text)
 
         
     def keyPressEvent(self, event):
-        if event.key:
+        if event.key or event.text:
             text = self.text()
             cpos = self.cursorPosition()
-            new_text = text[:cpos] + event.key + text[cpos:]
-            self.setText(new_text)
-            self.setCursorPosition(cpos + 1)
+            if event.text:
+                new_text = text[:cpos] + event.key + text[cpos:]
+                self.setText(new_text)
+                self.setCursorPosition(cpos + 1)
+            elif event.key == TKey.Key_Backspace:
+                if cpos > 0:
+                    new_text = text[:cpos-1] + text[cpos:]
+                    self.setText(new_text)
+                    self.setCursorPosition(cpos + 1)                    
+            elif event.key == TKey.Key_Left:
+                if cpos > 0:
+                    self.setCursorPosition(cpos - 1)
+            elif event.key == TKey.Key_Right:
+                if cpos < len(self.text()):
+                    self.setCursorPosition(cpos + 1)
+
+            else:
+                print repr(event.key)
 
         super(TLineEdit, self).keyPressEvent(event)