Need help creating an experiment with mouse input

Hi everyone,

I’m new to Psychopy and I’m working on creating an online experiment where subjects will see lines of different lengths and then be asked to draw the line they just saw using a mouse. Specifically, I want them to be able to increase the length of the line by 4 pixels with each scroll up and decrease it by 4 pixels with each scroll down.

So far, I’ve managed to create the experiment, and it seems to be working fine. However, after I press ‘n’ to proceed, I’m not seeing the probe where mouse input is supposed to be shown. I’m not sure what I’m missing.

Here’s the code I’ve written:

from psychopy import visual, core, event

Create a list of line lengths

line_lengths = [50, 100, 150, 200, 250, 300, 350, 400, 450, 500]

Create a window

win = visual.Window([1366, 768], monitor=“testMonitor”, units=“pix”)

Create a text stimulus to instruct participants

instruction_text = visual.TextStim(win, text=‘Press “n” key to start’, color=‘white’)
instruction_text.draw()
win.flip()

Wait for the ‘n’ key press to start the experiment

keys = event.waitKeys(keyList=[‘n’])

Function to handle keyboard responses

def handle_keyboard_response():
global length
keys = event.getKeys([‘m’, ‘b’])
if ‘m’ in keys: # Increase line length by 4 pixels when ‘m’ is pressed
length += 4
elif ‘b’ in keys: # Decrease line length by 4 pixels when ‘b’ is pressed
length -= 4

Loop through each line length

for length in line_lengths:
# Clear the window
win.flip()

# Create a line stimulus with the specified length
line = visual.Line(win, start=(-length/2, 0), end=(length/2, 0), lineColor='white')

# Loop to update line length based on keyboard responses
while True:
    line.draw()
    handle_keyboard_response()  # Check for keyboard responses
    win.flip()
    if 'n' in event.getKeys():  # Press 'n' key to confirm line length
        break

# Update the window to display the line
win.flip()

# Wait for a specified duration (e.g., 2 seconds) before presenting the next line
core.wait(2)

Close the window and end the experiment

win.close()
core.quit()

Any help or suggestions would be greatly appreciated!

Thanks,

Has this been written in Coder? You’ll need to use Builder to put it online.

I can’t see any reference to mouse input.

1 Like

Hi, thank you very much!! I am trying to create an online experiment where participants will see 12 unique line lengths and there will be a probe trial where they will reproduce lines by clicking, holding and dragging the mouse. I already have a code for the mouse input but it doesn’t work for online tasks I guess (
my_mouse = Mouse()
my_canvas = Canvas()
my_mouse.show_cursor(True)
my_mouse.set_pos(pos=(0.0,0.0))
my_canvas.show()

while True:
var.init_pos = my_mouse.get_pos()
button, start_pos, timestamp = my_mouse.get_click(timeout=20, visible=True)
var.start_resp = timestamp
if button is not None:
var.sx, var.sy = start_pos
ex, ey = start_pos

    while any(my_mouse.get_pressed()):
        (var.ex, var.ey), timestamp = my_mouse.get_pos()
        var.end_resp = timestamp
        my_canvas.clear()
        my_canvas.line(var.sx, var.sy, var.ex, var.ey)
        my_canvas.show()
    break)

I know that I am not supposed to use Python libraries for online studies but how else I can adjust that?

Thank you very much!