Apply hotkeys to the new context menu on Windows 11

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

Apply hotkeys to the new context menu on Windows 11

Post by lexikos » 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

Post by Helgef » 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

Post by thaihoa3189 » 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 2373 times

Me, dont have it:
image.png
image.png (126.31 KiB) Viewed 2373 times

lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

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

Post by lexikos » 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

Post by thaihoa3189 » 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 2266 times

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

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

It doesnt work, help me please. Im a fan of your ahk

lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

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

Post by lexikos » 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

Post by thaihoa3189 » 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 2162 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 2162 times

lexikos
Posts: 9494
Joined: 30 Sep 2013, 04:07
Contact:

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

Post by lexikos » 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

Post by thaihoa3189 » 08 Dec 2022, 05:12

@lexikos
thank u very much, again

Post Reply

Return to “Scripts and Functions (v2)”