feat(text_input): add event handler function (mouse and keyboard input)

This commit is contained in:
Kevin Baumgartner
2026-05-07 21:19:42 +02:00
parent b625acdd60
commit 387a328b1c
+20 -3
View File
@@ -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