2008-12-05 08:18:29 +00:00
|
|
|
#!/usr/bin/env python
|
2008-12-19 02:52:15 +00:00
|
|
|
# get_key.py - an sdl keygrabber for gfceux
|
|
|
|
# Lukas Sabota
|
|
|
|
# December 2008
|
|
|
|
#
|
|
|
|
# Licensed under the GPL
|
|
|
|
#
|
|
|
|
|
2008-06-28 19:13:37 +00:00
|
|
|
import sys
|
2008-12-05 08:18:29 +00:00
|
|
|
try:
|
|
|
|
import pygame
|
|
|
|
from pygame.locals import *
|
|
|
|
has_pygame = True
|
|
|
|
except ImportError:
|
|
|
|
self.has_pygame = False
|
2008-06-28 19:13:37 +00:00
|
|
|
|
2008-06-28 19:19:06 +00:00
|
|
|
class KeyGrabber:
|
2008-12-05 08:18:29 +00:00
|
|
|
""" KeyGrabber is a wrapper that gets an SDL key from the user using pygame """
|
|
|
|
def __init__(self, width=300, height=100):
|
|
|
|
pygame.init()
|
2008-12-05 22:50:43 +00:00
|
|
|
pygame.joystick.init()
|
|
|
|
if pygame.joystick.get_count() > 0:
|
2008-12-07 15:28:20 +00:00
|
|
|
for x in range(0, pygame.joystick.get_count()):
|
|
|
|
joy = pygame.joystick.Joystick(x)
|
|
|
|
joy.init()
|
2008-12-19 02:52:15 +00:00
|
|
|
print "joystick " + str(x) + " initialized."
|
2008-12-05 08:18:29 +00:00
|
|
|
screen = pygame.display.set_mode((width, height))
|
|
|
|
pygame.display.set_caption("Press any key. . .")
|
2008-12-05 22:50:43 +00:00
|
|
|
|
2008-06-28 19:19:06 +00:00
|
|
|
|
2008-12-05 08:18:29 +00:00
|
|
|
def get_key(self):
|
2008-12-19 02:52:15 +00:00
|
|
|
""" returns a tuple with information about the key pressed
|
|
|
|
(device_string, key_number, joy_number) """
|
2008-12-05 08:18:29 +00:00
|
|
|
while 1:
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == KEYDOWN:
|
|
|
|
pygame.display.quit()
|
2008-12-19 02:52:15 +00:00
|
|
|
return ("Keyboard", event.key)
|
2008-12-05 17:41:27 +00:00
|
|
|
if event.type == JOYBUTTONDOWN:
|
|
|
|
pygame.display.quit()
|
2008-12-07 15:28:20 +00:00
|
|
|
return ("Joystick", event.button, event.joy)
|
2008-12-19 02:52:15 +00:00
|
|
|
# TODO: figure out how fceux saves axes and hat movements and implement
|
|
|
|
# this might even be easier if we just make the C++ fceux code more modular
|
|
|
|
# and use a C++ snippet based off code in drivers/sdl/input.cpp and drivers/sdl/sdl-joystick.cpp
|
2008-12-07 15:28:20 +00:00
|
|
|
#if event.type == JOYAXISMOTION:
|
|
|
|
# pygame.display.quit()
|
|
|
|
# return event.joy, event.axis, event.value
|
2008-06-28 19:19:06 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2008-12-05 08:18:29 +00:00
|
|
|
kg = KeyGrabber()
|
|
|
|
print kg.get_key()
|
2008-06-28 19:13:37 +00:00
|
|
|
|