2
votes

I looked around the AHK forums, google, and here on stack overflow and could not find any method for pulling this off, so thanks BIG TIME in advance if you can figure this one out. I am using notepad in my examples, but I'm writing this script for a complicated Point Of Sale program.

I want to do something almost exactly like this, but using the application menus as described below instead of a button.

I would like to have a script that performs an action if a certain menu item is clicked. It does not matter whether that is determined by menu position, label or otherwise. Cursor position, however, is probably not viable for my needs.

A simple example of this would be to open a MsgBox when File-Open is selected with the mouse in notepad.

I have implemented a few techniques that enabled me to pull variables out of most control objects following a left click (buttons, drop-down menus, etc), but when I use the application menu (for example, in notepad: file-save or file-open) It returns a blank string. For example:

#z::
  MouseGetPos,,,,Ctrl,2
  ControlGetText, Text,,ahk_id %Ctrl%  
  MouseClick, left
  Sleep 2500
  MsgBox, %Text%

(Note that I used windows-z so as to not return a MsgBox after every left-click.)

Will return 'cancel' if the cancel button was pressed in the 'file-save as' dialogue of notepad, but returns nothing if I activate it while selecting anything from the application menus (file, edit, format, etc.)

I know that these menus are accessible to AHK, as you can do the following do activate 'File-Save' from the notepad menu:

WinMenuSelectItem, Untitled - Notepad,File,Save

But when I use window spy, I see the objects behind the menu, rather than strings that are actually in the menu.

Are these menus actually separate windows that I need to switch to active to read? Or am I trying to do something impossible?

Big thanks in advance, this is something that's been driving me crazy!

2

2 Answers

2
votes

Menus are not that easy to get info from, at least not with autohotkeys built-in commands, you will need to use DllCall's to get the info you need.

I have made you an example that works with notepad for me on win7 64b

What i did in the example is look for notepad run it if its not there then get the ID (hWnd) of the notepad window

When you press ctrl + LButton a DllCall to GetMenu using the ID of the notepad window is used to get a menu handle.

After that more DllCall's are used to get things like GetMenuItemCount, GetSubMenu and GetMenuItemInfo.

What this does is it lets us loop over the menu items one by one and getting info about them, in this example we're getting the state of the items, specifically the highlight state.

When an item is found that has the highlight state the script stores the indexs of the item and breaks the loops.

As this is only an example all it does is display a string message with the index numbers and sends a LButton press

Example:

ifWinNotExist, ahk_class Notepad
{
    Run, notepad.exe
    WinWait, ahk_class Notepad
}
WinGet, hWnd, ID, 


^Lbutton::

hMenu :=DllCall("GetMenu", "Uint", hWnd)

loop % (count := DllCall("GetMenuItemCount", "ptr", hMenu))
{
    menu_index := A_index

    ; Note that menu item positions are zero-based.
    hSubMenu := DllCall("GetSubMenu", "Uint", hMenu, "int", (A_index-1))

    ; Set the capacity of mii to sizeof(MENUITEMINFO)
    VarSetCapacity(mii, A_PtrSize=8 ? (Size:=80) : (Size:=48), 0)
    ; Set the cbSize field to sizeof(MENUITEMINFO)
    NumPut(Size, mii, 0)

    ; Set the mask to whatever you want to retrieve.
    ; In this case I set it to MIIM_STATE=1.
    NumPut(1, mii, 4)

    loop % (subCount := DllCall("GetMenuItemCount", "ptr", hSubMenu))
    {
        Sub_index := A_index

        ; Note that menu item positions are zero-based.
        DllCall("GetMenuItemInfo", "UInt", hSubMenu, "UInt", (A_index-1), "UInt", 1, "UInt", &mii)

        ; Get the state field out of the struct.
        fState := NumGet(mii, 12)

        if (fState & 0x80) ; 0x80 MFS_HILITE | 0x8 MFS_CHECKED
        {
            MenuString := "Found in top #" menu_index " Sub pos #" Sub_index
            break 2
        }
    }
}

if (MenuString)
{
    Tooltip % MenuString
}
else
{
    tooltip no menu item highlighted
}
send {Lbutton}
return

I hope that helps you get an idea of what is needed to do what your trying to do, other possible DllCalls to Look at are MenuItemFromPoint, GetMenuString.

This can take time to get right, but with the Dllcalls i have shown in this example, I hope that you can find other examples of how to use them with autohotkey on the forums.

1
votes
  • This AutoHotkey script should do what you require.
  • It has been tested on Notepad (Windows 7), and should be readily adaptable to other programs that use the standard context menus still used in the vast majority of software. Although many programs use custom menu bars, the context menus are often still standard context menus.
  • I have provided two scripts, one will show a ToolTip and block triggering a menu item, whenever any menu item is pressed, which is useful for understanding and for diagnostic purposes. The second script will show a ToolTip and block triggering any menu item if its text string starts with 'Open'.
  • Note: the text of the Notepad menu item is Open... Ctrl+O (Open...[tab]Ctrl+O) and not simply 'Open'.

-

;==================================================

;tested on Notepad (Windows 7)
;block any clicks on any menu items when notepad is the active window
;note: to bypass this and click an item anyway, use ctrl+click

;note: requires Acc.ahk library in AutoHotkey\Lib folder
;https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk
;on right of screen right-click Raw, Save target as...

#IfWinActive, ahk_class Notepad
;LButton::
CoordMode, Mouse, Screen
MouseGetPos, , , hWnd
WinGetClass, vWinClass, ahk_id %hWnd%

if vWinClass in #32768
{
vCount++ ;your code here
ToolTip blocked %vCount%, 500, 200 ;your code here
Return
}

SendInput {LButton Down}
KeyWait, LButton

MouseGetPos, vPosX, vPosY, hWnd
WinGetClass, vWinClass, ahk_id %hWnd%

if vWinClass in #32768
{
ControlSend, Dummy, {Click, 0, 0}, ahk_class Notepad

vCount++ ;your code here
ToolTip blocked %vCount%, 500, 200 ;your code here
Return
}

SendInput {LButton Up}
Return
#IfWinActive

;==================================================

;tested on Notepad (Windows 7)
;block any clicks on the 'open' menu item when notepad is the active window
;note: to bypass this and click an item anyway, use ctrl+click

;note: requires Acc.ahk library in AutoHotkey\Lib folder
;https://github.com/Drugoy/Autohotkey-scripts-.ahk/blob/master/Libraries/Acc.ahk
;on right of screen right-click Raw, Save target as...

#IfWinActive, ahk_class Notepad
LButton::
CoordMode, Mouse, Screen
MouseGetPos, , , hWnd
WinGetClass, vWinClass, ahk_id %hWnd%

if vWinClass in #32768
{
    vItemText := ""
    oAcc := Acc_Get("Object", "1", 0, "ahk_id " hWnd)
    Loop, % oAcc.accChildCount
    if (oAcc.accState(A_Index) & 0x80) ;MF_HILITE := 0x80
    if (1, vItemText := oAcc.accName(A_Index))
    break

    if (SubStr(vItemText, 1, 4) = "Open")
    {
    vCount++ ;your code here
    ToolTip blocked %vCount%, 500, 200 ;your code here
    Return
    }
}

SendInput {LButton Down}
KeyWait, LButton

MouseGetPos, vPosX, vPosY, hWnd
WinGetClass, vWinClass, ahk_id %hWnd%

if vWinClass in #32768
{
ControlSend, Dummy, {Click, 0, 0}, ahk_class Notepad

    vItemText := ""
    oAcc := Acc_Get("Object", "1", 0, "ahk_id " hWnd)
    Loop, % oAcc.accChildCount
    if (oAcc.accState(A_Index) & 0x80) ;MF_HILITE := 0x80
    if (1, vItemText := oAcc.accName(A_Index))
    break

    if (SubStr(vItemText, 1, 4) = "Open")
    {
    vCount++ ;your code here
    ToolTip blocked %vCount%, 500, 200 ;your code here
    Return
    }
}

SendInput {LButton Up}
Return
#IfWinActive

;==================================================

Note:
The code samples posted in the two links below, achieve related goals.
Is it possible to catch the close button and minimize the window instead? AutoHotKey
AutoHotKey: Run code on Window Event (Close)

Note:
The function JEE_MenuIsActive, link below, can be used to check whether the menu bar/sysmenu bar is active in order to distinguish title bar menus from right-click context menus.
GUI COMMANDS: COMPLETE RETHINK - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=25893