(SOLVED) How to toggle the Icon for microphone listen state and the state itself?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Technomage Awunes
Posts: 9
Joined: 25 Oct 2021, 20:33

(SOLVED) How to toggle the Icon for microphone listen state and the state itself?

Post by Technomage Awunes » 25 Oct 2021, 20:52

I found the base of the following script at this site: https://superuser.com/questions/695169/listen-to-device-autohotkey-need-advice-to-finish

There are a couple questions in the comments I've put in it

Code: Select all

#SingleInstance
;#NoTrayIcon

FileInstall, micOff.ico, micOff.ico 
FileInstall, micOn.ico, micOn.ico
FileInstall, toggle.ini, toggle.ini
;FileInstall, toggle.ahk, toggle.ahk ;is there a way to include the ahk you are attempting to compile in the compile to be extracted upon running the exe

PgUp::
isExist:=WinExist("ahk_exe rundll32.exe ahk_class #32770")
Run, % "rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,recording",,, uPID
WinWait, % "ahk_pid"uPID,, 1

WinWait,Sound

Send {CTRL DOWN}{Down}{CTRL UP}
ControlGet, List, List,, SysListView321, % "ahk_pid"uPID

FileRead, OutputVar, toggle.ini

Loop, Parse, List, `n
{
    IfInString, A_LoopField, %OutputVar%
    {
        ControlClick,&Properties
        Send {CTRL DOWN}{Tab}{CTRL UP}
        Send {Space}
        Send {Enter}
        break
    }
    else 
    {
        ControlSend,SysListView321,{Down}
    }
}

WinWait,Sound
Send {Esc}
Menu,Tray,Icon,micOn.ico,,1
Return

PgDn::
isExist:=WinExist("ahk_exe rundll32.exe ahk_class #32770")
Run, % "rundll32.exe shell32.dll,Control_RunDLL mmsys.cpl,,recording",,, uPID
WinWait, % "ahk_pid"uPID,, 1

WinWait,Sound

Send {CTRL DOWN}{Down}{CTRL UP}

ControlGet, List, List,, SysListView321, % "ahk_pid"uPID

FileRead, OutputVar, toggle.ini

Loop, Parse, List, `n
{
    IfInString, A_LoopField, %OutputVar%
    {
        ControlClick,&Properties
        Send {CTRL DOWN}{Tab}{CTRL UP}
        Send {Space}
        Send {Enter}
        break
    }
    else 
    {
        ControlSend,SysListView321,{Down}
    }
}

WinWait,Sound
Send {Esc}
Menu,Tray,Icon,micOff.ico,,1
Return

Home::
Reload

Del::
Menu,Tray,Icon,micOff.ico,,1 ; Need to know how to set the Icon to AHK's Suspend icon as my custom ones above override once used
Suspend
Return

Ins::
Menu,Tray,Icon,micOff.ico,,1 ; Need to know how to set the Icon to AHK's Pause icon as my custom ones above override once used
Pause
Return

End::
ExitApp
I'd like the icon to exist so I'll comment out the #NoTrayIcon

But I'd like the icon to switch between micOff.ico and micOn.ico, respective to the actual state of the "listen" option.

I know I need to put:

[code]Menu,Tray,Icon,micOff.ico,,1[/code]
and
[code]Menu,Tray,Icon,micOn.ico,,1[/code]

somewhere but not sure where.

I'll probably compile it so the default Icon is one of the two and then just need...


OH I think I got it. Will edit my own post if I do. (Or remove it if needed)
I think the code will have to be re-written a bit more than I can fathom at the moment. I am not happy with the way I've made it work. As it stands the PgUp turns on the mic and PgDn turns it off but only in icon as both will actually toggle it :( SOO close! maybe?

Anyway, anyone wanna contribute?

Final Edit:

After some help from AHK DIscord I found a 3rd party software that was able to change the listen state via command so here's the final script. You can review it to figure out the program name and then find the program online.

Code: Select all

;Version: toggle.microphone.0.06
#SingleInstance, Force
FileInstall, micOff.ico, micOff.ico 
FileInstall, micOn.ico, micOn.ico
FileInstall, toggle.ini, toggle.ini ; enter the name of the device you want to toggle in this file
FileInstall, soundvolumeview-x64.zip, soundvolumeview-x64.zip
FileInstall, SoundVolumeView.exe, SoundVolumeView.exe
FileInstall, toggle microphone.0.06, toggle microphone.0.06.ahk ;note in order to include the .ahk in the compiled version you need to copy the file and remove it's extention ... this line will then add it to the exe.
PgUp::
FileRead, OutputVar, toggle.ini
Run, SoundVolumeView.exe /SetListenToThisDevice "%OutputVar%" 1
Menu,Tray,Icon,micOn.ico,,1
Return

PgDn::
FileRead, OutputVar, toggle.ini
Run, SoundVolumeView.exe /SetListenToThisDevice "%OutputVar%" 0
Menu,Tray,Icon,micOff.ico,,1
Return

Home::
Reload

Del::
;Menu, tray, icon, AutoHotkey.exe, 3 ;If you attempt to use this it fails to respond to the Del key again
Suspend
Return

Ins::
Edit
Return

End::
ExitApp
Here is the final exe with all the files needed. see most recent script to see what is included.
toggle microphone.0.06.zip
Here is the final version of the script including the needed files
(816.41 KiB) Downloaded 16 times

Return to “Ask for Help (v1)”