Reputation: 327
I'm displaying some stimuli and then checking for keypresses via a keypress function, but I can't seem to be able to access the variables in that function e.g. Quit is supposed to be initiated if the user presses Q during key checks, and if the user presses 'g', running turns to '2' which is supposed to exit the overall while loop. I have tried using global variables, but I still could not get it to work, I'm also aware that global variables are considred risky.
def check_keys():
allKeys = event.getKeys(keyList = ('g','h','escape'))
for thisKey in allKeys:
if thisKey == 'escape':
dataFile.close()
window.close()
core.quit()
elif thisKey == 'g':
keyTime=core.getTime()
thisResp = 1
elif thisKey == 'h':
keyTime=core.getTime()
thisResp = 0
thisResp = 2
running = 1
while running == 1:
for frame in range(frames):
fix.draw()
upper_target.draw()
z= window.flip()
check_keys()
if thisResp == 1:
running = 2:
print running
Any help is appreciated.
Upvotes: 1
Views: 77
Reputation: 2152
Since thisResp
is not defined before the check_keys()
method, the method is not going to change the value of thisRep
. In order to change the value of thisResp
, I would either pass it as an argument to the check_keys()
, or have check_keys()
return either 1 or 0 and then set the value of thisResp
to what's return. Your code would look like the following using the second approach:
def check_keys():
allKeys = event.getKeys(keyList = ('g','h','escape'))
for thisKey in allKeys:
if thisKey == 'escape':
dataFile.close()
window.close()
core.quit()
elif thisKey == 'g':
keyTime=core.getTime()
return 1
elif thisKey == 'h':
keyTime=core.getTime()
return 0
return 2
thisResp = 2
running = 1
while running == 1:
for frame in range(frames):
fix.draw()
upper_target.draw()
z= window.flip()
thisResp = check_keys()
if thisResp == 1:
running = 2
break
print running
Upvotes: 2