Skip to content

Commit

Permalink
Fixed textinput event cursor selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ppizarror committed Aug 7, 2019
1 parent 7f04af5 commit 8eb4c8a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
13 changes: 11 additions & 2 deletions pygameMenu/widgets/textinput.py
Expand Up @@ -177,7 +177,6 @@ def __init__(self,
_ctrl.KEY_MOVE_DOWN,
_ctrl.KEY_MOVE_UP,
_pygame.K_CAPSLOCK,
_pygame.K_ESCAPE,
_pygame.K_LCTRL,
_pygame.K_LSHIFT,
_pygame.K_NUMLOCK,
Expand Down Expand Up @@ -847,9 +846,14 @@ def _update_cursor_mouse(self, mousex):
# Text does not have ellipsis, infered position is correct
else:
self._cursor_position = cursor_pos
if self._maxwidth != 0: # Update renderbox
self._cursor_position += self._renderbox[0]
self._renderbox[2] = cursor_pos
self._update_maxlimit_renderbox()

if self._selection_mouse_first_position == -1:
if self._selection_active:
if self._selection_active: # Unselect and select again
self._unselect_text()
self._selection_mouse_first_position = self._cursor_position
else:
a = self._selection_mouse_first_position
Expand Down Expand Up @@ -1411,6 +1415,11 @@ def update(self, events):
self._unselect_text()
updated = True

# Escape
elif event.key == _pygame.K_ESCAPE:
self._unselect_text()
updated = True

# Press lshift, rshift -> selection
elif event.key == _pygame.K_LSHIFT or event.key == _pygame.K_RSHIFT:
if not self._selection_active:
Expand Down
2 changes: 1 addition & 1 deletion test/_utils.py
Expand Up @@ -111,7 +111,7 @@ def joy_key(key, inlist=True, testmode=True):
return event_obj

@staticmethod
def key(key, char='', inlist=True, keydown=False, keyup=False, testmode=True):
def key(key, char=' ', inlist=True, keydown=False, keyup=False, testmode=True):
"""
Create a keyboard event.
Expand Down
17 changes: 9 additions & 8 deletions test/test_widgets.py
Expand Up @@ -124,14 +124,14 @@ def test_textinput(self):

# Assert events
textinput.update(PygameUtils.key(0, keydown=True, testmode=False))
textinput.update(PygameUtils.key(pygame.K_BACKSPACE, keydown=True, char=' '))
textinput.update(PygameUtils.key(pygame.K_DELETE, keydown=True, char=' '))
textinput.update(PygameUtils.key(pygame.K_LEFT, keydown=True, char=' '))
textinput.update(PygameUtils.key(pygame.K_RIGHT, keydown=True, char=' '))
textinput.update(PygameUtils.key(pygame.K_END, keydown=True, char=' '))
textinput.update(PygameUtils.key(pygame.K_HOME, keydown=True, char=' '))
textinput.update(PygameUtils.key(pygameMenu.controls.KEY_APPLY, keydown=True, char=' '))
textinput.update(PygameUtils.key(pygame.K_LSHIFT, keydown=True, char=' '))
textinput.update(PygameUtils.key(pygame.K_BACKSPACE, keydown=True,))
textinput.update(PygameUtils.key(pygame.K_DELETE, keydown=True))
textinput.update(PygameUtils.key(pygame.K_LEFT, keydown=True))
textinput.update(PygameUtils.key(pygame.K_RIGHT, keydown=True))
textinput.update(PygameUtils.key(pygame.K_END, keydown=True))
textinput.update(PygameUtils.key(pygame.K_HOME, keydown=True))
textinput.update(PygameUtils.key(pygameMenu.controls.KEY_APPLY, keydown=True))
textinput.update(PygameUtils.key(pygame.K_LSHIFT, keydown=True))
textinput.clear()

# Type
Expand All @@ -151,6 +151,7 @@ def test_textinput(self):
textinput._select_all()
self.assertEqual(textinput._get_selected_text(), 'test')
textinput.update(PygameUtils.key(pygame.K_t, keydown=True, char='t'))
textinput.update(PygameUtils.key(pygame.K_ESCAPE, keydown=True))

# Now the value must be t
self.assertEqual(textinput._get_selected_text(), '')
Expand Down

0 comments on commit 8eb4c8a

Please sign in to comment.