[clug] Building a simple UI with pygame, keystrokes and a window focus problem
Hal Ashburner
hal at ashburner.info
Tue Feb 1 03:37:50 MST 2011
Hey Clug,
I'm knocking up a quick and dirty dedicated do-hickey booth thing for a
friend's art project. This is going to run on a dedicated laptop.
I'm using pygame and an i-pac keyboard controller
http://www.ultimarc.com/ipac1.html
#I want sound effects when switches hit. Ok easy
sound1 = pygame.mixer.Sound("foo.ogg")
#later
sound1.play()
#switches hit, going via the ipac thingy should be handled as
keystrokes, yeah?
for event in pygame.events.get():
if event.type == KEYDOWN:
#a switch was hit!
do_command(event.key)
#somewhere in the initialisation I'm doing:
window = pygame.display.set_mode((width, height)), pygame.FULLSCREEN)
#which makes sure pygame gets the keystrokes.
#All good, right up until one of the things I want to do is launch
cheese as a photobooth kind of deal.
if not cheese:
cheese = subprocess.Popen("/usr/bin/cheese")
else:
cheese.terminate()
cheese = None
#which, as you probably saw a lot faster than me because I actually
coded & tested it before realising, takes focus away from the pygame
app, thereby swallowing all keystrokes.
So I just want to create a very very simple and stupid UI that responds
to keystrokes, on which it makes a noise or launches/kills a process. I
want all keystrokes to be handled by this UI and not captured by the any
launched process.
Am I doing it all wrong using pygame? Suggestions with or without pygame?
Cheers,
Hal
More information about the linux
mailing list