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

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

Related

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

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

How to manually trigger Autohotkey hotstrings?

In my main Autohotkey script I have several hundred hotstrings like these:
::fe::for example
::f::and
::fi::for instance
::fo::fortunate
::foy::fortunately
::glo::global
::gloy::globally
::ha::have
::hv::however
Fairly often it would be convenient to trigger a hotstring manually (e.g. by pressing ALT-9) rather than pressing and end character. Is there a way to do this? I haven't found anything in my Googling, so maybe there isn't. But it would be useful.
I've read the hotstrings options e.g. :*: but this isn't the same - I want normal hotstring operation, but also the option to manually force them to trigger as needed.
Updated:
So what you are in fact looking for is using ALT+9 as an end character. I'm not sure but you can probably not use key-combinations for that (see hotstring doc). I cannot think of a really clever way of doing that right now. You might try something like
::fe::
Transform, CtrlC, Chr, 3 ; comes from ahk input documentation, I never really understood how this is supposed to work but I guess there is a code for alt 9 as well somehow
input, key, L1 I M ; wait for the next 1 key
if(key==CtrlC)
sendraw forExample
return
Old answer:
You have to outsource the hotstring body:
::fe::gosub forExample
forExample:
send for example
return
, then you can define a hotkey somewhere:
!9::gosub forExample
If you want to be cool, use functions instead of subroutines.
Note:
::fe::something
is just a short form for
::fe::
send something
return
::fe::for example
::f::and
::fi::for instance
::fo::fortunate
::foy::fortunately
::glo::global
::gloy::globally
::ha::have
::hv::however
!8:: trigger_hotstring("fi")
!9:: trigger_hotstring("ha")
trigger_hotstring(hotstring){
Loop, Read, %A_ScriptFullPath%
{
If InStr(A_LoopReadLine, "::"hotstring "::")
{
SendInput, % StrSplit(A_LoopReadLine,"::"hotstring "::").2
break
}
}
}
If you use AutoHotkey v1.1.06+ you can use #InputLevel
::fe::for example
::f::and
; and so on
#InputLevel, 1 ; sending space will trigger hotstrings above this line
!F9::Send {space}
Edit:
I now see you want to omit the end char which needs a bit of extra work
One way would be to duplicate the hotstrings with additional options:
::fe::for example
:*O:fe_::for example ; duplicate the hotstrings like so
::f::and
::fi::for instance
::fo::fortunate
; and so on
#InputLevel, 1
!9::Send _
Another way would be to remove the endchar
::fe::for example
::f::and
::fi::for instance
::fo::fortunate
; and so on
#InputLevel, 1
!9::
Send {space}
Sleep 100 ; give it time to expand the hotstring, experiment with timing
Send {bs} ; now remove trailing space
Return
I generally use TAB as a trigger for all my hotstrings. like
:*:#pm ::mail1#protonmail.com
:*:#G ::mail2#gmail.com
:*:Btw ::By the way,{Left}
Note that the space that you see here is a tab and not a space. you can do this instead of doing Alt+9 to trigger your macro hotstring.
you can even use more than one Tab so you can be sure that you only trigger it when you really intend to.

advance hotstrings in autohotkey

I found this working autohotkey (ahk) code from the website https://jacksautohotkeyblog.wordpress.com/2015/10/22/how-to-turn-autohotkey-hotstring-autocorrect-pop-up-menus-into-a-function-part-5-beginning-hotstrings/ .
::agin::
TextMenu("again,a gin,aging")
Return
::duh::
TextMenu("what,huh,you're joking")
Return
TextMenu(TextOptions)
{
StringSplit, MenuItems, TextOptions , `,
Loop %MenuItems0%
{
Item := MenuItems%A_Index%
Menu, MyMenu, add, %Item%, MenuAction
}
Menu, MyMenu, Show
Menu, MyMenu, DeleteAll ;Moved from MenuAction:
}
MenuAction:
SendInput %A_ThisMenuItem%{Raw}%A_EndChar%
Return
This is a hotstring script with menu. For example when I type agin I get a many with three options (again,a gin,aging) to choose. Now I want to write it something like this:
agin=again,a gin,aging
duh=what,huh,you're joking
qwe=qwe,qww,ere
Because I have a lot of hotstrings.
As time has passed, in the most recent version of AHK it became possible to do!
Use option x to allow expressions in the oneliner.
Either as :x:...:: OR as a group option above all lines: #Hotstring x
:x:agin::TextMenu("again,a gin,aging")
As nice as it would be, unlike AutoHotkey Hotkeys, I don't believe you can put anything other than replacement text on the same line of an AutoHotkey Hotstring. In other words,
::agin::TextMenu("again,a gin,aging")
would not work while,
::agin::
TextMenu("again,a gin,aging")
Return
does. It is a few more characters long, but not overwhelming.

How to implement something like a select-case-esac structure in AutoHotKey

I am not sure how to start this one. I went through the help files of AHK with no result.
I need to establish a construct like this (this is not in either UNIX shell or in AHK scripting language. Just shows the idea I am after)
while true
do
gosub screenscrape
; here, all text on page is on clipboard
; variable "input" is a predefined portion of the clipboard
case $input in
string-1)
gosub subroutione1;;
string-2)
gosub subroutine2;;
...
*)
echo "not found"
sleep 1minute
done
to make the things more complex, the parts noted as string-1, through string-n are also variables, read in from few different files, depending on time of the day or by trigger from an event.
can someone point me to the right direction for this ?
Autohotkey does not have any case statements. What everyone does is to use if and else statements. (I know how it feels. I like case, too)
A few tips for you:
You can't have that $ or a - in your variable name in autohotkey
...but you CAN have an underscore.
You don't need to terminate your lines with a ;.
Here is a translation into autohotkey for you.
gosub screenscrape
; here, all text on page is on clipboard
; variable "input" is a predefined portion of the clipboard
if(string1){ ;if the variable is not blank it will evaluate to `true`
gosub subroutine1
}else if(string2){
gosub subroutine2
}else{
msgbox not found
sleep 60000 ;milliseconds
}
Also, you can use real functions in autohotkey - you don't have to rely on gosubs. If you use gosubs, make sure you place a return at the end of them. You should also read the autohotkey docs concerning the "auto execute" section.

autohotkey long text and in a virtual machine

So I'm trying to learn autohotkey scripts and the documentation is lacking at best. First, can authotkey read commands and perform actions and such inside a virtual machine? I have a windows host and a linux virtual machine running eclipse. I'd like to get a hostring (or a keyboard macro, either is fine) to put in some long (10+ lines) of text. Can that actually work in a VM or do I have to run autohotkey inside the VM for it to work?
As for implementing this, I have 2 problems. First, how do I display multiple lines of text from a keyboard macro? I know about the Send command, but I haven't figured out how that works. I have this:
:*:insert::
(
Text to
insert
goes here
and more here
)
And this works fine except in notepad++, it inserts consecutively more tabs, so it will look like
Text to
insert
goes here
and more goes here
And so in my many line macro, by the end it's several pages scrolled off the screen.
As for keyboard macro, changing the above to
#c::
Send{Raw} (
stuf
to send
)
Return
This gives syntax errors and I have no idea what the correct way of doing that would be. Should I just stick with using hotstrings?
You could try to modify the clipboard and use control + v to paste it into the proper place.
Try:
#c::
{
clipboard := "yourtext`nMultiline`nYet another line"
send, {control down}v{control up}
return
}
The first 'insert' hotstring is correct,
however, you would get the same result that you describe,
if you performed manually, the keypresses that the hotstring is sending.
To get the output you want,
you need to change these two settings:
Settings, Preferences...,
Auto-Completion,
untick: Enable auto-completion on each input
Settings, Preferences...,
MISC.,
untick: Auto-indent
the '#c' hotstring is amended below:
#c::
Send {Raw}
(
stuf
to send
)
Return