Advertisement
metalx1000

Send Keys on Midi input press

Feb 23rd, 2016
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import sys, pygame, pygame.midi
  4. import os #for calling xte to 'sendkeys' My not be the best way, but works...on Linux...if you have xautomation installed
  5.  
  6. # set up pygame
  7. pygame.init()
  8. pygame.midi.init()
  9.  
  10. # list all midi devices
  11. for x in range( 0, pygame.midi.get_count() ):
  12.   print pygame.midi.get_device_info(x)
  13.  
  14. # open a specific midi device
  15. i = pygame.midi.Input(3) #change this to your device
  16.  
  17. # run the event loop
  18. while True:
  19.   if i.poll():
  20.     midi_events = i.read(10)
  21.     if (midi_events[0][0][2] != 0):
  22.       key = str(midi_events[0][0][1])
  23.       print "Key is " + key
  24.       if (key == "43"):
  25.         os.system("xte 'str test this out'")
  26.       elif (key == "44"):
  27.         os.system("xte 'str Hello World'")
  28.       elif (key == "45"):
  29.         os.system("xte 'str Hello Bob'")
  30.       elif (key == "46"):
  31.         os.system("xte 'str Good-bye bob'")
  32.       elif (key == "47"):
  33.         os.system("xte 'str World part 2'")
  34.       elif (key == "48"):
  35.         os.system("xte 'str Last one'")
  36.  
  37.   # wait 10ms - this is arbitrary, but wait(0) still resulted
  38.   # in 100% cpu utilization
  39.   pygame.time.wait(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement