From 387a328b1ce59e1df645caaee4ce15fb8ae80634 Mon Sep 17 00:00:00 2001 From: Kevin Baumgartner Date: Thu, 7 May 2026 21:19:42 +0200 Subject: [PATCH] feat(text_input): add event handler function (mouse and keyboard input) --- src/text_input.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/text_input.py b/src/text_input.py index 6272f3b..64acc25 100644 --- a/src/text_input.py +++ b/src/text_input.py @@ -9,7 +9,7 @@ class TextInput: # input text self.text = "" - self.active = True + self.active = False # colors self.text_color = (255, 255, 255) @@ -18,8 +18,25 @@ class TextInput: self.background_color = (30, 30, 30) def handle_event(self, event): - # todo check keyboard input events - pass + # check if mouse click event on text input box + if event.type == pygame.MOUSEBUTTONDOWN: + if self.rect.collidepoint(event.pos): + self.active = True + else: + self.active = False + + # check for keyboard input + if event.type == pygame.KEYDOWN and self.active: + # enter will submit and exit input box + if event.key == pygame.K_RETURN: + self.active = False + + # backspace will delete last character + elif event.key == pygame.K_BACKSPACE: + self.text = self.text[:-1] + # other characters will be added to the string + else: + self.text += event.unicode def draw(self, screen): # draw background of input box