psychopy.org | Reference | Downloads | Github

Response triggers cannot be sent as many times as the response key is pressed

OS (e.g. Win10): Win 10
PsychoPy version (e.g. 1.84.x): version 1.84.2
Standard Standalone? (y/n) If not then what?: y
What are you trying to achieve?:
I am showing flickering lines for a long time (12 seconds). Observers need to indicate the number of those lines by pressing number keys on keyboard. I expect observers to respond as many as times as they want to during those 12 seconds. I also have a parallel port: I sent triggers at the start of the experiment, stimulus onset, stimulus offset, and each key press.

What did you try to make it work?:
Following this post, I managed to send the triggers when observers pressed a key.

What specifically went wrong when you tried that?:
However, I cannot send the triggers as many times as the observer presses the same key. One trigger (that corresponds to one response key) can only be sent once. How can I send as many triggers as the same key is pressed?
Here is code snippet where I send the triggers for response keys:

Begin Routine

mark2Sent = False
mark2Terminated = True
mark3Sent = False
mark3Terminated = True

Each Frame

        if resp.status == STARTED:
            theseKeys = event.getKeys(
                keyList=['num_0', 'num_1', 'num_2', 'num_3', 'num_4', 'num_5', 'num_6', 'num_7', 'num_8', 'num_9'])

            # check for quit:
            if "escape" in theseKeys:
                endExpNow = True
            if len(theseKeys) > 0:  # at least one key was pressed
                resp.keys.extend(theseKeys)  # storing all keys
                resp.rt.append(resp.clock.getTime())

        if (resp.status == STARTED) and not mark2Sent:
            if len(resp.keys) > 0:
                if 'num_0' in resp.keys:
                    p_port.setData(int(10))
                    mark_start_time = t
                    mark2Sent = True
                    mark2Terminated = False
                    print (int(10))

        if mark2Sent and not mark2Terminated:
            if t - mark_start_time >= 0.020:
                p_port.setData(int(0))
                mark2Terminated = True
                print (int(0))

        if (resp.status == STARTED) and not mark3Sent:
            if len(resp.keys) > 0:
                if 'num_1' in resp.keys:
                    p_port.setData(int(11))
                    mark_start_time = t
                    mark3Sent = True
                    mark3Terminated = False
                    print (int(11))

        if mark3Sent and not mark3Terminated:
            if t - mark_start_time >= 0.020:
                p_port.setData(int(0))
                mark3Terminated = True
                print (int(0))

Any help would be greatly appreciated!

This is because your code forbids it. Once you set mark2Sent to True, no further response will occur to num_0, and likewise for num_1 once mark3Sent is set to True.

Try reading through the logic of your code and see what will happen on each time the code is called. The logic will need to be altered if you want to keep responding to keypresses, but it gets tricky, as you also need to ensure that the pulse duration is maintained.

Thanks a lot for your response @Michael. I got around it like this: I no longer set a condition as if not mark2Sent. Doing this gave endless number of pulses when I press the key once. So I also added resp.keys.remove('num_0') to make sure that the pulse is sent once. This prevented response keys to be saved, so I also added another variable to save the pressed keys:
Begin Routine

pressedKeys = []

Each Frame

    if resp.status == STARTED:
        if len(resp.keys) > 0:
            if 'num_0' in resp.keys:
                p_port.setData(int(10))
                mark_start_time = t
                mark2Sent = True
                mark2Terminated = False
                #print (int(10))
                resp.keys.remove('num_0')
                pressedKeys.append('num_0')

Now triggers are sent whenever I press a key and all the pressed keys are saved (though I don’t like how roundabout it is). I am all ears if you have a more elegant solution!

Thanks a lot,
-Mia