import pygame
from pygame.locals import *
import config
LEFT = 0
RIGHT = 1
UP = 2
DOWN = 3
OK = 4
CANCEL = 5
OPTION = 6
keymap = {K_UP: UP, K_DOWN: DOWN, K_LEFT: LEFT, K_RIGHT: RIGHT, K_RETURN: OK, K_ESCAPE: CANCEL}
jsamap = {(0,-1.0): OK, (1,-1.0): DOWN, (1,1.0): UP }
jsbmap = {3: OK, 6: CANCEL, 2: OK, 7: CANCEL, 1: OK, 4: CANCEL, 10: OPTION, 9: OK}
def parseevent(e):
try:
if e.type == KEYDOWN:
return keymap[e.key]
elif e.type == JOYBUTTONDOWN:
return jsbmap[e.button]
elif e.type == JOYAXISMOTION:
if e.value != 0.0:
return jsamap[(e.axis,e.value)]
else:
return None
except KeyError:
return None
last_action = None
last_action_time = 0
def getaction():
global last_action, last_action_time
e = pygame.event.poll()
if e.type != NOEVENT:
last_action = parseevent(e)
if last_action:
last_action_time = pygame.time.get_ticks()
return last_action
else:
if pygame.time.get_ticks() - last_action_time > config.keytimeout:
last_action_time = pygame.time.get_ticks()
if last_action in (LEFT,RIGHT,UP,DOWN):
return last_action
return None