2019-08-09 21:53:05

Hi all, i can't remember if this question has been posted recently, but when i am using pygame for window and keyboard handling, how would i make the user input some text? i don't think the input function is going to work...

best regards
never give up on what ever you are doing.

2019-08-09 22:26:47

You need to handle keydown events. You know, pygame handles any user interaction by raising events. Those events need to be handled within the main game loop. Whenever a key is used when in the game window (maybe even outside), an event is created. You need to handle those events and take appropriate action.
Best Regards.
Hijacker

Thumbs up

2019-08-09 23:00:58

There's a list of key codes [here], example:

import pygame
from pygame import mixer
import sys

def Example():
#initialize pygame
    pygame.init()
#initialize sound mixer
    mixer.init()
#create display
    window = pygame.display.set_mode([640,480])
#load sound
    sound = mixer.Sound('tone5.wav')

#main update loop
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
            #if space is pressed, play sound
                if event.key == pygame.K_SPACE:
                    sound.play()
            #if escape is pressed, quit
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit(0)

    #update window
        pygame.display.update()

Example()

If you want to collect and compare text strings, then add a capture for event.unicode like so:

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        text += event.unicode
        print(text)
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

Thumbs up

2019-08-10 07:31:55

Hi i actually ment input like entering text. but i think @3 gave me an idea on what to use. thanks

best regards
never give up on what ever you are doing.

2019-08-10 15:11:05

I ment entering text as well, and @3 only showed you an example for what I described earlier, thanks for that smile.
Best Regards.
Hijacker

Thumbs up