Skip to content

Instantly share code, notes, and snippets.

@tailhook
Created September 19, 2010 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tailhook/586682 to your computer and use it in GitHub Desktop.
Save tailhook/586682 to your computer and use it in GitHub Desktop.
import pypm
import time
def listdev(options):
for i in xrange(pypm.CountDevices()):
inf = pypm.GetDeviceInfo(i)
print '{0}.{default} api: {1}, name: {2}, input: {3}, output: {4}'\
.format(i, *inf, default='*' if options.device == i else ' ')
def serve(options):
inp = pypm.Input(options.device)
while True:
if inp.Poll():
buf = inp.Read(128)
print buf
else:
time.sleep(0.1)
def get_options():
import optparse
op = optparse.OptionParser()
op.add_option('-l', '--list',
help="List devices",
dest="action", default=serve, action="store_const", const=listdev)
op.add_option('-d', '--device',
help="Select device for input",
dest="device", default=pypm.GetDefaultInputDeviceID(), type='int')
return op
def main():
op = get_options()
options, args = op.parse_args()
if args:
op.error("No arguments expected")
pypm.Initialize()
try:
options.action(options)
finally:
pypm.Terminate()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment