Skip to content
Create a gist now

Instantly share code, notes, and snippets.

display keyboard input using pygame
# Display keyboard input using pygame. Only prints letters (no numbers
# or special chars). Backspace deletes one character. Return clears
# the entire input.
# Run with the following command:
# python
import pygame
from pygame.locals import *
def name():
screen = pygame.display.set_mode((480, 360))
name = ""
font = pygame.font.Font(None, 50)
while True:
for evt in pygame.event.get():
if evt.type == KEYDOWN:
if evt.unicode.isalpha():
name += evt.unicode
elif evt.key == K_BACKSPACE:
name = name[:-1]
elif evt.key == K_RETURN:
name = ""
elif evt.type == QUIT:
screen.fill ((0, 0, 0))
block = font.render(name, True, (255, 255, 255))
rect = block.get_rect() = screen.get_rect().center
screen.blit(block, rect)
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.