Page 1 of 1

How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 20:20
by vladi
How can I open the context menu of the window's title bar via AutoHotkey and then click a menu item by name?

Many thanks for any pointers!

Re: How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 20:31
by Exaskryz
There is WinMenuSelectItem, but if you emulate human input you would use Send {Alt} to open the menu and then Send _ where _ is whatever the underlined character is in the menu. If that underlined character is to another menu, then you can send the next letter in that submenu.

Re: How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 20:33
by jeeswg
You can invoke it by pressing alt+space.

Usually the commands are: Restore/Move/Size/Minimize/Maximize/Close, which all have AutoHotkey commands that you can use instead.

Does your menu have additional items like in Internet Explorer?

So you could send alt+space, down multiple times, enter. Or for example, 'alt+space, x' to maximise a window, i.e. you can use letters for items with accelerator keys.

Code: Select all

q::
SendInput, !{Space}{Down 4} ;to focus the item
;SendInput, !{Space}{Down 4}{Enter} ;to invoke the item
return
It may also be possible to invoke an alt+space menu item directly using PostMessage and WM_COMMAND, so it might help if you mention what the program is. Cheers.

[EDIT:] @Exaskryz: does WinMenuSelectItem work on the alt+space menu?

Re: How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 20:52
by Exaskryz
I don't believe WinMenuSelectItem works on that menu. It doesn't even appear to work on hidden menus it seems, at least those that hide the menu bar and require you to press Alt to show it (like in Firefox (fork)).

Re: How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 21:39
by vladi
Problem is that the window contains a remote Windows session on another machine and Alt-Space is captured inside the window.

Re: How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 22:01
by jeeswg
alt+left+down?

Code: Select all

q::
SendInput, {Alt}{Left}{Down}
return
Sorry I'm not too familiar with remote sessions. Would PostMessage work?

Could you or anyone else give me a summary of (or link re.) some of the problems involved with AutoHotkey and remote sessions?

Re: How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 22:22
by vladi
No, Alt-Left-Down gets captured inside the session as well.

I guess I really need to right-click the title bar of the window... Is there an easy way to do this with a given window?

Re: How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 22:35
by jeeswg
Something like this?

Code: Select all

q::
CoordMode, Mouse, Window
MouseMove, 100, 10
Send, {Click right}
return
Clicking works but keypresses don't?

You want to apply something to the main window, rather than the remote session? ... So you want to do things but without using the keyboard?

And so AHK is running on your PC rather than within the remote session?

Re: How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 22:48
by Exaskryz
Is AHK running inside your remote session or on your "parent" or "encapsulated" desktop? You should be able to make AHK do anything you want on the desktop it is running on, though I have little experience with AHK and remote desktops. You can do things like ControlClick which targets a given window, and you can pass a coordinate or a control to it to right click. Just define the target window as the name of your desktop-sharing/viewing program.

Re: How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 22:50
by vladi
Cool! That's it, jeeswg! Thanks a lot!

To your question whether keypresses not work: Well, they work, but are interpreted as keypresses for the remote window inside the session window. HTH.

My complete code now looks like this:

Code: Select all

WinActivate, Remote Session
CoordMode, Mouse, Window
MouseMove, 100, 20
Send, {Click right}
Send, {Down}{Down}{Down}{Down}{Down}{Down}{Enter}
return
It's pretty hacky, but it does the job! :-)

Re: How to open the context menu of the window's title bar

Posted: 17 Aug 2017, 22:53
by jeeswg
See that confuses me though, because wouldn't those downs be sent to the remote window. Or perhaps that changes when the menu gets the focus.

Acc and accDoDefaultAction can be useful for invoking menu items.
Acc library (MSAA) and AccViewer download links - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=26201

[EDIT:] Btw you may be able to press a letter to invoke a menu item (hold alt to see underlines), and you could do {Down 6}.

[EDIT:] Examples of some alt+space menu/regular menu items in Internet Explorer being invoked via PostMessage:

Code: Select all

PostMessage, 0x111, 41511,, TabWindowClass1, A ;WM_COMMAND := 0x111 ;Menu bar
PostMessage, 0x111, 41474,, TabWindowClass1, A ;WM_COMMAND := 0x111 ;Status bar
PostMessage, 0x111, 41484,, TabWindowClass1, A ;WM_COMMAND := 0x111 ;Lock the toolbars
PostMessage, 0x80F0, 42455,,, A ;WM_APP+240 := 0x80F0 ;Show tabs on a separate row
PostMessage, 0x111, 3460,, TabWindowClass1, A ;WM_COMMAND := 0x111 ;Caret browsing
Obtained via HotkeyP, mentioned here:
best utilities + best AutoHotkey scripts (+ useful tips) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28149

[EDIT:] Internet Explorer is a bit unusual, normally you can invoke sysmenu items by doing this:
PostMessage, 0x112, % vID,,, A ;WM_SYSCOMMAND := 0x112
And normal menu items by doing this:
PostMessage, 0x111, % vID,,, A ;WM_COMMAND := 0x111

This can be useful for getting WM_COMMAND/WM_SYSCOMMAND IDs from menus:
Get Info from Context Menu (x64/x32 compatible) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=6&t=31971

[EDIT:] A way to show the sysmenu without sending clicks, and how to invoke standard sysmenu items:

Code: Select all

;show menus
PostMessage, 0x112, 0xF100, 0x20,, A ;WM_SYSCOMMAND := 0x112 ;SC_KEYMENU := 0xF100 ;show sysmenu
PostMessage, 0x112, 0xF100, 0x46,, A ;WM_SYSCOMMAND := 0x112 ;SC_KEYMENU := 0xF100 ;show f menu (e.g. File)
PostMessage, 0x112, 0xF100, 0x45,, A ;WM_SYSCOMMAND := 0x112 ;SC_KEYMENU := 0xF100 ;show e menu (e.g. Edit)

;invoke sysmenu items
PostMessage, 0x112, 0xF120,,, A ;WM_SYSCOMMAND := 0x112 ;SC_RESTORE := 0xF120
PostMessage, 0x112, 0xF010,,, A ;WM_SYSCOMMAND := 0x112 ;SC_MOVE := 0xF010
PostMessage, 0x112, 0xF000,,, A ;WM_SYSCOMMAND := 0x112 ;SC_SIZE := 0xF000
PostMessage, 0x112, 0xF020,,, A ;WM_SYSCOMMAND := 0x112 ;SC_MINIMIZE := 0xF020
PostMessage, 0x112, 0xF030,,, A ;WM_SYSCOMMAND := 0x112 ;SC_MAXIMIZE := 0xF030
PostMessage, 0x112, 0xF060,,, A ;WM_SYSCOMMAND := 0x112 ;SC_CLOSE := 0xF060

Re: How to open the context menu of the window's title bar

Posted: 20 Jan 2022, 07:59
by dieterle
The following command from jeeswg works well without mouse interaction:

PostMessage, 0x112, 0xF100, 0x20,, A

Thanks jeeswg for your help!

----
Steps to reproduce:
1. Open windows command promt. Win+R: cmd
2. Login in a remote shell via ssh: e.g. ssh [email protected]
3. Execute AHK short cut (see script below): Ctrl+Shift+n
=> Result: windows command line window menu opens.


Verified AHK test script:

Code: Select all


#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^+n::
   PostMessage, 0x112, 0xF100, 0x20,, A ;WM_SYSCOMMAND := 0x112 ;SC_KEYMENU := 0xF100 ;show sysmenu
return