使用下面的代码,我试图打开一个窗口10秒钟,让用户按照他们的意愿多次点击屏幕上的任何地方,我想记录鼠标点击位置。每次用户在窗口中单击(即myMouse.getPressed)时,我都会将单击位置附加到列表(mouse_click_locations [])。但是,列表在每个帧上多次附加相同的单击位置。我想将点击位置附加到列表一次,并且在启动另一次点击之前不再添加。我认为在每个帧的末尾添加'myMouse.clickReset()'可以做到这一点,但它没有什么区别。
10秒后,我希望每次点击鼠标时,列表都会填充一个位置(x,y坐标)。
from psychopy import visual, core, gui, event, sound
win = visual.Window([800,600],color=(1,1,1), colorSpace='rgb',
rgb=None, allowGUI=True, monitor='testMonitor',
units='deg', fullscr=False)
myMouse = event.Mouse(visible=True,win=win)
refresh_rate = 60.0
default_time = 10
time_window = default_time * refresh_rate
time_window = int(time_window)
running = 1
while running:
mouse_click_locations = []
for frame in range(time_window):
mouse_loc = myMouse.getPos()
mouse_click = myMouse.getPressed()
if mouse_click:
mouse_click_locations.append(mouse_loc)
win.flip()
myMouse.clickReset()
running = 0
win.close()
print mouse_click_locations
有人可以帮助我实现这个目标吗?我错误地使用了myMouse.clickReset()吗?
干杯, 乔恩
答案 0 :(得分:2)
这是因为脚本会检查帧循环中每次迭代的鼠标状态;即每秒60次。正如您所说,您只想为每个鼠标按下一个事件。这是一个解决方案,您只需暂停脚本,直到释放所有按钮。另请注意使用any
明确检查所有按钮。
# Record mouse position if a key is pressed
if any(myMouse.getPressed()) # Any button pressed, no matter which
mouse_click_locations.append(myMouse.getPos())
# Wait until all buttons are released
while any(myMouse.getPressed()):
pass
无需使用mouse.clickReset
。作为一个小注释,您不会更新屏幕上的可视内容,因此您不需要在循环中包含win.flip
。因为它等待下一次监视器更新,它有效地将响应时间(如果需要这些)舍入到最接近的1/60秒间隔。这一点,并依赖于默认值,也将大大简化脚本:
default_time = 10
from psychopy import visual, core, event
win = visual.Window(color='white', monitor='testMonitor', units='deg')
myMouse = event.Mouse(visible=True, win=win)
clock = core.ClocK()
# Collect unique click events before time runs out
mouse_click_locations = []
while clock.getTime() < default_time:
# Record mouse position if a key is pressed
if any(myMouse.getPressed()) # Any button pressed, no matter which
mouse_click_locations.append(myMouse.getPos())
# Wait until all buttons are released
while any(myMouse.getPressed()):
pass
print mouse_click_locations