1
votes

I've been trying to develop a "text box" class for pygame as a little personal project, and I've come across an issue that's really stumped me. I'm trying to expand on the pygame text input class found here, wrapping it in a text box class that supports multiple lines and, hopefully, scrolling capabilities.

My problem comes when trying to move the blinker up and down between the lines of text. Basically, hitting the "up" arrow once moves the blinker all the way to the top, and then it stops being responsive to move it down.

Here's the code for how I give the pygame_textbox class the events:

while True:

    events = pygame.event.get()

    for event in events:


        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    test_box.update(events)
    test_box_2.update(events)

    test_box.draw(screen, (100, 100))
    test_box_2.draw(screen, (200, 250))

    pygame.display.update()

Here's the code for the text box class (event comes from the above code):

         if event.type == pl.KEYDOWN:
                if self.is_active:
                    print("reading in text box: {}".format(event))
                    if event.key == pl.K_UP and self.cursor_line > 0:
                        self.cursor_line -= 1
                        print("cursor going UP to {}".format(self.cursor_line))
                    if event.key == pl.K_DOWN and self.cursor_line < len(self.text_input)-1:
                        self.cursor_line += 1
                        print("cursor going DOWN to {}".format(self.cursor_line))
                    if event.key == pl.K_RETURN:
                        self.text_input.insert(self.cursor_line+1, pygame_textinput.TextInput())
                        self.cursor_line += 1
                        for line in self.text_input:
                            print(line.input_string)

Trying to debug it seems to show that the pygame.event.get() queue is taking in way more KEYDOWN events than it's supposed to; one press of the button sends multiple (and sometimes ongoing) events. I'm new to pygame, but I'm pretty sure that's not supposed to happen with KEYDOWN events, right? There's only supposed to be one event triggered every time a key is pressed. What am I doing wrong here? Is this a bug with pygame itself?

Thanks for any help you can provide. I'm fairly new at this and I hope I formatted the question right.

1
How is the textbox class looping through the events? Could it be processing the same event multiple times? - Kingsley

1 Answers

0
votes

Just take a look at the pygame_textinput module you linked in your question:

    # Update key counters:
    for key in self.keyrepeat_counters:
        self.keyrepeat_counters[key][0] += self.clock.get_time()  # Update clock

        # Generate new key events if enough time has passed:
        if self.keyrepeat_counters[key][0] >= self.keyrepeat_intial_interval_ms:
            self.keyrepeat_counters[key][0] = (
                self.keyrepeat_intial_interval_ms
                - self.keyrepeat_interval_ms
            )

            event_key, event_unicode = key, self.keyrepeat_counters[key][1]
            pygame.event.post(pygame.event.Event(pl.KEYDOWN, key=event_key, unicode=event_unicode))

As you can see, it's the update method of the TextInput that repeats the key events by posting them again to pygame's event queue.