Python/Psychopy:记录鼠标点击位置
Python/Psychopy: Logging mouse click locations
使用下面的代码,我试图打开一个 window 10 秒,让用户在屏幕上的任意位置点击任意次数,我想记录鼠标点击位置.每次用户在 window(即 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() 不正确吗?
干杯,
乔恩
发生这种情况是因为脚本会在帧循环中的每次迭代中检查鼠标状态;即每秒 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
使用下面的代码,我试图打开一个 window 10 秒,让用户在屏幕上的任意位置点击任意次数,我想记录鼠标点击位置.每次用户在 window(即 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() 不正确吗?
干杯, 乔恩
发生这种情况是因为脚本会在帧循环中的每次迭代中检查鼠标状态;即每秒 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