onRelease not working
See original GitHub issueI am working on a synthesyzer, and my current objective is to get keyboard inputs and turn them into MIDI outputs. Here is the code I’m using:
`#Import libraries
import time
import rtmidi
import keyboard
#Define midi commands
midiout = rtmidi.MidiOut()
available_ports = midiout.get_ports()
#Callback when key is released
def onRelease(self):
global offNote
#Define the command to turn off a note, then turn it off.
for i in range(len(keys)):
note_off = [0x80, i+30, 0]
midiout.send_message(note_off)
#Callback when key is pressed def onPress(self): global onNote #Define the command to turn on a note, then turn it on. for i in range(len(keys)): if keyboard.is_pressed(keys[i]) == True: global onNote onNote = i print(keys[i]) note_on = [0x90, onNote+30, 112] midiout.send_message(note_on)
#List of the 61 keys to listen for keys = [41, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 43, 58, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 28, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 29, 125, 56, 57, 100, 126, 127, 97]
#Check for midi ports if available_ports: midiout.open_port(0) else: midiout.open_virtual_port(“My virtual output”) #Stop all notes when program is started for i in range(len(keys)): note_off = [0x80, i+30, 0] midiout.send_message(note_off)
try: while True: #Continue a loop sensing for any key being pressed or released and running the respective callback keyboard.on_press(onPress) keyboard.on_release(onRelease)
except: #Stop all notes when program is stopped for i in range(len(keys)): note_off = [0x80, i+30, 0] midiout.send_message(note_off)` With this code, I can start a note, but the note doesn’t stop when the key is released. What should I do?
Issue Analytics
- State:
- Created 2 years ago
- Comments:10 (1 by maintainers)
Top GitHub Comments
Fixing this properly requires a rewrite of the (small) core that handles hotkeys, something that I’m currently working on. Stay tuned!
Glad I could help, Cheers