Ooker
Ooker

Reputation: 3022

How to assign hotkeys to navigate folders only when Windows Explorer is active?

I want to using numbers to quickly navigate some frequent folders only when Windows Explorer is active; the use of those keys in other programs is unchanged. This thread provides the code matches my need, except it will use Run if no Explorer window is active, which I've stripped out in the code below:

#e::Run Explorer D:\Download
1::NavRun("D:\Download")
3::NavRun("E:\Setups")
4::NavRun("E:\Music")
8::NavRun("D:\")
9::NavRun("E:\")
0::NavRun("F:\")

; http://msdn.microsoft.com/en-us/library/bb774094
GetActiveExplorer() {
    static objShell := ComObjCreate("Shell.Application")
    WinHWND := WinActive("A")    ; Active window
    for Item in objShell.Windows
        if (Item.HWND = WinHWND)
            return Item        ; Return active window object
    return -1    ; No explorer windows match active window
}

NavRun(Path) {
    if (-1 != objIE := GetActiveExplorer())
        objIE.Navigate(Path)
}

However, I cannot use the numbers listed in the script anymore in any other programs. How can they be used again?

Upvotes: 0

Views: 187

Answers (1)

Relax
Relax

Reputation: 10543

Try this:

Hold the key pressed for longer than 0.3 seconds in explorer to navigate to a directory.

#UseHook

#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") ; explorer

    1::
    2::
    3::
    4::
    KeyWait, %A_ThisHotkey%, T0.3
    if (ErrorLevel)
        long_press := true
    else
        Send, %A_ThisHotkey%
    return

#If (long_press)

    1 Up::
    2 Up::
    3 Up::
    4 Up::
    long_press := false
    If (A_PriorHotKey = "1")
        NavRun("C:\")
    If (A_PriorHotKey = "2")
        NavRun("D:\")
    If (A_PriorHotKey = "3")
        NavRun("E:\")
    return

#If

; http://msdn.microsoft.com/en-us/library/bb774094
GetActiveExplorer() {
    static objShell := ComObjCreate("Shell.Application")
    WinHWND := WinActive("A")    ; Active window
    for Item in objShell.Windows
        if (Item.HWND = WinHWND)
            return Item        ; Return active window object
    return -1    ; No explorer windows match active window
}

NavRun(Path) {
    if (-1 != objIE := GetActiveExplorer())
        objIE.Navigate(Path)
    else
        Run, % Path
}

This way you can use the keys in a normal way if you want to type text in explorer.

Upvotes: 1

Related Questions