Multiple keyboard components in one routine without using a loop

OS: Windows 10
PsychoPy version: v2021.1.4
What is your coding skill?: Pretty rudimentary, I can adapt some code to fit my needs, but it’s rather basic.
What are you trying to achieve?:
I am creating an experiment where participants have to listen to sequences of (two types of) words, ranging from 2 w/seq to 6 w/seq. I have managed to build and code the sounds, but after each sequence, participants need to press f or j for the sounds they just listened to. So not one key per sequence, but one key per word.
I also need to know if the sequence of keys is correct, e.g. if a participant listens to a two-word sequence with the correct key ‘f, j’ then my data will only return 1 if those keys in that order are correct.

What did you try to make it work?:
So far I have tried two solutions:

  1. In the excel sheet where I refer to my sounds ranging from stim_1 to stim_6, I have made a 7th column called seq which would put the above example like [‘f’, ‘j’].
    In the keyboard component, I unchecked ‘Force end of routine’, put the allowed keys as ‘j’, ‘f’, left the stop field blank, and changed ‘constant’ to ‘set every repeat’. In the data tab, Store was set to ‘all keys’, and correct answer is $seq. In a coder component, I put:

Begin Routine:

#initial set up and reset
seq2 = 0

Each Frame (although I also tried this in End Routine):

#every time j or f is pressed one is added

if(keySeq2.keys == 'j' or keySeq2.keys == 'f'):
    seq2 = seq2 + 1

End Routine:

#if two keys are pressed the routine ends

if(seq2 >= 2):
    feedSeq2.finished = True

  1. In the excel sheet where I refer to my sounds ranging from stim_1 to stim_6, I have made 6 more columns ranging from key_1 to key_6. So in the example above key_1 would be ‘f’ and key_2 would be ‘j’.
    For each key, I would create a keyboard component: stop is left blank, I unchecked ‘Force end of routine’ for all but the last keyboard component, put the allowed keys as ‘j’, ‘f’, and changed ‘constant’ to ‘set every repeat’. In the data tab, Store was set to ‘first keys’, and correct answer is $key_+ the number of the stimulus.

(I hope this was clear enough, I can add screenshots if that makes it easier)

What specifically went wrong when you tried that?:

  1. Basically everything? I would press keys, but I would stay within the routine, and I couldn’t find any data in the csv file that even mentioned pressing anything. So either my code is wrong, or what I’m trying to do isn’t possible like this. Hence possibility 2.

  2. Now this one is more complicated. It would continue to the next routine, but there would be absolutely no regularity in when it would move on. Sometimes it would take several seconds, sometimes just the required key presses.

I received this error message in my runner (but only when I ran it the first time):

17.4784     WARNING     Got keycode {} but that code isn't yet known
17.4784     WARNING     Keypress was given unknown key code (174)
18.0448     WARNING     Got keycode {} but that code isn't yet known
18.0448     WARNING     Keypress was given unknown key code (174)
18.8970     WARNING     Got keycode {} but that code isn't yet known
18.8970     WARNING     Keypress was given unknown key code (174)
##### Experiment ended. #####

In my csv file, I did get key presses and it even said which ones were correct, so that was good. But I don’t know how to make it more consistent.

If you need any aditional information, please let me know.

Change Store to “all keys”. Then keySeq2.keys should contain a list where keySeq2.keys[0] is the first key and keySeq2.keys[1] is the second key.

This is interesting, and it does show the randomness of when my experiment moves from keySeq2_1 to keySeq2_2. (As you responded to my second possibility, I assumed you meant that one)

I don’t think you can put a sequence as a correct answer, so I think you will need to score the answer in code.

Is that for possibility 1 or 2? And how can I do that?

The easiest solution can be found here:

1 Like