keyboard icon indicating copy to clipboard operation
keyboard copied to clipboard

onRelease not working

Open JimmyLienert opened this issue 3 years ago • 10 comments

I 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?

JimmyLienert avatar Aug 17 '21 21:08 JimmyLienert

Same issue like mine #475 , it looks to me like its not possible to install 2 hooks at the same time. If you need onPress and onRelease at the same time, use the keyboard.hook function and use KeyboardEvent.event_type to determine if it was a key down or key up event.

johmarjac avatar Aug 18 '21 07:08 johmarjac

Fixing this properly requires a rewrite of the (small) core that handles hotkeys, something that I'm currently working on. Stay tuned!

boppreh avatar Aug 18 '21 10:08 boppreh

Same issue like mine #475 , it looks to me like its not possible to install 2 hooks at the same time. If you need onPress and onRelease at the same time, use the keyboard.hook function and use KeyboardEvent.event_type to determine if it was a key down or key up event.

Thank you! This should help quite a bit! How would I use KeyboardEvent.event_type to differentiate which callback to perform? It seems to me that keyboard.hook just senses if there is a key event, and wouldn't recognize if it was up or down.

JimmyLienert avatar Aug 18 '21 18:08 JimmyLienert

event_type property is either down or up. I am very very new to python but a simple string compare did the trick for me.

So if you had your callback function like that:

def keyboard_hook(e):
    if e.event_type == "down" and e.name == "your key":
        #do sth

johmarjac avatar Aug 18 '21 18:08 johmarjac

def keyboard_hook(e):
    if e.event_type == "down" and e.name == "your key":
        #do sth

So this is setting the event type in the callback? Would I do if e.event_type == "down" for onPress and if e.event_type == "up" for onRelease?

JimmyLienert avatar Aug 18 '21 18:08 JimmyLienert

Yes exactly

johmarjac avatar Aug 18 '21 18:08 johmarjac

Thank you! Also, would I just put both event_type lines into one callback or would I want to still have separate callbacks?

JimmyLienert avatar Aug 18 '21 18:08 JimmyLienert

def keyboard_hook(e):
    if e.event_type == "down" and e.name == "w":
        #W key_pressed
    elif e.event_type == "up" and e.name == "w":
        #W key_released

    if e.event_type == "down" and e.name == "s":
        #S key_pressed
    elif e.event_type == "up" and e.name == "s":
        #S key_released

# and so on

all goes into this one callback

johmarjac avatar Aug 18 '21 18:08 johmarjac

Awesome, thank you so much!! This worked and my code is working as intended!

JimmyLienert avatar Aug 18 '21 18:08 JimmyLienert

Glad I could help, Cheers

johmarjac avatar Aug 18 '21 18:08 johmarjac