Reputation: 3
I am fairly new to coding with python but I have to learn it as part of my PhD. I want to program a task in which on a straight line (top to bottom) there are 4 possible positions (here circles) that are target zones. Later a stimulus will be presented that corresponds to one of these targets. Afterwards the subject will have to move a mouse cursor on the line to select the correct stimulus. I want to do the input via the wheel of the computer mouse. So if I scroll up the cursor on the button moves along the line and can be placed on one of the zones and then a reward is given.
To my question: How do I implement the input of the mouse wheel, bind it to the cursor and restrict the the cursor to stay on the line (disable all other mouse movements) ?
Thank you in advance.
Upvotes: 0
Views: 507
Reputation: 5683
In PsychoPy Coder --> Demos --> input --> mouse, you will see a demo script on how to react to different kinds of mouse input. Also, see the documentation. In code, you would do something like this, where points
is a list of coordinates on your line.
# 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.
Upvotes: 1