Ask user to choose inside a List / Menu

Post your working scripts, libraries and tools.
User avatar
Nitrateag
Posts: 3
Joined: 30 Aug 2021, 08:35

Ask user to choose inside a List / Menu

Post by Nitrateag » 02 Feb 2023, 10:25

Hello

I'm AHK V2 script of an usefull function that I never share, but i use a lot : askMenuList()

Imagine you launch a key stroke, but inside your script, you have an indetermination (which file to use ? which option ?...).
You want to ask to user what he want to do more precisely, get the answare and continue, but you don't wand creaute a new windows just for that.

askMenuList put your script in sleep and open a "contextual menu" style.
Image
When the user made his choice, the script wake-Up and get the answer.

I hope this will be usefull to you like he is for me ^^

Code: Select all


#Requires AutoHotkey >=v2.0-

; THIS IS TESTING/EXAMPLE CODE, COMMENT IT WHEN HE IS USELESS

#SingleInstance Force
#Warn All, MsgBox

; Build your matrix choice
matrixChoice := [
  ['&Txt displayed', 'Firts choice Value'],
  ['Second choice `t Right side information', 2],
  ,                                   ; <-- empty value Add a delimiter
  ['&Sub Menu`thit S',  [             ; <-- sub matrix for sub menu
      ['&Txt displayed, once again', 'Sub Menu - Firts choice Value'],
      ['return 22', 22],
      ['&Last option`thit L', 'Sub Menu - Value returned by last option']
    ]
  ],
  ['&Sub Menu 2',  [         
      ['i have no inspiration', 'Sub Menu 2 - Firts choice Value'],
      ['&Another subMenu `t Hit A',  [   ; <-- sub sub matrix for sub sub menu...
          ['Ok, you find me !', 'Farest value']
]]]]]

; Ask to the user NOW and wait an answer. the value 'Nevermind =P' is the value return in case of no choice from user
valChosen := askMenuList(matrixChoice, 'Nevermind =P')

msgBox 'You chose <' valChosen '>'

; --------------- END TEST/EXAMPLE CODE -------------------------------


Global askMenuList_returnVal

askMenuList(matChoice_diplay_value, noChoice := '')
{
  global askMenuList_returnVal := noChoice

  askMenuList_buildMenu(matChoice_diplay_value).show() ; <-- hope reference on  Menu object will not be lost and object will be delete after the show() =P maybe I have to store him in local variable
  
  return askMenuList_returnVal
}

askMenuList_buildMenu(matMenu)
{
  _menu := Menu()
  for key, display_value in matMenu
  {
    if(IsSet(display_value))
    {
      if(display_value[2] is Array) ; if value is an array ([Name, [...]]), this is a sub-menu
        _menu.Add(display_value[1], askMenuList_buildMenu(display_value[2]))
      else                         
        _menu.Add(display_value[1], askMenuList_select.Bind(display_value[2]))
    }
    else ; <-- else if empty value (no pair [Name, Value]), we add a separator
      _menu.Add()
  }
  return _menu
}

askMenuList_select(value, *)
{
  global askMenuList_returnVal := value
}

[Mod edit: Topic moved to 'AHK v2 Scripts and Functions'.]

Return to “Scripts and Functions (v2)”