[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] device events?



Michael wrote:
Is there anything like events for devices in PyGame? Something like events to let your program know when the cd/dvd drive has been opened or closed. Is there any other common Pygame method to watch for disc changes? If not, would there be any chance of getting that added sometime in the near future? Thanks.
it would not be hard to do this with a simple polling function. could be done in all python this way.



POLLEVENT = USEREVENT+1
CDEVENT = USEREVENT+2
CDStates = None

def gamemainloop():
pygame.time.set_timer(POLLEVENT, 1000)
while 1:
for event in pygame.event.get():
#handle those other events too
if event.type = POLLEVENT:
polldevices()
elif event.type == CDEVENT:
drivename = event.cd.get_name()
action = ('remove', 'insert')[event.action]
print "DISC ACTION: drive=%s action=%s", % \
(drivename, action)

def polldevices():
global CDStates
pygame.cdrom.init() #just to be sure
if CDStates == None: #first initialize
CDStates = []
for x in range(pygame.cdrom.get_count()):
cd = pygame.crdrom.CD(x)
cd.init()
CDStates.append([cd, not cd.get_empty()])
else:
for state in CDStates:
cd, oldfull = state
full = not cd.get_empty()
if full != oldfull:
state[1] = full
e = pygame.event.Event(CDEVENT, cd=cd, action=full)
pygame.event.post(e)