63

I haven't seen any shortcut for creating "A New Text Document" or any file. Is there any?

10 Answers10

61

Windows 7 and earlier: Alt+F, W, T works, regardless of whether the file or folder pane has focus.

Windows 8: This doesn't work in any more. Menu, w, t works as long as the file pane has focus.

Windows 10: Menu, w, t works as long as the file pane has focus and no file is selected. Press CONTROL-SPACE to clear the selected files (thanks @RogUE).

Windows 10 2nd Method: Alt+H, W, UP, UP, UP, ENTER. This works in even if a file is selected:

Ramhound
  • 44,080
18

I wrote a script based on the suggestions to use AutoHotkey. If you want to change the .txt file extension, this requires Hide extentions for known file types to be off.

; If Windows+N is pressed in Windows Explorer
#IfWinActive, ahk_class CabinetWClass
#n::
    Send {Alt} ; Menu
    Send f     ; > File
    Send w     ; > New
    Send t     ; select Text Document
    Send ^a    ; select all
    Exit

If you need any help with AutoHotkey, let me know. :)

pjpscriv
  • 321
iglvzx
  • 23,818
5

The following works under XP (I don't have Windows 7):

  • make sure the Files (not Folders) pane has focus (Tab or F6 to get there).
  • make sure no file is selected (press CTRL+Space to unselect one file if necessary).
  • bring up either the File menu with Alt+F or the context menu with the context menu key or Shift+F10.
  • Press W for New, and T for Text Document.
studiohack
  • 13,477
Hugh Allen
  • 10,120
4

It may be possible with a third party solution (haven't tested it, autohotkey is famous too), since it's not available in Windows 7's official keyboard shortcuts. You can create a new folder using Ctrl+Shift+N though.

GmonC
  • 2,402
1

I've been using this autohotkey script for ages.

;   New text file

#IfWinActive AHK_CLASS #32770
    Capslock & f11::
#IfWinActive AHK_CLASS CabinetWClass
    Capslock & f11::

    ; make it work even though a file is previously selected
    Send {PgUp} ; Force select the first file 
    Send ^{Space} ; Clear the selection

    Sleep 250 ; Remove delay if not required
    Send {AppsKey} ; Menu key
    Send w ; New
    Send t ; Text Document
    return
#IfWinActive
  • Works on file dialogs (save as / open file)
  • Works even though a file is selected
  • Works even though cursor is positioned off screen

Change "Capslock & f11" to your preferred shortcut.


To understand the syntax above, see below example,

;   Syntax - To have the same hotkey subroutine executed by more than one variant, the easiest way is to create a stack of identical hotkeys, each with a different #IfWin directive above it. For example:
    ;#IfWinActive ahk_class Notepad
    ;#z::
    ;#IfWinActive ahk_class WordPadClass
    ;#z::
    ;MsgBox You pressed Win+Z in either Notepad or WordPad.
    ;return

Hope you find this useful!

pjpscriv
  • 321
tinker
  • 350
1
#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.
#NoTrayIcon ; the process will not appear on windows taskbar on the right
#IfWinActive, ahk_class CabinetWClass
^r:: ;
KeyWait Control 
Sleep 40
Send {Alt} ;
Sleep 40
Send {Enter};
Sleep 40
Send {Enter}

You need to create .ahk file with this script. Also, you need to check if it creates .txt file. For this, open windows explorer, press Alt, then press enter, and look how many times do you have to press Up arrow key to it to open .txt file. After that, copy or delete those two lines of code

Send {Enter};
Sleep 40

Don't forget to add this to your startup apps folder so that you don't have to open this script every time. You can do that:

  • open the windows run app by pressing win+r
  • type: shell:startup
  • drag your .ahk script to this folder
pjpscriv
  • 321
molodchyk
  • 141
1

I found none of these scripts quite did the job for Windows 11 machines. This is what I came up with - a bit verbose, but it works. Depending on your right-click content menu, the script may need to be edited slightly. I needed different combinations for my different machines:

  • My desktop: W, W, Enter, T
  • My laptop: W, T

This script below sends the desktop combination.

#SingleInstance Force

; Item in list polyfill IsItemInList(item, list, del:=",") { haystack:=del if !IsObject(list) haystack.= list del else for k,v in list haystack.= v del
Return !!InStr(del haystack del, del item del) }

; Ctrl + Alt + N ^!n:: {

; Get active window
activeWin := WinActive("A")
activeClass := WinGetClass(activeWin)

; If active window isn't File Explorer - exit
explorerClasses := ["CabinetWClass", "ExploreWClass"]
if !IsItemInList(activeClass, explorerClasses) {
    return
}

; A small wait - script doesn't function without it
Sleep 100

; Move the mouse to the left side of the window
WinGetPos(&winX, &winY, &winWidth, &winHeight)    
MouseMove (winWidth - 35), (winHeight // 2)

; Left click to clear any selection
Send "{LButton}"

; Shift + right click - Old Context Menu
Send "+{RButton}"
Sleep 10

; Twice press W - "New"
Send "w"
Sleep 10
Send "w"
Sleep 10

Send "{Enter}"
Sleep 10

; Press T - "Text Document"
Send "t"

}

This script uses the hotkey combination Ctrl+Alt+N, but obviously you can set it to any combination you wish.

pjpscriv
  • 321
1

I just tried the following on my Swedish Windows 7 machine and it works good. No need to install third party.

  1. Open folder
  2. Press Alt to display menubar
  3. A (Swe. Arkiv, Eng. File)
  4. N (Swe. Nytt, Eng. New)
  5. T (Swe. Textdokument, Eng. Textdocument)

Short: Alt, A, N, T

pjpscriv
  • 321
0

Right click (leave right click mouse button) then press 'W' (leave W) and finally press 'T'.

Works with Windows 8.1.

-1

Update

  • Press #n to create a new file
  • Added ability: select the file when its created
  • More code on ComObject
  • Improvement on speed (but still takes like 1 sec)
  • This is NOT a ready-to-use ahk file
    -- try to understand it first
    & remove some of the trivial for-debug dependencies like TooltipX printErr
  • ahk v2
#Requires AutoHotkey v2.0

#Include "%A_ScriptDir%\Lib\utilUsp\ToolTipX.ahk"

#HotIf WinActive("ahk_class CabinetWClass") ; #HotIf hwnd_WinExplorer_currActive := WinActive("ahk_class CabinetWClass") ; @dk this way of assign...

; ## create new txt file ; ;M; explorerGetPath(hwnd := 0) { ; https://www.autohotkey.com/boards/viewtopic.php?p=387113#p387113 ; ;M; Static winTitle := 'ahk_class CabinetWClass' ; ;M; hWnd ? explorerHwnd := WinExist(winTitle ' ahk_id ' hwnd) ; ;M; : ((!explorerHwnd := WinActive(winTitle)) && explorerHwnd := WinExist(winTitle)) ; ;M; If explorerHwnd ; ;M; For window in ComObject('Shell.Application').Windows ; ;M; Try If window && window.hwnd && window.hwnd = explorerHwnd ; ;M; Return window.Document.Folder.Self.Path ; ;M; Return False ; ;M; } ; ;M; ; ;M; #n:: { ; ;M; hwnd_WinExplorer_currActive := WinActive("A") ; WinActive("A ahk_class CabinetWClass") ; @dk if this valid syntax ; ;M; if (hwnd_WinExplorer_currActive == 0 || WinGetClass("ahk_id " . hwnd_WinExplorer_currActive) != "CabinetWClass") { ; ;M; ToolTipX("this is not windows explorer") ; ;M; return ; ;M; } ; ;M; path_Cwd := explorerGetPath(hwnd_WinExplorer_currActive) ; ;M; ToolTipX(path_Cwd) ; ;M; path_NewTxtFile := path_Cwd . "\Z_NewTxtFile.txt" ; ;M; sn := 1 ; ;M; while (FileExist(path_NewTxtFile) != "") { ; ;M; sn += 1 ; ;M; path_NewTxtFile := path_Cwd . "\Z_NewTxtFile" . sn . ".txt" ; ;M; if (sn == 5) { ; ;M; ToolTipX("too many new files already exists :: " . sn) ; ;M; return ; ;M; } ; ;M; } ; ;M; ; ;M; FileAppend("", path_NewTxtFile) ; ;M; openFileLocation(path_NewTxtFile, hwnd_WinExplorer_currActive) ; ;M*; } #n:: { hwnd_WinExplorer_currActive := WinActive("A") ; WinActive("A ahk_class CabinetWClass") ; @dk if this valid syntax if (hwnd_WinExplorer_currActive == 0 || WinGetClass("ahk_id " . hwnd_WinExplorer_currActive) != "CabinetWClass") { ToolTipX("this is not windows explorer") return }

comWin := hwnd2com(hwnd_WinExplorer_currActive) if (comWin == "") { printErr("comWin convert fail. hwnd :: " . hwnd_WinExplorer_currActive) return }

; Get Active Explorer Path - AutoHotkey Community ; https://www.autohotkey.com/boards/viewtopic.php?t=116402 path_Cwd := comWin.Document.Folder.Self.Path ToolTipX(path_Cwd)

static comFs := ComObject("Scripting.FileSystemObject") ; @not_sure the use of static sn := 0 while (true) { ; FileExist(path_NewTxtFile) != "" sn += 1 path_NewTxtFile := path_Cwd . "\Z_NewTxtFile" . sn . ".txt" if (sn == 5) { ToolTipX("too many attempts / new files already exists :: " . sn) return } try { ; CreateTextFile Method | Microsoft Learn ; https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/windows-scripting/5t9b5c0c(v=vs.84) comFs.CreateTextFile(path_NewTxtFile, false) ; no overwrite (-- the docs lies... default is true) break } catch Error as e { if (e.Message != "(0x800A003A) ") { ; dk why has space ; printErr(JSON.stringify(e)) throw e } else { ; @do_nothing go to next loop ToolTipX("file already exists :: " . path_NewTxtFile) } } }

; comWin.Navigate(path_Cwd) ; Shell.Application.Windows[0].Document.SelectItem(path_NewTxtFile, 0x1D) comWin.Document.SelectItem(path_NewTxtFile, 0x1D) }

#HotIf

(//...)

; ########

; Opening a Folder Without the Creation of a Separate Window - AutoHotkey Community ; https://www.autohotkey.com/boards/viewtopic.php?style=1&t=96250 ; https://autohotkey.com/board/topic/102127-navigating-explorer-directories/#entry634365

; http://msdn.microsoft.com/en-us/library/bb774094 hwnd2com(hwnd) { static comShell := ComObject("Shell.Application") for comWin in comShell.Windows { try { hwnd_curr := comWin.HWND } catch Error as e { printErr(e.Message) return } if (hwnd_curr = hwnd) { return comWin } } return "" ; fail }


Old

Method 1 [FileAppend & GetActiveExplorerPath()]

@logic::

@reference::

@source_code:: (basically same as the code in reference above)

; ######################################################

; ~~// https://superuser.com/questions/1091268/how-to-create-a-new-text-document-txt-file-by-a-hotkey ; ~~// https://superuser.com/questions/133175/is-there-a-shortcut-for-creating-a-new-file ; https://superuser.com/questions/1368969/create-a-text-file-with-a-keyboard-shortcut-in-windows-explorer

#If WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass") || WinActive("ahk_class Progman") || WinActive("ahk_class WorkerW")

#n:: WinGet, active_id, ID, A name := "NewTxtFile" ; WinActivate, ahk_id %active_id% if (WinActive("ahk_class CabinetWClass") || WinActive("ahk_class ExploreWClass")) { ; explorer FolderPath := getWindowsExplorerPath() FileAppend,, %FolderPath%%name%.txt ; Run, %FolderPath%%name%.txt } else if (WinActive("ahk_class Progman") || WinActive("ahk_class WorkerW")) { ; desktop FileAppend,, %A_Desktop%%name%.txt ; Run, %A_Desktop%%name%.txt } return

; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925

getWindowsExplorerPath() { explorerHwnd := WinActive("ahk_class CabinetWClass") if (explorerHwnd) { ; i := 1 for window in ComObjCreate("Shell.Application").Windows { ; ToolTip(++i) try { if (window.hwnd == explorerHwnd) { return window.Document.Folder.Self.Path } } } } }

#If

@minor-comment::

  • this method may be recommended over the others, since window navigation panel & popup context menu are unreliable due to their: slowness (depends) / wait for popup / diff windows version hotkey are different / if your popup context menu has the hotkey conflict with others

  • (aga) this method may (or may not) be slower than others

  • FileAppend does what it means -- so if you already have one file with the name NewTxtFile.txt, it appends to that file ...

    tweak the code if you need

  • this doesnt focus to the file for editing the file name

Method 2 [window navigation panel]

(another answer above) https://superuser.com/a/194593/997622

Method 3 [popup context menu]

(as many other answers talked) SendInput, {AppsKey}wt

Nor.Z
  • 143