私は音楽を書くためにユーザー入力を受け取るPygameプログラムを書いています。特定のイベント(つまり、キーボードまたはマウスの入力)を待機し、ユーザーが各エントリでEnterキーを押すことなく、入力をすぐに分析します。
while (running == 1):
for event in pygame.event.get():
# If event is a keypress
if(event.type == pygame.KEYDOWN):
key = pygame.key.get_pressed() # get_pressed returns an boolean array
# of every key showing pressed or not
# Find which key is pressed
for x in range(len(key)):
if(key[x] == 1):
break
# If a number key is pressed (0-9)
if(x >= 48 and x <=57):
# Set the octave to keypress
gui.setOctave(x-48) # gui is an instance of a class
# that controlls pygame display
これを行うために私が知っている唯一の方法は、無限のwhileループを使用することです。ただし、これは実行にほぼすべてのCPUパワーを消費します。ループを使用せずにこれを実行する別の効率的な方法はありますか?