Skip to content

Instantly share code, notes, and snippets.

@ohsqueezy
Created January 1, 2013 16:46
Show Gist options
  • Save ohsqueezy/4428513 to your computer and use it in GitHub Desktop.
Save ohsqueezy/4428513 to your computer and use it in GitHub Desktop.
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 pygame-display-input.py
import pygame
from pygame.locals import *
def name():
pygame.init()
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:
return
screen.fill ((0, 0, 0))
block = font.render(name, True, (255, 255, 255))
rect = block.get_rect()
rect.center = screen.get_rect().center
screen.blit(block, rect)
pygame.display.flip()
if __name__ == "__main__":
name()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment