【问题标题】:Mouse wheel moves cursor鼠标滚轮移动光标
【发布时间】:2016-09-30 13:01:13
【问题描述】:

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

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

提前谢谢你。

【问题讨论】:

    标签: python-2.7 scroll mousewheel psychopy


    【解决方案1】:

    在 PsychoPy Coder --> Demos --> input --> mouse 中,您将看到一个演示脚本,说明如何对不同类型的鼠标输入做出反应。另请参阅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.
    

    【讨论】:

    • 您好乔纳斯,感谢您的回答。它帮助我理解了 getWheelRel() 函数的工作机制。但是,我必须意识到我使用的是“pygame”类型的窗口,它显然不支持 getWheelRel() 函数,因为它一直返回 [0,0]。我最终通过使用 mouse.getPressed 按下鼠标左键和右键来上下移动光标。
    猜你喜欢
    • 2011-09-29
    • 2011-08-28
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    相关资源
    最近更新 更多