Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

How to select an item from a GUI menu?


  • Please log in to reply
6 replies to this topic
wpb
  • Members
  • 5 posts
  • Last active: Dec 19 2015 05:00 PM
  • Joined: 15 Jun 2012

I've searched Google and these forums and not managed to find an answer to my problem.

 

I have created a simple AHK menu and show it on a hotkey press with Menu, MyMenu, Show - it all works as expected.

 

However, I want to open the menu with a certain item already highlighted (so that just hitting Enter will do the business). I thought I could do a Send, {Down n} to accomplish that (with 'n' being the item number I wanted to highlight), but it seems that Menu, Show is blocking the script.

 

How do I achieve the result I'm looking for, please?



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

What is the code you're currently using?

 

Edit: This is a good question actually. I thought maybe using SetTimer would work, but it doesn't. The appearance of a Menu seems to totally pause the script until the menu is dismissed. I even tried to lower the thread's priority (after using SetTimer, in case that made a difference like the priority of the thread carrying over). Nothing I've thought to do works. Except one hell of an inelegant solution:

^6::
Menu, abc, Add, Item1
Menu, abc, Add, Item2
Menu, abc, Default, Item2
Menu, abc, Add, Item3
Run, uglyscript.ahk
Menu, abc, Show
return

bumpone:
Send {Down}
return
Sleep 500
Send {Down 2}

Those are two separate scripts, the latter being called uglyscript.ahk (And saved in the same directory as the original script).



wpb
  • Members
  • 5 posts
  • Last active: Dec 19 2015 05:00 PM
  • Joined: 15 Jun 2012

Wow, that is ugly! ...I mean, thanks! ;) Just kidding. Seriously, thanks for the suggestion. Does anyone else have any other (possibly less ugly!) suggestions? TIA,



lblb
  • Members
  • 120 posts
  • Last active: Dec 02 2015 08:05 AM
  • Joined: 22 May 2012

Since menus always stop all threads, your strategy may be difficult to implement. You could try using a Gui instead and a different control. Here is an example using a listbox:

#SingleInstance, force

Gui, +hwndhGUI +LastFound +AlwaysOnTop -Caption +ToolWindow
Gui, Margin, 0, 0
Gui, Add, ListBox, x0 y0 w100 h200 vColorChoice, Red|Green|Blue|Black|White|Cancel
Return

F2::
GuiControl, Choose, ColorChoice, 2
Gui, Show,, Test listbox
Return

F3::
GuiControl, Choose, ColorChoice, 3
Gui, Show,, Test listbox
Return

F4::
GuiControl, Choose, ColorChoice, 4
Gui, Show,, Test listbox
Return


#IfWinActive Test listbox
Enter::
Gui, Submit, Hide
If ColorChoice = Red
	Msgbox, red
If Colorchoice = Green
	Soundbeep
Return
#If


wpb
  • Members
  • 5 posts
  • Last active: Dec 19 2015 05:00 PM
  • Joined: 15 Jun 2012

Oh, that's great. Thank you!



bruno
  • Members
  • 635 posts
  • Last active: Nov 04 2015 02:26 PM
  • Joined: 07 Mar 2011

 

Since menus always stop all threads, your strategy may be difficult to implement. You could try using a Gui instead and a different control. Here is an example using a listbox:

#SingleInstance, force

Gui, +hwndhGUI +LastFound +AlwaysOnTop -Caption +ToolWindow
Gui, Margin, 0, 0
Gui, Add, ListBox, x0 y0 w100 h200 vColorChoice, Red|Green|Blue|Black|White|Cancel
Return

F2::
GuiControl, Choose, ColorChoice, 2
Gui, Show,, Test listbox
Return

F3::
GuiControl, Choose, ColorChoice, 3
Gui, Show,, Test listbox
Return

F4::
GuiControl, Choose, ColorChoice, 4
Gui, Show,, Test listbox
Return


#IfWinActive Test listbox
Enter::
Gui, Submit, Hide
If ColorChoice = Red
	Msgbox, red
If Colorchoice = Green
	Soundbeep
Return
#If

could you explain this in the last block, specifically?:

#IfWinActive

#If



Exaskryz
  • Members
  • 3249 posts
  • Last active: Nov 20 2015 05:30 AM
  • Joined: 23 Aug 2012

#IfWinActive is simply limiting the Enter hotkey, which will be the key you can use to select your item, to the context of when your GUI is available. The F2, F3, and F4 hotkeys are there to preselect a ColorChoice as the "menu" pops up. Without #IfWinActive here, the Enter key will essentially be disabled when you wanted to use it normally.

 

The #If at the end isn't necessary if this is a final script. However, it is something overlooked when people merge scripts or add onto a script. If any hotkeys are added to the script underneath the #If, then it'll have a global context and activate no matter what windows are in focus. But if the #If isn't explicitly included in sample scripts given to people, they may not realize they have to redefine #If and will find their additions not working as expected.

 

Does that answer the questions or confusion you had for this block?