Modularized the sdl KeyGrabber
This commit is contained in:
parent
2db6590fbd
commit
508734aee6
28
get_key.py
28
get_key.py
|
@ -3,15 +3,23 @@ import pygame
|
|||
import sys
|
||||
from pygame.locals import *
|
||||
|
||||
pygame.init()
|
||||
width = 300
|
||||
height = 100
|
||||
screen = pygame.display.set_mode((width, height))
|
||||
pygame.display.set_caption("Press any key. . .")
|
||||
|
||||
while 1:
|
||||
for event in pygame.event.get():
|
||||
if event.type == KEYDOWN:
|
||||
print event.key
|
||||
sys.exit(event.key)
|
||||
class KeyGrabber:
|
||||
""" KeyGrabber is a wrapper that gets an SDL key from the user using pygame """
|
||||
def __init__(self, width=300, height=100):
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((width, height))
|
||||
pygame.display.set_caption("Press any key. . .")
|
||||
|
||||
def get_key(self):
|
||||
while 1:
|
||||
for event in pygame.event.get():
|
||||
if event.type == KEYDOWN:
|
||||
#print event.key
|
||||
pygame.display.quit()
|
||||
return event.key
|
||||
|
||||
if __name__ == "__main__":
|
||||
kg = KeyGrabber()
|
||||
print kg.get_key()
|
||||
|
||||
|
|
Loading…
Reference in New Issue