Advertisement
Guest User

AHK + Nircmd sound output device switch

a guest
Feb 18th, 2020
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I second this. I was looking for a way to set up a hotkey to switch between headphones and speakers and that's how I discovered AHK. Nircmd was the recommended solution and I found some sample scripts that people suggested. From there I made this:
  2.  
  3. ;=== Windows Sound Controls ===
  4. ; Uses Ctrl-Win and a numberpad key:
  5. ; NP/ Toggle output between speakers and headphones
  6. ; NP+ Current Volume +5
  7. ; NP- Current Volume -5
  8. ; NP* Current Volume mute
  9.  
  10. #^NumpadAdd::
  11. SoundSet,+5
  12. return
  13.  
  14. #^NumpadSub::
  15. SoundSet,-5
  16. return
  17.  
  18. #^NumpadMult::
  19. SoundSet,+1,,Mute ; toggle mute
  20. Return
  21.  
  22. ;---------------------------
  23. #^NumpadDiv::
  24.     audioDeviceToggle:=!audioDeviceToggle ; This toggles the variable between true/false
  25.     if audioDeviceToggle
  26.     {
  27.         Run nircmd setdefaultsounddevice "Speakers"
  28.         soundToggleBox("Speakers")
  29.     }
  30.     else
  31.     {
  32.         Run nircmd setdefaultsounddevice "Realtek HD Audio 2nd Output"
  33.         soundToggleBox("Headphones")
  34.     }
  35. Return
  36.  
  37. ;---------------------------
  38. ; Display sound toggle GUI
  39. soundToggleBox(Device)
  40. {
  41.     IfWinExist, soundToggleWin
  42.     {
  43.         Gui, destroy
  44.     }
  45.    
  46.     Gui, +ToolWindow -Caption +0x400000 +alwaysontop
  47.     Gui, Add, text, x35 y8, Default sound: %Device%
  48.     SysGet, screenx, 0
  49.     SysGet, screeny, 1
  50.     xpos:=screenx-275
  51.     ypos:=screeny-100
  52.     Gui, Show, NoActivate x%xpos% y%ypos% h30 w200, soundToggleWin
  53.    
  54.     SetTimer,soundToggleClose, 2000
  55. }
  56. soundToggleClose:
  57.    SetTimer,soundToggleClose, off
  58.     Gui, destroy
  59. Return
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement