Windows – Create a text file with a keyboard shortcut in Windows Explorer

autohotkeykeyboardkeyboard shortcutswindows 7windows-explorer

I'm using the solution from How to create a new text document (TXT) file by a Hotkey? since a few years, with Autohotkey, and it allows to create a new text file anywhere in the Windows explorer with a keyboard shortcut.

There is one drawback: when a file already has focus in the Explorer file list ("Details" view), it doesn't work, mainly because, when a file is selected, the Contextual menu doesn't show the "New > Text document".

Question: how to have a shortcut that creates a new text document, even if a file is currently selected in Details view of the Windows Explorer?

Best Answer

; ahk_group ExplorerGroup
GroupAdd, ExplorerGroup, ahk_class ExploreWClass
GroupAdd, ExplorerGroup, ahk_class CabinetWClass

; ahk_group DesktopGroup
GroupAdd, DesktopGroup, ahk_class Progman
GroupAdd, DesktopGroup, ahk_class WorkerW

; ahk_group ExplorerDesktopGroup
GroupAdd, ExplorerDesktopGroup, ahk_group ExplorerGroup
GroupAdd, ExplorerDesktopGroup, ahk_group DesktopGroup

            RETURN   ; === end of auto-execute section ===


#IfWinActive ahk_group ExplorerDesktopGroup

    F1::
    WinGet, active_id, ID, A
    InputBox, name, Create a New Text Document, Enter a name:,, 300, 120
    If !ErrorLevel
    {
        WinActivate, ahk_id %active_id%
        IfWinActive ahk_group DesktopGroup ; desktop
        {       
            FileAppend,, %A_Desktop%\%name%.txt
            Run, %A_Desktop%\%name%.txt
        }
        else
        IfWinActive ahk_group ExplorerGroup ; explorer
        {
            FolderPath := GetActiveExplorerPath()           
            FileAppend,, %FolderPath%\%name%.txt
            Run, %FolderPath%\%name%.txt
        }
    }
    return 

#IfWinActive

;* =========   FUNCTION     ===================

; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
GetActiveExplorerPath()
{
    explorerHwnd := WinActive("ahk_class CabinetWClass")
    if (explorerHwnd)
    {
        for window in ComObjCreate("Shell.Application").Windows
        {
            if (window.hwnd==explorerHwnd)
            {
                return window.Document.Folder.Self.Path
            }
        }
    }
}
Related Question