我编写了一个Python脚本,如果按下键盘上的键触发钢琴声音,它会将MIDI数据发送到笔记本电脑上的另一个程序
我的问题是:在一架真正的钢琴上,如果我按住一个键,一个音符就会发出持续的声音。但是,如果我在运行脚本时按键盘上的a键,而不是像一架真正的原声钢琴那样,则按键时音符会发出多次声音。我想这个问题可以通过一些if和loop逻辑来解决。我只是不知道怎么做
有人能给我提些建议吗
我的剧本是这样的:
import time
import rtmidi
import mido
import keyboard
outport = mido.open_output('loopMIDI 1')
while True:
#Pad A
if keyboard.is_pressed("a"):
msg = mido.Message("note_on", note=36, velocity=100, time=10)
outport.send(msg)
time.sleep(0.05)
else:
msg = mido.Message("note_off", note=36, velocity=100, time=10)
outport.send(msg)
您需要一个能够记住是否按下该键的变量:
您可以使用^{} 保存有关多个键的信息
在MaxiMouse的大力帮助下,我可以完成我想要的。我使用了MaxiMouse的建议,稍加修改就可以让脚本正常工作
我将把工作代码留在这里
相关问题 更多 >
编程相关推荐