2

I'm working on a project and I found this code for a text box in Python: How to create a text input box with pygame?

Now, I want to change the code so that while the user holds backspace the text box will delete characters.

I tried to change the code to this:

if event.type == pg.KEYDOWN:
    if self.active:
        if event.key == pg.K_RETURN:
            self.text = ''
        if event.key == pg.K_BACKSPACE:
            while pg.KEYDOWN is pg.K_BACKSPACE:
                print "shock"
                self.text = self.text[:-1]
        else:
            self.text += event.unicode
        # Re-render the text.
        self.txt_surface = FONT.render(self.text, True, self.color)

but it does not work and I can't find any solutions online.

skrx
  • 19,980
  • 5
  • 34
  • 48
CodeCop
  • 1
  • 2
  • 15
  • 37

1 Answers1

2

You can enable keyboard repeat, so holding down a key will generate multiple key down events.

So call pg.key.set_repeat(500, 200), after pg.init(), to generate a backspace key down event every 200 milliseconds after an initial delay of half a second.

import random
  • 3,054
  • 1
  • 17
  • 22