Program nonresponsive to key presses

OS (e.g. Win10): macOS Ventura
PsychoPy version (e.g. 1.84.x): v2023.2.3 on laptop
**Using standard standalone
What are you trying to achieve?: I am running an experiment using the Balloon Analogue Risk Task. I’m using the demo provided by PsychoPy. I need ‘space’ and ‘return’ to do several things:

  • BOTH need to generate a random number between 1 and 20, and if that number is 1, trigger a sound to play
  • ‘SPACE’ needs to inflate the balloon and increase earnings (just like in the original demo)
  • ‘RETURN’ needs to bank the earnings and end the routine

What did you try to make it work?:
Here is the most recent version of the code:
#Begin Experiment
from psychopy import sound, core, event

import random

ratiosoundplayer = sound.Sound(“Audio Files/baby_chorus_corrected.wav”)
sound_files = [
“Audio Files/file1.wav”,
“Audio Files/file2.wav”,
“Audio Files/file3.wav”,
“Audio Files/file4.wav”,
“Audio Files/file5.wav”,
“Audio Files/file6.wav”,
“Audio Files/file7.wav”,
“Audio Files/file8.wav”,
“Audio Files/file9.wav”,
“Audio Files/file10.wav”,
]

random.shuffle(sound_files)
selected_sound_files_per_trial =
key_count = 0
key_onsets =
Probability = 0
audio_index = 0
last_key_time = 0

#Each Frame
keys = key_experiment.getKeys([‘space’, ‘return’]) # Get all keypresses since last frame

if keys:
key_count += 1
print(“key count:”, key_count)
Probability = random.randint(1, 20)
print(“random number:”, Probability)
if ‘space’ in keys:
nPumps += 1
if nPumps > maxPumps:
popped = True
continueRoutine = False

# If Probability is 1 and the sound is not playing
if Probability == 1 and ratiosoundplayer.status != 1:
    audio_index += 1
    if audio_index < len(sound_files):
        ratiosoundplayer.setSound(sound_files[audio_index])
        ratiosoundplayer.play()
        print("Playing sound:", sound_files[audio_index])

if ‘return’ in keys:
continueRoutine = False

I have a key component (key_experiment) and a sound component (ratiosoundplayer).

What specifically went wrong when you tried that?:
It will only register key presses occasionally. When it does register, everything works as it should: key_count increases, a random number is generated, if the number is 1 a sound plays, nPumps increases, the earnings increases and it banks the earnings or loses all earnings when return is pressed (not present in the code, embedded in respective text components) or nPumps is greater than maxPumps, respectively, and then the routine ends correctly. But nothing happens most of the time when I press keys. I have been getting messages like this every time I run the program, and this has happened since the very beginning back when the code looked quite different:
0.0873 WARNING t of last frame was 46.59ms (=1/21)
1.5785 WARNING Multiple dropped frames have occurred - I’ll stop bothering you about them!
This is right in the beginning of running the experiment. I get these same messages every time I run it on a different computer (Windows 11, v2024.2.1) as well. Not sure if it’s related to the problem at all.

Sorry in advance if there is any important information I missed.

I think your issue is that you can only get key presses that occur in between the keyboard component and the code component.

Replace your Each Frame code with:

if key_experiment.keys:
     if len(key_experiment.keys) > key_count:
          key_count += 1
          print("key count:", key_count)
          Probability = random.randint(1, 20)
          print("random number:", Probability)
          if 'return' in key_experiment.keys:
               continueRoutine = False
          elif key_experiment.keys[-1] == 'space':
               nPumps += 1
               if nPumps > maxPumps:
               popped = True
               continueRoutine = False

# If Probability is 1 and the sound is not playing
          if Probability == 1 and ratiosoundplayer.status != 1:
              audio_index += 1
              if audio_index < len(sound_files):
                  ratiosoundplayer.setSound(sound_files[audio_index])
                  ratiosoundplayer.play()
                  print("Playing sound:", sound_files[audio_index])