Created
January 1, 2013 16:46
-
-
Save ohsqueezy/4428513 to your computer and use it in GitHub Desktop.
display keyboard input using pygame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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