Last active
August 29, 2015 14:02
-
-
Save tsbertalan/b04d28dff6b760a6015f to your computer and use it in GitHub Desktop.
music keys CLI
This file contains hidden or 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
#!/usr/bin/env python | |
''' | |
A brittle little program that emits media keys on the :0 X11 display | |
when it gets certain input characters at the command line. | |
I use this for transforming my phone into a gestural remote control for my | |
Google Play Music on my desktop, using Server Auditor as an Android SSH client, | |
and this extension to listen for media key presses and send them to Google Play Music: | |
https://chrome.google.com/webstore/detail/key-socket-media-keys/fphfgdknbpakeedbaenojjdcdoajihik | |
. | |
I adapt the code from this SO question: | |
http://stackoverflow.com/questions/22397289/finding-the-values-of-the-arrow-keys-in-python-why-are-they-triples | |
''' | |
import sys, tty, termios | |
from os import system | |
class _Getch: | |
def __call__(self): | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(sys.stdin.fileno()) | |
ch = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
def musNext(): | |
system('export DISPLAY=:0; xdotool key XF86AudioNext') | |
def musPrev(): | |
system('export DISPLAY=:0; xdotool key XF86AudioPrev') | |
def musPlay(): | |
system('export DISPLAY=:0; xdotool key XF86AudioPlay') | |
def volUp(): | |
system('export DISPLAY=:0; xdotool key XF86AudioRaiseVolume') | |
def volDown(): | |
system('export DISPLAY=:0; xdotool key XF86AudioLowerVolume') | |
def volMute(): | |
system('export DISPLAY=:0; xdotool key XF86AudioMute') | |
class Handler(object): | |
'''Accumulates a sequence of key ords. | |
Starts the sequence from scratch if a control character (ord==27) | |
is encountered. | |
For a few special keys or sequences, does some actions.''' | |
def __init__(self): | |
print "press x to exit" | |
self.seq = [] | |
def handle(self, key): | |
i = ord(key) | |
if i == 32 or i == 9: # space or tab | |
print "toggle play/pause" | |
musPlay() | |
if i == 109: # m | |
print "toggle mute" | |
volMute() | |
if i == 120: # x | |
print "exiting" | |
exit() | |
if i == 27: # control character | |
self.seq = [] | |
self.seq.append(i) | |
if len(self.seq) == 3: | |
# first sequence is for desktop; second is for phone | |
# (using Server Auditor with the UTF8 charset and xterm "Emulation Type") | |
if self.seq[1:] in ([91, 68], [79, 68]): # left arrow | |
print "previous" | |
musPrev() | |
elif self.seq[1:] in ([91, 67], [79, 67]): # right arrow | |
print "next" | |
musNext() | |
elif self.seq[1:] in ([91, 65], [79, 65]): # up arrow | |
volUp() | |
elif self.seq[1:] in ([91, 66], [79, 66]): # down arrow | |
volDown() | |
def get(h): | |
inkey = _Getch() | |
while True: | |
k=inkey() | |
if k!='': | |
break | |
h.handle(k) | |
def main(): | |
h = Handler() | |
while True: | |
get(h) | |
if __name__=='__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment