1+ from logging import Logger
2+ from logging import getLogger
13
4+ from pygame .constants import K_BACKSPACE
5+ from pygame .constants import K_DELETE
26from pygame .locals import K_LEFT
37from pygame .locals import K_RIGHT
48from pygame .locals import K_TAB
9+
10+ from pygame .event import Event
11+
512from pygame import draw
613
714from albow .utils import overridable_property
@@ -17,6 +24,8 @@ class TextEditor(Widget):
1724
1825 .. Note::
1926 There is currently no support for selecting or copying and pasting text.
27+ In pygame 2.0.0.dev10, the backspace and delete keycodes return an empty string
28+ as their unicode representation.
2029 """
2130
2231 text = overridable_property ('text' )
@@ -49,6 +58,8 @@ class TextEditor(Widget):
4958 """
5059 _text = ""
5160
61+ clsLogger : Logger = getLogger (__name__ )
62+
5263 def __init__ (self , width : int , upper : bool = None , ** kwds ):
5364 """
5465 The height is determined by the height of a line of text in the font in effect at construction time.
@@ -72,12 +83,12 @@ def get_text(self):
7283
7384 def set_text (self , theNewText ):
7485 """
75- Internally, the widget uses these methods to access the text being edited. By default they access text held
76- in a private attribute. By overriding them, you can arrange for the widget to edit text being held
86+ Internally, the widget uses these methods to access the text being edited. By default they access text held
87+ in a private attribute. By overriding them, you can arrange for the widget to edit text being held
7788 somewhere else.
78-
89+
7990 Args:
80- theNewText:
91+ theNewText:
8192
8293 """
8394 self ._text = theNewText
@@ -98,9 +109,13 @@ def draw(self, surface):
98109 y = frame .top
99110 draw .line (surface , fg , (x , y ), (x , y + h - 1 ))
100111
101- def key_down (self , theKeyEvent ):
112+ def key_down (self , theKeyEvent : Event ):
113+
102114 if not (theKeyEvent .cmd or theKeyEvent .alt ):
115+
103116 k = theKeyEvent .key
117+ self .logger .debug (f'{ k = } ' )
118+
104119 if k == K_LEFT :
105120 self .move_insertion_point (- 1 )
106121 return
@@ -111,14 +126,17 @@ def key_down(self, theKeyEvent):
111126 self .attention_lost ()
112127 self .tab_to_next ()
113128 return
129+ if k == K_BACKSPACE :
130+ self .handleDelete ()
131+ return
132+ if k == K_DELETE :
133+ self .handleDelete ()
134+ return
135+
114136 try :
115137 c = theKeyEvent .unicode
116138 except ValueError :
117139 c = ""
118- #
119- # Python 3 update
120- #
121- # if self.insert_char(c) <> 'pass':
122140 if self .insert_char (c ) != 'pass' :
123141 return
124142 if theKeyEvent .cmd and theKeyEvent .unicode :
@@ -144,21 +162,13 @@ def move_insertion_point(self, d):
144162 self .insertionPoint = i
145163
146164 def insert_char (self , c ):
165+
166+ self .logger .debug (f'{ c = } ' )
167+
147168 if self .upper :
148169 c = c .upper ()
149170 if c <= "\x7f " :
150- if c == "\x08 " or c == "\x7f " :
151- text , i = self .get_text_and_insertion_point ()
152- if i is None :
153- text = ""
154- i = 0
155- else :
156- text = text [:i - 1 ] + text [i :]
157- i -= 1
158- self .change_text (text )
159- self .insertionPoint = i
160- return
161- elif c == "\r " or c == "\x03 " :
171+ if c == "\r " or c == "\x03 " :
162172 return self .call_handler ('enter_action' )
163173 elif c == "\x1b " :
164174 return self .call_handler ('escape_action' )
@@ -176,6 +186,19 @@ def insert_char(self, c):
176186 return
177187 return 'pass'
178188
189+ def handleDelete (self ):
190+
191+ text , i = self .get_text_and_insertion_point ()
192+ if i is None :
193+ text = ""
194+ i = 0
195+ else :
196+ text = text [:i - 1 ] + text [i :]
197+ i -= 1
198+ self .change_text (text )
199+ self .insertionPoint = i
200+
201+ # noinspection PyUnusedLocal
179202 def allow_char (self , c ):
180203 """
181204 This method meant to be overridden
0 commit comments