Repository: MyreMylar/pygame_gui
Pull request #318 (2022-08-14 20:21:42)

Add HOME & END key support to text entry line
As title.


Patch:
diff --git a/pygame_gui/elements/ui_text_entry_line.py b/pygame_gui/elements/ui_text_entry_line.py
index a3e0c65..0cc8d9a 100644
--- a/pygame_gui/elements/ui_text_entry_line.py
+++ b/pygame_gui/elements/ui_text_entry_line.py
@@ -587,6 +587,18 @@ class UITextEntryLine(UIElement):
                 self.edit_position += 1
                 self.cursor_has_moved_recently = True
             consumed_event = True
+        elif event.key == pygame.K_HOME:
+            if abs(self.select_range[0] - self.select_range[1]) > 0:
+                self.select_range = [0, 0]
+            self.edit_position = 0
+            self.cursor_has_moved_recently = True
+            consumed_event = True
+        elif event.key == pygame.K_END:
+            if abs(self.select_range[0] - self.select_range[1]) > 0:
+                self.select_range = [0, 0]
+            self.edit_position = len(self.text)
+            self.cursor_has_moved_recently = True
+            consumed_event = True
         return consumed_event
 
     def _process_keyboard_shortcut_event(self, event: pygame.event.Event) -> bool:


Test patch:
diff --git a/tests/test_elements/test_ui_text_entry_line.py b/tests/test_elements/test_ui_text_entry_line.py
index 0338dd5..8a0d6f5 100644
--- a/tests/test_elements/test_ui_text_entry_line.py
+++ b/tests/test_elements/test_ui_text_entry_line.py
@@ -678,11 +678,45 @@ class TestUITextEntryLine:
 
         text_entry.set_text('dan')
         text_entry.focus()
+        assert text_entry.edit_position == 3
 
         processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
                                                                           {'key': pygame.K_LEFT}))
 
         assert processed_key_event
+        assert text_entry.edit_position == 2
+
+    def test_process_event_home(self, _init_pygame: None, default_ui_manager: UIManager,
+                                _display_surface_return_none: None):
+        text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
+                                     manager=default_ui_manager)
+
+        text_entry.set_text('dan')
+        text_entry.focus()
+        text_entry.select_range = [0, 2]
+
+        processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
+                                                                          {'key': pygame.K_HOME}))
+
+        assert processed_key_event
+        assert text_entry.select_range == [0, 0]
+        assert text_entry.edit_position == 0
+
+    def test_process_event_end(self, _init_pygame: None, default_ui_manager: UIManager,
+                               _display_surface_return_none: None):
+        text_entry = UITextEntryLine(relative_rect=pygame.Rect(100, 100, 200, 30),
+                                     manager=default_ui_manager)
+
+        text_entry.set_text('dan')
+        text_entry.focus()
+        text_entry.select_range = [0, 2]
+
+        processed_key_event = text_entry.process_event(pygame.event.Event(pygame.KEYDOWN,
+                                                                          {'key': pygame.K_END}))
+
+        assert processed_key_event
+        assert text_entry.select_range == [0, 0]
+        assert text_entry.edit_position == 3
 
     def test_process_event_text_right_select_range(self, _init_pygame: None,
                                                    default_ui_manager: UIManager,