@@ -106,6 +106,7 @@ def setText(self, text, cursorPos=0x1000):
106106 if text != self ._text :
107107 self .textChanged .emit (text )
108108 self ._text = TTkString (text )
109+ if cursorPos != self ._cursorPos :
109110 self ._cursorPos = max (0 ,min (cursorPos , len (text )))
110111 self ._pushCursor ()
111112
@@ -290,38 +291,43 @@ def keyEvent(self, evt:TTkKeyEvent) -> bool:
290291 return super ().keyEvent (evt )
291292 return True
292293
294+ text = self ._text
295+ cursorPos = self ._cursorPos
296+
293297 if evt .key == TTkK .Key_Left :
294298 if self ._selectionFrom < self ._selectionTo :
295- self . _cursorPos = self ._selectionTo
296- self . _cursorPos = self ._text .prevPos (self ._cursorPos )
299+ cursorPos = self ._selectionTo
300+ cursorPos = self ._text .prevPos (self ._cursorPos )
297301 elif evt .key == TTkK .Key_Right :
298302 if self ._selectionFrom < self ._selectionTo :
299- self . _cursorPos = self ._selectionTo - 1
300- self . _cursorPos = self ._text .nextPos (self ._cursorPos )
303+ cursorPos = self ._selectionTo - 1
304+ cursorPos = self ._text .nextPos (self ._cursorPos )
301305 elif evt .key == TTkK .Key_End :
302- self . _cursorPos = len (self ._text )
306+ cursorPos = len (self ._text )
303307 elif evt .key == TTkK .Key_Home :
304- self . _cursorPos = 0
308+ cursorPos = 0
305309 elif evt .key == TTkK .Key_Insert :
306310 self ._replace = not self ._replace
307311 elif evt .key == TTkK .Key_Delete :
308312 if self ._selectionFrom < self ._selectionTo :
309- self . _text = self ._text .substring (to = self ._selectionFrom ) + self ._text .substring (fr = self ._selectionTo )
310- self . _cursorPos = self ._selectionFrom
313+ text = self ._text .substring (to = self ._selectionFrom ) + self ._text .substring (fr = self ._selectionTo )
314+ cursorPos = self ._selectionFrom
311315 else :
312- self . _text = self ._text .substring (to = self ._cursorPos ) + self ._text .substring (fr = self ._text .nextPos (self ._cursorPos ))
316+ text = self ._text .substring (to = self ._cursorPos ) + self ._text .substring (fr = self ._text .nextPos (self ._cursorPos ))
313317 elif evt .key == TTkK .Key_Backspace :
314318 if self ._selectionFrom < self ._selectionTo :
315- self . _text = self ._text .substring (to = self ._selectionFrom ) + self ._text .substring (fr = self ._selectionTo )
316- self . _cursorPos = self ._selectionFrom
319+ text = self ._text .substring (to = self ._selectionFrom ) + self ._text .substring (fr = self ._selectionTo )
320+ cursorPos = self ._selectionFrom
317321 elif self ._cursorPos > 0 :
318322 prev = self ._text .prevPos (self ._cursorPos )
319- self . _text = self ._text .substring (to = prev ) + self ._text .substring (fr = self ._cursorPos )
320- self . _cursorPos = prev
323+ text = self ._text .substring (to = prev ) + self ._text .substring (fr = self ._cursorPos )
324+ cursorPos = prev
321325
322326 if ( self ._inputType & TTkK .Input_Number and
323327 not self ._isFloat (self ._text ) ):
324328 self .setText ('0' , 1 )
329+ else :
330+ self .setText (text , cursorPos )
325331
326332 self ._pushCursor ()
327333
0 commit comments