Record key presses and end routine with a spacebar

Hi all,

I would like to record the keys pressed by the participant (any key) in a given routine while ending the routine when the participant presses the spacebar. In other words, I want to record all keys (i.e., export the key pressed to the results .csv file) and end the routine when the spacebar is pressed. Is this feasible in builder? If not, how would you do it in a code component?

Thanks!

Hello,

not most elegant probably but possible.

Simply add two keyboard-components, one registering all keys (allowed Keys) except space, the other only registering space. Set the latter to Data - store - nothing. This components ends the routine. Uncheck Force End Routine in the former component.

Best wishes Jens

1 Like

It works perfectly, thank you! I have a bonus question: I am using an AZERTY keyboard. Do you know whether it is possible (and how) to specify this in the experiment so that the recorded key presses correspond to my keyboard? Or should I translate the keys myself from QWERTY to AZERTY after data collection?

Hello,

what do you use to register the answer?

BTW, it is better to start a new topic when a new problem arises. It facilitates searches.

Best wishes Jens

Hi,

thank you for your answer.

I am using the keyboard of my laptop. Also, I am unsure how to record all keys in a keyboard component (I could not find the information in the documentation). Should I put “all” or “any” (or something else) in the “allowed Keys” box?

Best wishes,

Ladislas

Hello,

well in one approach you don’t use keyboard-component but some code to register the keypresses

Put in the Begin-tab of a code-component the following code:

inputText = ""
actualKeys= ""
shift_flag = False
antwort.alignHoriz ='center'
event.clearEvents('keyboard')

In the every frame tab, add this:

actualKeys = event.getKeys()
n = len(actualKeys)
i = 0
while i < n:        # solange was eingegeben wurde und verarbeitet werden muss
    if actualKeys[i] == 'return':
        # pressing RETURN means time to stop
        continueRoutine = False
        break
    elif actualKeys[i] == 'backspace':
        inputText = inputText[:-1]  # lose the final character
        i = i + 1
    elif actualKeys[i] == 'z':
        inputText += 'y'
        i = i + 1
    elif actualKeys[i] == 'y':
        inputText += 'z'
        i = i + 1
    elif actualKeys[i] == 'bracketleft':
        inputText += 'ĂŒ'
        i = i + 1
    elif actualKeys[i] == 'apostrophe':
        inputText += 'Ă€'
        i = i + 1
    elif actualKeys[i] == 'semicolon':
        inputText += 'ö'
        i = i + 1        
    elif actualKeys[i] == 'space':
        inputText += ' '
        i = i + 1
    elif actualKeys[i] in ['minus', '-', 'num_subtract', 'Minus', 'slash']:
        inputText += '-'
        i = i + 1
    elif actualKeys[i] in ['lshift', 'rshift', 'SHIFT']:
        shift_flag = True
        i = i + 1
    else:
        if len(actualKeys[i]) == 1:
            # we only have 1 char so should be a normal key, 
            # otherwise it might be 'ctrl' or similar so ignore it
            if shift_flag:
                inputText += actualKeys[i].upper()
                shift_flag = False
            else:
                inputText += actualKeys[i]
        i = i + 1

In the End Routine tab add

thisExp.addData("Antwort", inputText)
inputText = ""

The code-component has to be the first element in your routine. Use a Text-component to display the text ($inputText, set every frame).

Notice that this code recodes some key y → z, z → y, and some Umlauts which is perhaps what you need. In addition, you need to adapt the stop routine part from ‘return’ to ‘space’. Notice that ‘space’ is accepted by the ifelse-construction. So delete the relevant line.

Best wishes Jens