pythonpsychopy

Detecting the pressed keys in multi-languages in Psychopy


I'm creating an experiment where I loop/finish a loop depending on the clicked key.

The example below continues the loop when the participant presses the r key. When the participant presses the p or q key, the loop is finished:

keys = event.getKeys()

for thisKey in keys:
    if thisKey == 'r':
        redisplay_image_loop.finished = False
    elif thisKey =='p' or thisKey == 'q':
        redisplay_image_loop.finished = True

The above example works great, but if the keyboard language is set to Hebrew when we start the experiment, the keys are no longer recognized. It only works if the keyboard language is set to English when we run the experiment.

Is there any way to solve this issue? Maybe by checking the key code of the pressed key?

The keys I need are:

p = פ = 80
q = 81 = /
r = ר = 82

Thanks!


Solution

  • Found a solution:

    # Begin Experiment
    current_key = 0
    
    # Each Frame
    keys = event.getKeys()
    
    if len(keys)>0:
        for thisKey in keys:
            current_key = ord(thisKey[0])
            
            # 112=p, 113=q, 114=r
            if current_key == 112 or current_key == 113:
                redisplay_image_loop.finished = True
                continueRoutine = False
            elif current_key == 114:
                redisplay_image_loop.finished = False
                continueRoutine = False
    

    The ord() function returns the keycode of a provided string. You will always get the same number from the current key, no matter which language you use.