Okay, you need to understand how midi works. MIDI event is triggered(input_device.poll() is True) when there is a change of state of any synth keys, e.g. key was pressed or released. When this happens, your data variable contains list with [state, note, velocity, something(I couldn't identify it)]. Also, there are 15 channels. I found out that key press calls state 128+channel_number and key release calls event with state 144+channel_number. You have to keep track of actually pressed notes by yourself. Here's sample code for what you're trying to do:
pressed = []
def readInput(input_device, channel):
while True:
if input_device.poll():
event = input_device.read(1)[0]
data = event[0]
# [state, note, velocity, something(always 0 for me)]
timestamp = event[1]
if data[0] == 128 + channel: # note off on channel data[0]-128
if data[1] in pressed:
pressed.remove(data[1])
if data[0] == 144 + channel: # note on on channel data[0]-144
if not data[1] in pressed:
pressed.append(data[1])
if all(el in pressed for el in [36, 40, 43, 46]):
print("chord = Cmaj7")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…