Apply hotkeys to the new context menu on Windows 11

Post your working scripts, libraries and tools.
lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Apply hotkeys to the new context menu on Windows 11

30 Apr 2022, 22:47

The goal is to implement the following hotkeys, within the "modern" context menu of files in File Explorer on Windows 11:
  • Press E to edit the selected file(s).
  • Press D to recycle or delete the selected file(s).
Image
Spoiler

In the current version (build 22000.613), most shortcut keys that would work in the classic menu (which can be shown by selecting "Show more options") do not work in the modern menu. I am used to right clicking and pressing "e" to edit a script, or "d" to delete a file, and do not particularly want to change these habits.

The solution has two parts:
  • Detect when a hotkey is pressed while a File Explorer context menu is open.
  • Find the selected item.

When a context menu is open, the Explorer window is active, not the context menu. The context menu can be detected with WinExist, so this combination of conditions will work:

Code: Select all

#HotIf WinActive("ahk_class CabinetWClass") && WinExist("PopupHost ahk_class Xaml_WindowedPopupClass")
Since pressing the Delete key already works, remapping that to the D key is simple: d::Delete or d::Send "{Delete}".

To invoke other actions, we need to know which file is selected, as shown below:

Code: Select all

e::{  ; Press E to edit the selected files.
    hwnd := WinExist("A")
    for window in ComObject("Shell.Application").Windows
        if window.HWND = hwnd
            for item in window.Document.SelectedItems
                item.InvokeVerb "edit"
                ; Run 'edit "' item.Path '"'
}
Spoiler

There is no requirement for the invoked verb to actually appear in the new Windows 11 menu, and of course, the script can do whatever it wants once it has the file path.


Altogether, with more reusable code:

Code: Select all

#Requires AutoHotkey v2.0-beta.3

#HotIf WinActive("ahk_class CabinetWClass")
    && WinExist("PopupHost ahk_class Xaml_WindowedPopupClass")

e::ForEachSelectedFile(item => item.InvokeVerb("edit"))
d::ForEachSelectedFile(item => item.InvokeVerb("delete"))
+d::Send("{Esc}"), WinWaitClose(,, 1) && Send("+{Delete}")

ForEachSelectedFile(action) {
    if WinExist("PopupHost ahk_class Xaml_WindowedPopupClass")
        Send "{Esc}"
    hwnd := WinExist("A")
    for window in ComObject("Shell.Application").Windows
        if window.HWND = hwnd
            for item in window.Document.SelectedItems
                action(item)
}
If we invoke "delete" with Shift held down, it will show a confirmation dialog, but then Explorer will steal the focus. Sending +{Del} appears to be more reliable. We could also skip the confirmation dialog by using FileDelete, but that's more dangerous.


Known issue: If you right click an item in the navigation pane/sidebar, the script will think you have right clicked an item in the main pane.
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Apply hotkeys to the new context menu on Windows 11

05 May 2022, 04:14

Thank you very much, it works great!

Cheers.
Spoiler
thaihoa3189
Posts: 35
Joined: 23 Mar 2022, 22:10

Re: Apply hotkeys to the new context menu on Windows 11

05 Dec 2022, 03:54

@lexikos
How can you have Edit Script item in the context menu? Please guide me, im struggling with click Show more options and then click Edit Script.

You:
image.png
image.png (38.51 KiB) Viewed 4195 times

Me, dont have it:
image.png
image.png (126.31 KiB) Viewed 4195 times
lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Apply hotkeys to the new context menu on Windows 11

05 Dec 2022, 21:39

@thaihoa3189 It's mentioned in the spoiler in my post. Set HKCR\AutoHotkeyScript\pintohome default value to the display text "Edit Script", and HKCR\AutoHotkeyScript\pintohome\command default value to the command for your editor (you can copy it from edit\command).
thaihoa3189
Posts: 35
Joined: 23 Mar 2022, 22:10

Re: Apply hotkeys to the new context menu on Windows 11

06 Dec 2022, 09:27

@lexikos
i tried what you told. I make new key pin to home and new key command inside of it. The value is set like this. But it doesnt work
This is my command key:
image.png
image.png (85 KiB) Viewed 4088 times

This is my pintohome key:
image.png
image.png (78.25 KiB) Viewed 4088 times

And this is my context menu after edit registry and restart my pc:
image.png
image.png (86.14 KiB) Viewed 4088 times

It doesnt work, help me please. Im a fan of your ahk
lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Apply hotkeys to the new context menu on Windows 11

06 Dec 2022, 19:43

Sorry, that should be HKCR\AutoHotkeyScript\Shell\pintohome and HKCR\AutoHotkeyScript\Shell\pintohome\Command (I missed the "Shell"). It is exactly the same as for the existing "edit" command but with "pintohome" instead of "edit".

You could just rename "edit" to "pintohome", but then the Edit command won't work correctly.
thaihoa3189
Posts: 35
Joined: 23 Mar 2022, 22:10

Re: Apply hotkeys to the new context menu on Windows 11

07 Dec 2022, 21:11

@lexikos
Thank you so much, finally i did it. This is it:
image.png
image.png (171.24 KiB) Viewed 3984 times

But how can i change the icon from PinToHome to the Star-Add To Favourite like you?. I like your icon more than this Pin icon
image.png
image.png (171.93 KiB) Viewed 3984 times
lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Apply hotkeys to the new context menu on Windows 11

07 Dec 2022, 21:27

The icon is based on the verb name. For build 22000.613, pintohome was a star.
thaihoa3189
Posts: 35
Joined: 23 Mar 2022, 22:10

Re: Apply hotkeys to the new context menu on Windows 11

08 Dec 2022, 05:12

@lexikos
thank u very much, again
lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Apply hotkeys to the new context menu on Windows 11

22 Mar 2024, 23:06

At some point Microsoft changed the window class of the context menu from Xaml_WindowedPopupClass to Microsoft.UI.Content.PopupWindowSiteBridge. Aside from that, the script still works.
lexikos
Posts: 9780
Joined: 30 Sep 2013, 04:07
Contact:

Re: Apply hotkeys to the new context menu on Windows 11

24 May 2024, 17:48

It appears that the popup window now has the title "Pop-upHost" instead of "PopupHost". :roll:

Class is still Microsoft.UI.Content.PopupWindowSiteBridge.
likethevegetable
Posts: 85
Joined: 05 May 2021, 08:54

Re: Apply hotkeys to the new context menu on Windows 11

04 Jun 2024, 21:10

Looking forward to giving this try. On a somewhat related note, have you tried to implement shortcuts for opening folders in new tabs, with say Shift+Click?

Gosh there's a lot of low-hanging fruit for Win 11 to be better.
User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: Apply hotkeys to the new context menu on Windows 11

11 Jun 2024, 10:58

This is great, I'm glad this is possible with relative ease, but still not the most obvious I guess.
For now, I have simply opted to disable the new context menu and stick with the classic one. :think:
https://www.lifewire.com/bring-back-old-context-menu-in-windows-11-8603528
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Return to “Scripts and Functions (v2)”

Who is online

Users browsing this forum: No registered users and 33 guests