Last active
August 29, 2015 14:22
-
-
Save YellowOnion/26915f9f8f974681d03e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyHook, pythoncom | |
import rtmidi_python as rtmidi | |
# this app will list all MIDI devices and associated numbers | |
# Pick the correct number associated, and restart the app. | |
MIDI_DEVICE = 1 | |
# key codes helpers | |
KP_7 = 103 | |
KP_8 = 104 | |
KP_9 = 105 | |
KP_4 = 100 | |
KP_5 = 101 | |
KP_6 = 102 | |
KP_1 = 97 | |
KP_2 = 98 | |
KP_3 = 99 | |
# Key to Midi note mapping, all other keys are ignored | |
Key = { KP_7: 48, | |
KP_8: 49, | |
KP_9: 50, | |
KP_4: 51, | |
KP_5: 52, | |
KP_6: 53, | |
KP_1: 54, | |
KP_2: 55, | |
KP_3: 56 | |
} | |
def on_midi(code, midi, state): | |
if not state[code]: | |
state[code] = True | |
try: | |
midi.send_message([0x90, Key[code], 100]) | |
except KeyError: | |
pass | |
def off_midi(code, midi, state): | |
state[code] = False | |
try: | |
midi.send_message([0x80, Key[code], 100]) | |
except KeyError: | |
pass | |
def on_key_construct(midi, state): | |
def on_key(event): | |
on_midi(event.KeyID, midi, state) | |
return True | |
return on_key | |
def off_key_construct(midi, state): | |
def off_key(event): | |
off_midi(event.KeyID, midi, state) | |
return True | |
return off_key | |
print "Available MIDI devices:" | |
midi = rtmidi.MidiOut() | |
for i, port in enumerate(midi.ports): | |
print str(i) + ": " + port | |
print "Connecting to device: " + midi.ports[MIDI_DEVICE] | |
midi.open_port(MIDI_DEVICE) | |
state = {} | |
for x in range(256): | |
state[x] = False | |
hookM = pyHook.HookManager() | |
hookM.KeyDown = on_key_construct(midi, state) | |
hookM.KeyUp = off_key_construct(midi, state) | |
hookM.HookKeyboard() | |
pythoncom.PumpMessages() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment