mouse.getPos() when space bar is pressed

I am looking to output the x mouse position when the space bar is pressed.

So far I have used the following if statement in begin routine:

keys = event.getKeys()

if "space" in keys:
    mouse_pos=mouse_2.getPos()[0]

Then used addData at end routine:

thisExp.addData('mouse_pos', mouse_pos)

The out put I get is almost as I want it.
However the very first value comes out as the second value (and so on until the end where the final value is missed off).

Instead of the first value I get whatever I define the mouse_pos to be before the experiment (e.g. None). If I don’t type mouse_pos = None then the experiment will not run with the error:

mouse_pos not defined.

Any ideas on how I could alter the if statement OR alter the addData() so that mouse_pos is a list and I could remove the first value.

I think the problem could be solved this way

keys = event.getKeys()

if "space" in keys:
    mouse_pos=mouse_2.getPos()[0]
    thisExp.addData('mouse_pos', mouse_pos)

tandy

I’ll give that a go. Thanks!

I did try that code but it still presents None as the first value then all of the other values pushed down one relative to where the should be in the rows (this is much better than missing the final value of the end so thank you!)

The solution I used to get the data outputting as a column (still out of place by one row) was using an if else statement to create separate columns in the data file- one column for the None value (mouse_none) and another column for my response values (mouse_pos).

keys = event.getKeys()

if "space" in keys:
    mouse_pos=mouse_2.getPos()[0]

if mouse_pos is None:
        thisExp.addData('mouse_none', mouse_pos)
else:
        thisExp.addData('mouse_pos', mouse_pos)

Then I realised this could be done even more simply by only outputting one column.

So

keys = event.getKeys()

if "space" in keys:
    mouse_pos=mouse_2.getPos()[0]

if mouse_pos is not None:
        thisExp.addData('mouse_pos', mouse_pos)

Therefore it still has the value of None but the ‘None’ value doesn’t present in the outputted data file. I’m going to take this as the solution and the just make changes to the Matlab script I am using to read the data out of the file (so I can account for the numbers being out of sync by one row).

1 Like