Package: pygame-textinput
Pygame Text Input ModuleThis module provides two utility classes that simplify entering text using pygame. The classes are:TextInputVisualizerwhich can be used to both manage and draw text input. Simply pass all events returned bypygame.event.get()to it every frame, and blit itssurfaceattribute on the screen.TextInputManagerthat can be used to just manage inputted text, with no visual aspect. Used byTextInputVisualizerbehind the scenes.InstallationSimplest way is using pypi:python3 -m pip install pygame-textinputUsageVisualizerThe easiest way is toTextInputVisualizerwithout any arguments. Then, feed allpygameevents to itsupdatemethod every frame, and blit it'ssurfaceproperty to the screen. Here's a minimal example:importpygame_textinputimportpygamepygame.init()# Create TextInput-objecttextinput=pygame_textinput.TextInputVisualizer()screen=pygame.display.set_mode((1000,200))clock=pygame.time.Clock()whileTrue:screen.fill((225,225,225))events=pygame.event.get()foreventinevents:ifevent.type==pygame.QUIT:exit()# Feed it with events every frametextinput.update(events)# Blit its surface onto the screenscreen.blit(textinput.surface,(10,10))pygame.display.update()clock.tick(30)Notes on the newer version:You have to watch for "return" presses by the user yourself, e.g. like this:if[evforevineventsifev.type==pygame.KEYDOWNandev.key==pygame.K_RETURN]:print("Oooweee")Contrary to the old version, key-stroke repeats are not manually introduced anymore, since they can now be enabled withinpygamedirectly:pygame.key.set_repeat(200,25)# press every 50 ms after waiting 200 msThis new version has also been optimized such that you canmodify any fields on the flyand the actual surface will only re-render if you access it usingtextinput.surface- and only if you actually modified any values.Arguments / Fields:All these values can be both specified as arguments to the constructor and modified at later time by setting them as attributes (e.g.textinput.font_color = (255, 0, 0)). The surface itself will only re-render once it is accessed viatextinput.surface.ArgumentDescriptionmanagerTheTextInputManagerused to manage the inputfont_objectThepygame.font.Fontobject used for renderingantialiaswhether to render the font antialiased or notfont_colorcolor of font renderedcursor_blink_intervalThe interval of the cursor blinking, in mscursor_widthThe width of the cursor, in pixelscursor_colorThe color of the cursorManagerIf you prefer to draw the text on the screen yourself, you can useTextInputManagerto only manage the string that has been typed so far.LikeTextInputVisualizer, you feed itsupdatemethod all events received bypygame.event.get()which you want it to process.TextInputVisualizerdoes this for you inside itsupdatemethod if you pass it aTextInputManager.Arguments:ArgumentDescriptioninitialThe initial value (text)validatorA function taking astringand returning abool. Every time the input value is modified, this function is called; if the function returnsTrue, the input is accepted, otherwise it is ignored.So say you want to only allow input to up to 5 letters, you could do that withmanager=TextInputManager(validator=lambdainput:len(input)<=5)FieldsFieldDescriptionvalueThe inserted value so far. When change,cursor_posis kept as far as possible.cursor_posThe position of the cursor.0is before the first character,len(manager.value)the position after the last. Values outside this range are clamped.ExampleHere's an example that shows most features:importpygameimportpygame.localsasplpygame.init()# No arguments needed to get startedtextinput=TextInputVisualizer()# But more customization possible: Pass your own font objectfont=pygame.font.SysFont("Consolas",55)# Create own manager with custom input validatormanager=TextInputManager(validator=lambdainput:len(input)<=5)# Pass these to constructortextinput_custom=TextInputVisualizer(manager=manager,font_object=font)# Other customizations:textinput_custom.cursor_width=4textinput_custom.cursor_blink_interval=400# blinking interval in mstextinput_custom.antialias=Falsetextinput_custom.font_color=(0,85,170)screen=pygame.display.set_mode((1000,200))clock=pygame.time.Clock()# Pygame now allows natively to enable key repeat:pygame.key.set_repeat(200,25)whileTrue:screen.fill((225,225,225))events=pygame.event.get()foreventinevents:ifevent.type==pygame.QUIT:exit()# Feed it with events every frametextinput.update(events)textinput_custom.update(events)# Get its surface to blit onto the screenscreen.blit(textinput.surface,(10,10))screen.blit(textinput_custom.surface,(10,50))# Modify attributes on the fly - the surface is only rerendered when .surface is accessed & if values changedtextinput_custom.font_color=[(c+10)%255forcintextinput_custom.font_color]# Check if user pressed returnif[evforevineventsifev.type==pygame.KEYDOWNandev.key==pygame.K_RETURN]:print(f"User pressed enter! Input so far:{textinput.value}")pygame.display.update()clock.tick(30)