鼠标滚轮移动光标

时间:2016-09-30 13:01:14

标签: python-2.7 scroll mousewheel psychopy

我对使用python进行编码相当新,但我必须将其作为博士学位的一部分来学习。我想编写一个任务,其中在一条直线(从上到下)有4个可能的位置(这里是圆圈),它们是目标区域。稍后将呈现对应于这些目标之一的刺激。之后,受试者必须在线上移动鼠标光标以选择正确的刺激。我想通过电脑鼠标的滚轮进行输入。因此,如果我向上滚动按钮,按钮沿着线移动,可以放在其中一个区域,然后给予奖励。

我的问题:如何实现鼠标滚轮的输入,将其绑定到光标并限制光标停留在线上(禁用所有其他鼠标移动)?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

在PsychoPy编码器中 - >演示 - >输入 - >鼠标,您将看到有关如何对不同类型的鼠标输入做出反应的演示脚本。另请参阅documentation。在代码中,您可以执行以下操作:points是您线路上的坐标列表。

# Your points
points = [[0, 0], [1, 0.5], [2, 1], [3, 1.5]]

# Set up mouse
from psychopy import visual, event
win = visual.Window()  # A mouse must be in a window
mouse = event.Mouse()  # initialize mouse object

# Begin listening
event.clearEvents('mouse')  # Do this in the beginning of every trial, to discard "old" mouse events.
index = 1  # start value
while not any(mouse.getPressed()):  # for this example, keep running until any mouse button is pressed
    #print mouse.getWheelRel()
    scroll_change = int(mouse.getWheelRel()[1])  # returns (x,y) but y is what we understand as scroll.
    if scroll_change != 0:
        index += scroll_change  # increment / decrement index
        index = min(len(points)-1, max(index, 0))  # Round down/up to 0 or number of points
        print points[index]  # print it to show that it works. You would probably do something else here, once you have the coordinates.