[visionegg] Re: Terminating a While Loop
- From: John Herrington <john.herrington@xxxxxxxx>
- To: visionegg@xxxxxxxxxxxxx
- Date: Tue, 17 Jul 2007 13:39:44 -0400
Hi Jeff,
Here is some code I use that aborts my experiments with an escape key.
It's a bit convoluted (others may have a better solution) but it gets
the job done.
The code handles keypresses generally and is set as an event in my main
Presentation object via this command:
___________
myPresentation.parameters.handle_event_callbacks =
(pygame.locals.KEYDOWN, keypress)
___________
So the function called keypress is implemented every time there is a
keypress event while the Presentation object is running. Now here's the
function itself:
____________
#Function for keypress event
def keypress(event):
#Get key event. If escape, end experiment
kb=pygame.key.get_pressed()
if kb[27] == 1: stim_update(4)
else:
resps.append([pygame.time.get_ticks()-starttime,trialctr,pygame.mouse.get_pressed()])
_____________
So, according to the above code, if the escape key is pressed (e.g.
element 27 in the get_pressed output is set to 1), a function called
stim_update is called and passed the value 4. If the key wasn't escape,
the keypress is added to a list alongside some other relevant info
(reaction time etc.). I later process that list as a subject response.
But the main thing here is the stim_update function called when the
escape button is pressed. Here is a snippet of the relevant code within
that function:
________________
def stim_update(evt):
...snip...
if evt == 4: #End of exp event
#Exp. is ended by setting main presentation evt duration to 0
myPresentation.set(go_duration=(0,'seconds'))
________________
So, in a nutshell, if the escape key is pressed, a function is called
that sets the Presentation duration to 0, which in effect ends it.
Hope this helps - john
Maher, Jeffrey wrote:
Hello,
Im currently working on the following code trying to determine a way to exit a
while loop before the criteria is satisfied.
i=0
while i<eval(self.number.get()):
p.go
i=i+1
My goal is to set it to a certain key that will either pause the program, and then resume it once pressed again, or have it exit the program all-together, either/or would be acceptable.
I had tried setting it to the "esc" key by doing the following code that I found while searching for an answer:
if msvcrt.kbhit() and msvcrt.getch() == chr(27):
break
But that just causes the program to fail to start up.
I have also tried:
for event in pygame.event.get():
if event.type == pygame.locals.KEYDOWN:
if event.key == pygame.locals.K_ESCAPE:
sys.exit()
But this again just causes the program to fail to start up.
Any suggestions would be greatly appreciated.
-Jeff
====================================
The Vision Egg mailing list
Archives: http://www.freelists.org/archives/visionegg
Website: http://www.visionegg.org/mailinglist.html
======================================
The Vision Egg mailing list
Archives: http://www.freelists.org/archives/visionegg
Website: http://www.visionegg.org/mailinglist.html
- References:
- [visionegg] Terminating a While Loop
- From: Maher, Jeffrey
Other related posts:
- » [visionegg] Terminating a While Loop
- » [visionegg] Re: Terminating a While Loop
- » [visionegg] Re: Terminating a While Loop
- » [visionegg] Re: Terminating a While Loop
- » [visionegg] Re: Terminating a While Loop
Hello, Im currently working on the following code trying to determine a way to exit a while loop before the criteria is satisfied.i=0
while i<eval(self.number.get()):
p.go
i=i+1
My goal is to set it to a certain key that will either pause the program, and then resume it once pressed again, or have it exit the program all-together, either/or would be acceptable.
I had tried setting it to the "esc" key by doing the following code that I found while searching for an answer:
if msvcrt.kbhit() and msvcrt.getch() == chr(27):
break
But that just causes the program to fail to start up.
I have also tried:
for event in pygame.event.get():
if event.type == pygame.locals.KEYDOWN:
if event.key == pygame.locals.K_ESCAPE:
sys.exit()
But this again just causes the program to fail to start up.
Any suggestions would be greatly appreciated.
-Jeff
==================================== The Vision Egg mailing list Archives: http://www.freelists.org/archives/visionegg Website: http://www.visionegg.org/mailinglist.html
- [visionegg] Terminating a While Loop
- From: Maher, Jeffrey