rename a file by selecting enter instead of F2 Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Jelleeey
Posts: 33
Joined: 18 May 2021, 08:21

rename a file by selecting enter instead of F2

Post by Jelleeey » 22 May 2021, 05:37

Hey guys,

I am a mac user trying to make the transition to windows. I am used to being able to press the enter key to rename a selected file. However, in windows this just opens the file. Is it possible to have a script that makes the enter key rename the file (maybe by sending F2) but only when a file is selected?

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: rename a file by selecting enter instead of F2

Post by mikeyww » 22 May 2021, 06:09

Code: Select all

#IfWinActive ahk_class CabinetWClass
$Enter::
ControlGetFocus, fc, A
Send % Instr(fc, "DirectUI") ? "{F2}" : "{Enter}"
Return
#IfWinActive

Jelleeey
Posts: 33
Joined: 18 May 2021, 08:21

Re: rename a file by selecting enter instead of F2

Post by Jelleeey » 22 May 2021, 07:44

Thank you very much for that! I tried it but it doesnt work as it should:

On desktop:
It doesnt trigger the F2 button when I select a file and press enter.

In file eplorer:
The F2 got triggered in explorer, which is good. But actually, I have F1 and F2 mapped to brightness controls for the display so it will toggle that instead. Is there a way to trigger not the hotkey F2 but the underlaying command that F2 triggers?

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: rename a file by selecting enter instead of F2

Post by mikeyww » 22 May 2021, 08:53

Code: Select all

$F2::MsgBox, Test ; Use the "$" hotkey modifier

#IfWinActive ahk_exe explorer.exe
$Enter::
ControlGetFocus, fc
Send, % fc ~= "SysListView321|DirectUI" ? "{F2}" : "{Enter}"
Return
#IfWinActive

Jelleeey
Posts: 33
Joined: 18 May 2021, 08:21

Re: rename a file by selecting enter instead of F2

Post by Jelleeey » 23 May 2021, 09:25

I got it to work with the following code:

#IfWinActive ahk_exe explorer.exe
Enter::
send, {AppsKey}
Send m
return


Basically it simulates right clicking and pressing the 'rename' button and this works pretty good. However, now I stumble upon a little inconvenience, because after this I want to press the enter again to confirm the name change. Now of course it sends the same right click + m command whenever I press enter. Is there a way to make an exception of this code where enter behaves like a normal enter command whenever 'in the renaming process'? Is there a way to indicate / replace the code in the '#IfWinActive ahk_exe explorer.exe' line to make this exception?

Greets Jelle

Jelleeey
Posts: 33
Joined: 18 May 2021, 08:21

Re: rename a file by selecting enter instead of F2

Post by Jelleeey » 23 May 2021, 09:25

mikeyww wrote:
22 May 2021, 08:53

Code: Select all

$F2::MsgBox, Test ; Use the "$" hotkey modifier

#IfWinActive ahk_exe explorer.exe
$Enter::
ControlGetFocus, fc
Send, % fc ~= "SysListView321|DirectUI" ? "{F2}" : "{Enter}"
Return
#IfWinActive
Thanks a lot for this idea. I unfortunately cannot use the F2 button as it is already used by something else

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: rename a file by selecting enter instead of F2

Post by mikeyww » 23 May 2021, 10:04

Do you mean that your script uses F2, or some other program uses F2?

Jelleeey
Posts: 33
Joined: 18 May 2021, 08:21

Re: rename a file by selecting enter instead of F2

Post by Jelleeey » 23 May 2021, 10:06

mikeyww wrote:
23 May 2021, 10:04
Do you mean that your script uses F2, or some other program uses F2?
another program uses f2 (adjust brightness of display)

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: rename a file by selecting enter instead of F2  Topic is solved

Post by mikeyww » 23 May 2021, 10:19

Here is one idea.

Code: Select all

#IfWinActive ahk_exe explorer.exe
Enter::
If !file := Explorer_GetSelection().1
 Return
SplitPath, file, oldfn, dir
InputBox, newfn, Rename file, %oldfn%,, 400, 125
If (ErrorLevel || newfn = "")
 Return
If Instr(FileExist(file), "D")
 FileMoveDir, %file%, %dir%\%newfn%
Else FileMove, %file%, %dir%\%newfn%
If ErrorLevel
 MsgBox, 48, Error, An error occurred when renaming the file.
Else MsgBox, 64, Success, Done!, 1
Return
#IfWinActive

Explorer_GetSelection() {
 ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 WinGetClass, winClass, % "ahk_id" hWnd := WinExist("A")
 If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass")
  Return
 shellWindows := ComObjCreate("Shell.Application").Windows, sel := []
 If !(winClass ~= "Progman|WorkerW") {
  For window in shellWindows
   If (window.HWND && hWnd = window.HWND && shellFolderView := window.Document)
    Break
 } Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
 For item in shellFolderView.SelectedItems
  sel.Push(item.Path)
 Return sel
}

Jelleeey
Posts: 33
Joined: 18 May 2021, 08:21

Re: rename a file by selecting enter instead of F2

Post by Jelleeey » 23 May 2021, 10:31

mikeyww wrote:
23 May 2021, 10:19
Here is one idea.

Code: Select all

#IfWinActive ahk_exe explorer.exe
Enter::
If !file := Explorer_GetSelection().1
 Return
SplitPath, file, oldfn, dir
InputBox, newfn, Rename file, %oldfn%,, 400, 125
If (ErrorLevel || newfn = "")
 Return
If Instr(FileExist(file), "D")
 FileMoveDir, %file%, %dir%\%newfn%
Else FileMove, %file%, %dir%\%newfn%
If ErrorLevel
 MsgBox, 48, Error, An error occurred when renaming the file.
Else MsgBox, 64, Success, Done!, 1
Return
#IfWinActive

Explorer_GetSelection() {
 ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
 WinGetClass, winClass, % "ahk_id" hWnd := WinExist("A")
 If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass")
  Return
 shellWindows := ComObjCreate("Shell.Application").Windows, sel := []
 If !(winClass ~= "Progman|WorkerW") {
  For window in shellWindows
   If (window.HWND && hWnd = window.HWND && shellFolderView := window.Document)
    Break
 } Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
 For item in shellFolderView.SelectedItems
  sel.Push(item.Path)
 Return sel
}
Thanks!! But I get this error message. Any ideas?
Attachments
2021-05-23 17_25_16-MacKeyboard.ahk.png
2021-05-23 17_25_16-MacKeyboard.ahk.png (56.5 KiB) Viewed 1141 times

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: rename a file by selecting enter instead of F2

Post by mikeyww » 23 May 2021, 10:33

Yep. You have to include the function in the script. Scroll down in the posted code.

Jelleeey
Posts: 33
Joined: 18 May 2021, 08:21

Re: rename a file by selecting enter instead of F2

Post by Jelleeey » 23 May 2021, 10:47

cool, it works!

Not exactly as i had in mind but cool that this works :) Thanks!

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: rename a file by selecting enter instead of F2

Post by feeko » 10 May 2022, 10:46

Thank you for this @mikeyww

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: rename a file by selecting enter instead of F2

Post by feeko » 24 May 2022, 11:47

Can I add code to this script so that when it triggers the rename function it also pastes text too? This is the original code and the second is the code I tried to add to it. I'm trying to add the content of the clipboard + a default text to the file name.

Code: Select all

#IfWinActive ahk_exe explorer.exe


!d::
ControlGetFocus, fc
Send, % fc ~= "SysListView321|DirectUI" ? "{F2}" : "{Enter}"
Return
#IfWinActive

Code: Select all

#IfWinActive ahk_exe explorer.exe


!d::
ControlGetFocus, fc
Send, % fc ~= "SysListView321|DirectUI" ? "{F2}" : "{Enter}",  clipboard " - Test"
Return
#IfWinActive

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: rename a file by selecting enter instead of F2

Post by feeko » 24 May 2022, 14:49

I got this to work.

Code: Select all

#IfWinActive ahk_exe explorer.exe
!d::
ControlGetFocus, fc
Send, % fc ~= "SysListView321|DirectUI" ? "{F2}" : 
Send, % clipboard " - Test"
Return
#IfWinActive

Post Reply

Return to “Ask for Help (v1)”