Is it possible for AutoHotkey to modify the context menu within an application? Specifically, I am wanting to add an "AHK Macros" menu option to the context menu in several different applications such as Microsoft Word, Lotus Notes, the various web browsers, etcetera that contains a list of scripts specific to each application.
I know I can work around the problem by making a stand-alone menu (IE: Right Click for standard context menu, Alt+Right Click for my menu), but I am wanting to share this with some computer neophytes and do not want to confuse them by adding non-standard interactions.
Possible to modify context menus for applications?
-
- Posts: 1259
- Joined: 16 Apr 2015, 09:41
Re: Possible to modify context menus for applications?
Try this... Run the script, and then right-click inside a Window that normally has a context-menu. There will be a new item.
https://autohotkey.com/boards/viewtopic ... 45#p170145
https://autohotkey.com/boards/viewtopic ... 45#p170145
Code: Select all
#Persistent
SetWinEventHook(0x6, 0x6, 0, RegisterCallback("HookProc", "F"), 0, 0, 0) ; EVENT_SYSTEM_MENUPOPUPSTART := 0x6
HookProc(hWinEventHook, event, hwnd, idObject, idChild, dwEventThread, dwmsEventTime)
{
static MN_GETHMENU := 0x1E1, info := {}, funcObj := Func("OnMenuClick").Bind(info)
if !info.pos
Hotkey, ~LButton, % funcObj, On
SendMessage, MN_GETHMENU,,,, ahk_id %hwnd%
hMenu := ErrorLevel
itemCount := DllCall("GetMenuItemCount", Ptr, hMenu)
info.pos := itemCount, info.hWnd := hwnd, info.hMenu := hMenu
DllCall("AppendMenu", Ptr, hMenu, UInt, 0, Ptr, 0, Str, "My Custom Item")
}
OnMenuClick(info) {
CoordMode, Mouse
MouseGetPos, X, Y, hWnd
if (hWnd != info.hWnd)
Return
POINT := X | Y << 32
VarSetCapacity(RECT, 16, 0)
DllCall("GetMenuItemRect", Ptr, 0, Ptr, info.hMenu, UInt, info.pos, Ptr, &RECT)
if DllCall("PtInRect", Ptr, &RECT, Int64, POINT)
MsgBox, My item was clicked!
}
SetWinEventHook(eventMin, eventMax, hmodWinEventProc, lpfnWinEventProc, idProcess, idThread, dwFlags)
{
return DllCall( "SetWinEventHook", UInt, eventMin, UInt, eventMax
, Ptr, hmodWinEventProc, Ptr, lpfnWinEventProc
, UInt, idProcess, UInt, idThread
, UInt, dwFlags, Ptr )
}
- JoeWinograd
- Posts: 2214
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: Possible to modify context menus for applications?
Hi TheDewd,
That is very cool! I just tested it in W10 with AHK 1.1.28.02 U32. It works in KEDIT, Notepad, Total Commander, and all tray icons that I tried it with, but does not work in Chrome, Excel, Power PDF Advanced, and Word. Any thoughts on why it works in some programs and not others?
Also, every time I right-click on an AHK tray icon (not other tray icons), it adds another "My Custom Item" entry...looks like this now on my AHK hotkeys/hotstrings start-up script:
Any thoughts on that? Thanks, Joe
That is very cool! I just tested it in W10 with AHK 1.1.28.02 U32. It works in KEDIT, Notepad, Total Commander, and all tray icons that I tried it with, but does not work in Chrome, Excel, Power PDF Advanced, and Word. Any thoughts on why it works in some programs and not others?
Also, every time I right-click on an AHK tray icon (not other tray icons), it adds another "My Custom Item" entry...looks like this now on my AHK hotkeys/hotstrings start-up script:
Any thoughts on that? Thanks, Joe
-
- Posts: 1259
- Joined: 16 Apr 2015, 09:41
Re: Possible to modify context menus for applications?
Thank you for that script. It works perfectly in some applications like Windows File Explorer and Notepad.TheDewd wrote:Try this... Run the script, and then right-click inside a Window that normally has a context-menu. There will be a new item.
Unfortunately, it does not work in other applications like Microsoft Word or Google Chrome. I suspect those applications do not use the traditional Windows UI elements for their context menus, so may not be something AHK can intercept & modify.
Who is online
Users browsing this forum: Google [Bot], peter_ahk and 335 guests