Skip to content

Instantly share code, notes, and snippets.

@alvesvaren
Created July 18, 2018 12:55
Show Gist options
  • Save alvesvaren/dc94df6666d8a6639b6cd34765b3f570 to your computer and use it in GitHub Desktop.
Save alvesvaren/dc94df6666d8a6639b6cd34765b3f570 to your computer and use it in GitHub Desktop.
import keyboard, soco, time
from win32api import GetKeyState
from win32con import VK_SCROLL
trigger = 77
speaker_name = 'Example'
while True:
try:
player = soco.discovery.by_name(speaker_name)
break
except Exception:
pass
def sonos(type, arg='', var=None):
if not var:
if type=='play':
if player.get_current_transport_info()['current_transport_state'] == 'PLAYING':
type='pause'
exec(f'player.{type}({arg})')
print(f'\'{type}()\' executed.')
else:
exec(f'player.{type} {var}')
print(f'\'{type} {var}\' executed.')
def add_hotkeys():
keyboard.add_hotkey((-179), sonos, args=['play'], suppress=True)
keyboard.add_hotkey((-178), sonos, args=['stop'], suppress=True)
keyboard.add_hotkey((-177), sonos, args=['previous'], suppress=True)
keyboard.add_hotkey((-176), sonos, args=['next'], suppress=True)
keyboard.add_hotkey((-175), sonos, args=['volume', '', '+=1'], suppress=True)
keyboard.add_hotkey((-174), sonos, args=['volume', '', '-=1'], suppress=True)
keyboard.add_hotkey((-173), sonos, args=['mute', '', '= not player.mute'], suppress=True)
if GetKeyState(VK_SCROLL):
print('Scroll lock is on.')
else:
print('Scroll lock is off.')
while True:
if GetKeyState(VK_SCROLL) == 0 or GetKeyState(VK_SCROLL) == -128:
try:
keyboard.unhook_all_hotkeys()
print('Unhooked all hotkeys!')
except AttributeError:
print('No hotkeys found.')
else:
add_hotkeys()
print('Added hotkeys!')
keyboard.wait(70)
time.sleep(1)
print('Scroll lock was pressed.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment