AutoHotKey function to perform File-Rename without relying on sending F2 key code - autohotkey

I want define a custom hotkey to rename a selected file in File Explorer. So my new hotkey should behave exactly like the F2 key does by default. That is, when I press the hotkeys, the file name should be editable, allowing me to type a new name. However, I can't use the F2 key to cause windows to do this.
The reason is that I'm using the default hotkeys for something else. I am often running an application (unrelated to AutoHotKey) where the buttons in the UI are triggered using keyhooking for all of the F keys. The only suggestions that I can find on this are to have my custom hotkey use 'send' to raise the default key codes that would be associated with the action. This won't work, because I am using those hotkeys for something else. What I need is a solution that causes a file to be renameable without sending the F2 keycode.
^+!R::
Send {F2} ;This won't work for me
return

Actually the original hotkey can be used, just add $ before the hotkey that you don't want to be fired by the send command. Also using app-specific hotkeys a good idea to minimize possible conflicts.
Try this:
#If winActive("ahk_exe Explorer.EXE")
^+!R::
send {F2}
return
$F2::
send {down}
return
In case you have an application which scans the F2 key globally and unconditionally, and you cannot redefine it, there is not much you can do from within AHK. So ideally you should get rid of that application, and use e.g. AHK for same functionality, or find some workaround.
In this particular case the easiest workaround is alternative way to rename the file:
^+!R::
send {AppsKey}
sleep 100
send {m}
return

Related

AutoHotkey send without replacement

I'm using AutoHotkey. Is there a way of using "Send" without getting the previously wirtten Hotstring replaced?
So if I want to use the Hotkey System it always overrides the System with the text I set after "Send". But how can I achive that it is just appending my Send text to the hotstring?
Send, Hotstring Rest of Things You Want to Say
I think you may be mistaken in what you're trying to say or how you're using Send, as it doesn't replace previously written hotstrings. If you post an example of what you're saying, it will be easier to help. Also, what do you mean when you say "Hotkey System"?
Helpful info from the online documentation, Hotkeys & Hotstrings.
EDIT (per your comment below):
I understand what you're saying now. This is the default way that hotstrings work and has nothing to do with using the Send command. If you want to keep the activating text in a hotstring, you need to use the B0 option. Something like this:
:*B0:System::.out.println()
Note that the * makes it so you don't need to type a period after "system". If you want it to work that way instead, remove the asterisk.

How to Release AutoHotkey Control in a Logic Statement?

Goal:
Use the hotkey 'C' to mimmic 'New Email Window' in Outlook, similar to Gmail.
Attempt:
c::
SetTitleMatchMode, 2
If Not WinActive("Message")
Send, ^n
return
Problem:
This script does work, but then inside that 'New Email' Window the 'c' char is locked out, and I can't use it while typing.
Tried Solutions:
I tried adding and empty an 'else {}' but that does not seem to work. Thoughts?
Environment:
Windows 10 / Outlook 2016+
This is probably what you want,
SetTitleMatchMode,2
#if WinActive("Microsoft Outlook")
{
c::
Send, ^n
return
}
This way, it will allow to type the char 'c' anywhere, including the new email window. But, it will trigger if you are in the main Outlook window, opening a new email window for you.
This uses context-sensitive title matching to map C to CTRL+Nwhen "Microsoft Outlook" is in the window title:
SetTitleMatchMode 2 ; All #If statements match anywhere in title
#IfWinActive Microsoft Outlook
c::^n
#IfWinActiv
This worked. I think in my case it needed to know specifically what to do if the window was not active, and in my case it was to fire a regular 'c', I would have thought that logic was built in by default recognize.
c::
SetTitleMatchMode 2
IfWinActive, Outlook
Send, ^n
Else
send, c
return
$c::
If (WinActive("Microsoft Outlook") { ;may/may not be "Microsoft Outlook' use window spy to find out more
send ^N ; if you got that to work then dont mess with this part
} else {
send {c}
}
Return
Basically this checks if Outlook is active then if so sends ^N and if not it will send the character C.
However this isnt the best idea: creating single key hotkeys that are used for typing...
Better idea: you change the hotkey ( the part before "::` ) to something like $^!c
( the $ is so that any other hotkey sending c wouldn't activate this one )
Another aproach would be:
$~c::
If (WinActive(ahk_class "outlook.exe") { ; or something along the lines of that...
sleep 250
send ^N
}
Return
For the most part this does the same thing except it retains the functionality of the c key better, however it may cause issues with typing in outlook so once again,
Please consider either a non-typing single key hotkey such as Rcontrol for example. The ~ in the hotkey means don't revoke original key functionality, The sleep is added to ensure that when you are using the hotkey that the retained c character isn't added to your new email.
Hope this helped ( I am fairly certain that this works ) I don't use outlook so I don't know how well Outlook will respond to this type of thing, and haven't tested this out but I do know my AHK basics and did do function syntax double checking so good luck to you, read my cmments in the code because they are essential to these code snippets functionality.
you may want to add && If (Not WinActive("Message")) ;or whatwver the message window is called if the two snippets didn't work for your needs add this because your new message window may also be called outlook so this will interfere with the typing.

(AHK) Creating variable hotkeys that gets the key names from a 2 char file name of a script

I'm trying to make something for our employees to use so that they dont have to alter the script itself to define hotkeys. This may only work for hotkeys which can be defined by a single character, but that's fine, as there are so many combinations that can be made with them, and they can be very easy to remember. The script would look only at 2 character AHK files (or 6 if you must include the extension) in the working directory. And the variables it would search for could be defined with RegEx so for the first hotkey, it would look like ^. and then second would look like .(?=.) Once a match is found, it would simply launch that matched file. Has something like this been done before? It seems so simple but I can't seem to find anything on it.
Edit: Elliot brought this to my attention: http://autohotkey.com/board/topic/60630-easy-editmanage-hotkeyshotstrings-plugin-ahk-l/
It's a neat script manager, and very useful, but it's not what I'm looking for.
I dont not want an additional interface. I want to be able to change the hotkeys by using the filename.
Based on the answer of Forvin. Added the execution of the corresponding ahk script.
#Persistent
SetTimer, FindNewHotkeys, 2000
FindNewHotkeys:
Loop, %A_ScriptDir%\*
{
RegExMatch(A_LoopFileName, "^(.)(.).ahk$", hk)
If (hk)
{
Hotkey, ~%hk1% & ~%hk2%, HotkeyLabel
}
}
Return
HotkeyLabel:
RegExMatch(A_ThisHotkey, "~(.) & ~(.)", hk)
run, %hk1%%hk2%.ahk
Return
#Persistent
SetTimer, FindNewHotkeys, 2000
FindNewHotkeys:
Loop, %A_ScriptDir%\*
{
RegExMatch(A_LoopFileName, "^(.)(.).ahk$", hk)
If (hk)
Hotkey, ~%hk1% & ~%hk2%, HotkeyLabel
}
Return
HotkeyLabel:
MsgBox, A hotkey has been pressed!
Return

AutoHotkey SendInput Problems

[Key Mappings of New Media Remote]
http://i.stack.imgur.com/ivMSK.png
Using AutoHotkey I want to interrupt Keyboard Keys sent like AppsKey and Browser_Home and send another key instead. Unfortanately the best I can manage is to send a key as well as the key function.
F3::SendInput {a}
Browser_Home::SendInput {a}
Browser_Home::SendInput a
Browser_Home:: a
The first line 'F3 to a' works as intended; the F3 Key outputs the letter a.
The 2nd and 3rd line Browser_Home only launches Browser to Home.
The 4rd line launches the Browser_Home as well as outputting the letter a.
Anyone know what I'm doing wrong / know how to fix my script to output a without launching browser to home?
According to documentation under Hotkey:
$ is usually only necessary if the script uses the Send command to send the keys that comprise the hotkey itself, which might otherwise
cause it to trigger itself. The $ prefix forces the keyboard hook to
be used to implement this hotkey, which as a side-effect prevents the
Send command from triggering it. The $ prefix is equivalent to having
specified #UseHook somewhere above the definition of this hotkey
Try this and report back: $Browser_Home::SendInput {a}

Send command, isn't something wrong with AutoHotkey?

So I have this game, called AirMech. It doesn't recognize mouse buttons as controls (yet) so I tried to use AutoHotkey to circumvent it until it's implemented.
#IfWinActive, AirMech
XButton1::Send c
Didn't work. So I tried SendGame, SendPlay and everything else, didn't work either. I googled it, and found out that some games don't recognize any Send commands at all.
Before giving up, I just tried a simple mapping:
#IfWinActive, AirMech
XButton1::c
It actually worked.
Is it expected than no Send command works, but the latter does? What if I wanted to trigger other actions ('c' plus a MsgBox, for instance)?
AutoHotkey has the ability to send keystrokes in a variety of different ways (SendRaw / SendInput / SendPlay / SendEvent). I'm not quite sure what approach the simple key::key mapping uses, but it must be one of them. My guess is that one of SendRaw, SendInput, SendPlay, or SendEvent will work the same as key::key.
Also #IfWinActive sometimes doesn't work exactly the way you expect, especially with fullscreen games. So I usually test my AHK scripts without the #IfWinActive to make sure they're working correctly. Once it's working, I introduce the conditional.
UPDATE
From http://www.autohotkey.com/docs/misc/Remap.htm:
When a script is launched, each remapping is translated into a pair of
hotkeys. For example, a script containing a::b actually contains the
following two hotkeys instead:
*a::
SetKeyDelay -1 ; If the destination key is a mouse button, SetMouseDelay is used instead.
Send {Blind}{b DownTemp} ; DownTemp is like Down except that other Send commands in the script won't assume "b" should stay down during their Send.
return
*a up::
SetKeyDelay -1 ; See note below for why press-duration is not specified with either of these SetKeyDelays. If the destination key is a mouse button, SetMouseDelay is used instead.
Send {Blind}{b Up}
return
My notes:
I suspect the reason a::b is working but a::Send b is not is because of how a::b breaks button down and button up handlers into two separate mappings. The game's gameloop probably polls the gameplay keys for "keydown" state, which would not be maintained consistently if AHK is synthesizing repeats. Remapping a_down->b_down and a_up->b_up probably makes AHK emulate more accurately the act of holding the key down, which may matter for programs which test for key state in particular ways (GetAsyncKeyState?).
The asterisk in the mapping means "Fire the hotkey even if extra modifiers are being held down."