Get Text of Option Highlighted in Context Menu

Get help with using AutoHotkey and its commands and hotkeys
Shotdox
Posts: 1
Joined: 20 Jun 2018, 09:42

Get Text of Option Highlighted in Context Menu

20 Jun 2018, 10:01

Hi,

I have been trying to write a simple script that opens up the windows sound settings and Disables/Enables my speakers, so that I can switch between my headphones and speakers with a key press.

So far I have:

Code: Select all

*F6::
Run, mmsys.cpl
WinWait,Sound
; Press the down key twice to select my speakers
ControlSend,SysListView321,{Down 2}
; Open the options for my speakers and press down 3 times to highlight disable
Send {AppsKey}{Down 3}
Send {Enter}
ControlClick,OK
return
Which disables my speakers as I want, but I want the same key to enable my speakers as well so I want something more like this:

Code: Select all

*F6::
Run, mmsys.cpl
WinWait,Sound
; Press the down key twice to select my speakers
ControlSend,SysListView321,{Down 2}

; Open the options for my speakers and press down twice, if the highlighted option is "Enable", send enter, otherwise press down one more time and send enter

ControlClick,OK
return
I have no idea how to get what the highlighted option is though, I have tried ControlGetFocus and ControlGetText, but they give "SysListView321" and "Select a playback device below to modify its settings:"

I have not used ahk before and any help would be appreciated!

Thanks
User avatar
TLM
Posts: 1338
Joined: 01 Oct 2013, 07:52

Re: Get Text of Option Highlighted in Context Menu

20 Jun 2018, 14:30

You have to loop through the listview's items to get each rows text.
Here's an example of such a loop

Code: Select all

*F6::
Run, mmsys.cpl
WinWait Sound ahk_class #32770

ControlGet, List, List,, SysListView321, Sound ahk_class #32770
Loop, Parse, List, `n
{
	ItemText := StrSplit( A_LoopField, "`t" ) ; << splits item's text into tab delimited array
	if ( !InStr( A_LoopField, "Disabled" ) && ItemText[1] = "Speakers" )
	{
		MsgBox code to disable speakers
	}
	else if ( ItemText[1] = "Speakers" )
	{
		MsgBox code to enable speakers
	}
}
return
You just have to replace the Msgbox blocks with your code.
hth

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Mipha, panhartstuff, Quisquose, Syrup and 47 guests