2016-09-30 174 views
0

我對python編碼相當陌生,但我必須將它作爲我的博士學習的一部分來學習。我想要編寫一個任務,在其中一條直線(從上到下)有4個可能的位置(這裏是圓圈),它們是目標區域。隨後將呈現與這些目標中的一個相對應的刺激。之後,對象將不得不移動鼠標光標以選擇正確的刺激。我想通過電腦鼠標的輪子做輸入。所以,如果我向上滾動按鈕上的光標,可以將其放在其中一個區域上,然後給予獎勵。鼠標滾輪移動光標

我的問題:如何實現鼠標滾輪的輸入,將其綁定到光標並限制光標停留在行上(禁用所有其他鼠標移動)?

預先感謝您。

回答

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. 
+0

你好,喬納斯,謝謝你的回答。它幫助我理解getWheelRel()函數的工作機制。然而,我不得不意識到我使用的是「pygame」類型的窗口,它顯然不支持getWheelRel()函數,因爲它繼續返回[0,0]。我通過用鼠標左鍵和右鍵鼠標按鈕來上下移動光標。 – fishermen1275