For an eye tracking experiment i want to implement a way to close the window after the user presses the escape or q button. Eventually I want the keys to also stop the Gaze recording and save the eye-tracking data to a .cvs file. As of now the window closes after a few seconds without any input.
Simple function calls like e.g.
if keys[0] in ["p"]:
print("hello")
are also not working
I’m new to PsychoPy so any help would be appreciated!
This is my code implementation as of now
import pickle
import pandas as pd
from psychopy import core, event, misc, visual, monitors, data, gui
from titta import Titta, helpers_tobii as helpers
MY_MONITOR = 'testMonitor' # needs to exists in PsychoPy monitor center
FULLSCREEN = True
SCREEN_RES = [1920, 1080]
SCREEN_WIDTH = 52.7 # cm
VIEWING_DIST = 70 # distance from eye to center of screen (cm)
monitor_refresh_rate = 60 # frames per second (fps)
mon = monitors.Monitor(MY_MONITOR) # Defined in defaults file
mon.setWidth(SCREEN_WIDTH) # Width of screen (cm)
mon.setDistance(VIEWING_DIST) # Distance eye / monitor (cm)
mon.setSizePix(SCREEN_RES)
win = visual.Window(monitor=mon, fullscr=FULLSCREEN,
screen=1, size=SCREEN_RES, units='deg')
for keys in event.getKeys():
if keys[0] in ["escape", "q"]:
print(keys)
win.close()
it is only checking the first char of the key name. Try:
for key in event.getKeys():
if key in ["escape", "q"]:
print(key)
win.close()
Also, in your example getKeys() will most likely return no keys since it is not in a loop so it only checks for keys once right after you create the window. Did you mean to use event.waitKeys() instead of getKeys?
Thank you very much!
So if I’d use event.waitKeys() it would check for keys until the key is pressed and not just once? I will change this tomorrow and will come back to you!
It will wait for a key to be pressed and then return. If you want to wait for another key you would need to put event.waitKeys() in a loop or call event.waitKeys() again later in your script
thanks, now i have the problem that the window closes regardless of which key i press although i limited it to escape only. it also closes when i remove the win.close() function.
import pickle
import pandas as pd
from psychopy import core, event, misc, visual, monitors, data, gui
from titta import Titta, helpers_tobii as helpers
MY_MONITOR = 'testMonitor' # needs to exists in PsychoPy monitor center
FULLSCREEN = False
SCREEN_RES = [852, 480]
SCREEN_WIDTH = 52.7 # cm
VIEWING_DIST = 70 # distance from eye to center of screen (cm)
running = True
monitor_refresh_rate = 60 # frames per second (fps)
mon = monitors.Monitor(MY_MONITOR) # Defined in defaults file
mon.setWidth(SCREEN_WIDTH) # Width of screen (cm)
mon.setDistance(VIEWING_DIST) # Distance eye / monitor (cm)
mon.setSizePix(SCREEN_RES)
win = visual.Window(monitor=mon, fullscr=FULLSCREEN,screen=1, size=SCREEN_RES, units='deg')
for key in event.waitKeys():
if key in ["escape"]:
win.close()
If the above is your full script, then as soon as you press a key it will end the experiment after getting the key and checking for ‘escape’. Ending the experiment will also close the expriment window. i.e:
for key in event.waitKeys():
if key in ["escape"]:
print("closing because of escape press")
win.close()
else:
print("Key pressed: ", key)
print("End of experiment - window will close")