[solved] Log time and duration of simultaneous key presses
Hello,
Sorry to bother you again. My question is a variant of the one I posted earlier (http://www.cogsci.nl/forum/index.php?p=/discussion/1199/solved-log-time-and-duration-of-multiple-key-presses#Item_3).
The script proposed in that discussion (see below) will record only one keypress at a time. However, I would like to be able to record both time and duration of 2 simultaneous key presses. My experiment is about music-induced emotions and when the partecipants indicate they experienced 2 different emotional states, I ask them to re-listen to the piece and press [key1] when they feel emotion [a] and [key2] when they feel emotion [b]. In both cases they have to keep the key pressed for as long as they experience that emotion.
This is the script that logs time and duration of single keypresses:
import pygame
t0 = self.time()
while pygame.mixer.get_busy():
response_time = self.time() - t0
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
exp.log('KEYDOWN key=%s rt=%d' \
% (pygame.key.name(event.key), response_time))
if event.type == pygame.KEYUP:
exp.log('KEYUP key=%s rt=%d' \
% (pygame.key.name(event.key), response_time))
However, if after I press one key (and keep it pressed) I press a second key, the latter is not recorded by the script. Is it possible to record time and duration of simultaneous keypresses?
Thank you so much for your help. I am just learning Python, but don't seem to know my way around it yet.
All the best,
Eveline
Comments
Hi Eveline,
The script, aside from indentation errors due to pasting in the forum, should do exactly what you want it do: record all key presses and releases while a sound is playing. Are you sure that the problem is not something simple, such as running this script only after the sound has finished playing, or misunderstanding the log file?
To figure out the problem, you can add some print statements throughout the script. By printing a message (with a timestamp) when the
inline_script
starts, a message when it is done, and a message for everyevent
that is received, you can gain insight into what happens exactly.Cheers!
Sebastiaan
Check out SigmundAI.eu for our OpenSesame AI assistant!
Hi Sebastiaan,
You were absolutely right. The script does log simultaneous key presses. The problem arose because I asked the participant to press "left ctrl" for emotion [a] and "right ctrl" for emotion [b]. As I found out by doing several test runs, the script will log simultaneous key presses for about every key with the ecception of simultaneous left and right ctrl as well as simultaneous left and right shift keys. No idea why that is, but now I just ask my partecipants to press 2 alfanumeric keys and everything works perfectly.
All the best,
Eveline