[solved] Log time and duration of multiple key presses
Hello,
I am trying to set up a listening experiment in which I ask the participant, while listening to an audio stimulus, to press a key when they feel a certain emotion and to keep it pressed for as long as they feel this emotion.
I would like to log both the time at which the participants starts pressing the key as well as the duration of the keypress. I tried the inline script found in this post: http://forum.cogsci.nl/index.php?p=/discussion/37/solved-how-to-log-the-duration-of-a-key-press/p1, by adding these two lines to get the exact times:
self.experiment.set("key_up_time", key_up_time)
self.experiment.set("key_down_time", key_down_time)
However, this raises two problems: first of all, the "key_down_time" value is very high, possible counting from the beginning of the experiment and not from the onset of the audio stimulus (of which I set the duration at 0 so it would begin to collect keyboard responses right away). Secondly, this allows me to record only one keypress. Once the key is released, the sequence proceeds to the next item, although the sound is still playing in the background.
What I would like to achieve is for the whole stimulus to be played, during which both the onset time and the duration of each key press is logged. E.g. one could feel an emotion beginning after 10 seconds, feel this emotion for 5 seconds. Than 20 seconds of nothing, and then once again the emotion could reappear for another 40 seconds. For each time the participant presses a key, I would like to log the time at which this happens and the duration of the keypress. Is there any way to do this?
Thank you in advance for your help!
Comments
Hi Eveline,
So if I understand correctly, you want to log all key presses and key releases that occur while a sound is playing. Is that right?
To do this, it's best to use either the legacy or xpyriment back-end. Both of these are built on PyGame, so you can use the PyGame event system. The example script below shows the basic logic.
I would also consider what you want your logfiles to look like. Your data is essentially continuous, which is difficult to combine with the spreadsheet-like logfile that OpenSesame normally uses. The script below writes lines to the logfile, which you can parse afterwards. But don't combine this with a
logger
, or your logfiles will become very messy!See also:
Cheers,
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
Thanks a lot! It works perfectly! This was exactly what I was looking for. I have never programmed in Python before, so I was lost trying to write code myself.
The logfile isn't too messy, as I don't expect people to press the key very often while listening (probably not more than 1-2 times per stimulus).
Thanks again for your help.
Cheers,
Eveline