HotKeyString is very similar to Hotstrings.
The difference is, they are little more complex, much more unlikely to be activated by accident and do not require EndChar.
Different to hotstrings, hotkeystrings get activated by pressing the sequence of characters while keeping one of previous keys down.
Your HotKeyString is "dir".
- Press and hold {d}, type {i}{r}, than release {d}For some HotKeyStrings you will have to use a little more complex sequence. For example HotKeyString "calc".
- #MaxThreadsPerHotkey must be at least 2 for this to work, because c is used twice :!:
1. Press and hold {c}
2. Press and hold {a}
3. Release {c} but keep holding {a}
4. Type {l}{c}
5. Release {a}
You will need to call HotKeyString("LabelOrFunction") only once, It will create all hotkeys, watch your typing and launch a function or label.
When launching function, the first parameter will be the key sequence typed.
When launching Label, ErrorLevel will contain typed sequence.
HotKeyString will detect any sequence as long as you press more than 2 keys and the key released last is different to the last key in sequence.
So for example HotKeyString "dir", when the last released key is r, function will not launch.
You can make HotKeyString context-sensitive, for example HotKeyString("function","ahk_class Notepad")
You may change KeySet to include more or less characters, for example letters or numbers only.
HotKeyString("function","","0123456789")
Enjoy
Simple Example
#include HotKeyString.ahk btw=by the way ahk=AutoHotkey wkr=With Kind Regards`n`nYour Name HotKeyString("Label") Return Label: SendInput % %ErrorLevel% ReturnA little more complex example.
#include HotKeyString.ahk #MaxThreadsPerHotkey 99 ;Required for HotKeyString "calc", because c needs to be pressed twice. Func() ;will create static variables HotKeyString("Func") ;Enable HotKeyString Return Func(variable=""){ static If (!_init && (_init:=1)){ ;some hotkeys dir:=A_ScriptDir win:=A_WinDir prog:=A_ProgramFiles calc:=A_WinDir "\system32\calc.exe" Paint:=A_WinDir "\system32\mspaint.exe" Return } If (%variable%="") Return If GetKeyState("Shift","P") ;Show in explorer instead of launching the path Run,% "explorer.exe /e`, /n`, /select`," . %variable% else Run % %variable% }Function
HotKeyString(Func,IfWinActive="",KeySet = "abcdefghijklmnopqrstuvwxyz0123456789_"){ static If ((IsLabel(Func) || IsFunc(Func)) && (_func:=func) && (_KeySet:=KeySet)) Loop Parse, _KeySet { If IfWinActive<> Hotkey, IfWinActive, %IfWinActive% HotKey ~*$%A_LoopField%, HotKeyStringDown, B HotKey ~*%A_LoopField% up, HotKeyStringUp, B If IfWinActive<> Hotkey, IfWinActive } Return HotKeyStringDown: StringReplace kd, A_ThisHotKey, ~ StringReplace kd, kd, * StringReplace kd, kd, $ If (StrLen(keys)>1 or kd!=keys) keys .= kd KeyWait, %kd% SetTimer, HotKeyStringClear,-100 Return HotKeyStringUp: If (StrLen(keys)<2) Goto, HotKeyStringClear StringReplace k, A_ThisHotKey, ~ StringReplace k, k, * StringReplace k, k, %A_Space%up pressed=0 Loop,Parse,_KeySet pressed+=GetKeyState(A_LoopField,"P") If (!pressed && k!=SubStr(keys,0)){ ControlGetFocus, Control , A If Control contains Edit,Scintilla SendInput % "{BS " . StrLen(keys) . "}" If IsFunc(_Func) %_Func%(keys) else if IsLabel(_Func) && ErrorLevel:=keys Gosub % _Func Goto, HotKeyStringClear } Return HotKeyStringClear: pressed=0 Loop,Parse,_KeySet pressed+=GetKeyState(A_LoopField,"P") If !pressed keys= Return }
Optional Function
Using this one requires you to hold any key down, just press in sequence and release the previous key after pressing next key.
- E.g. (btw) press and hold b, press and hold t and release b, then press w and release t, then release w.
Due to this Backspacing is not done as well :!:
Minimum letters is set to 3 (because when typing fast it would often activate HotKeyString)
HotKeyString(Func,IfWinActive="",KeySet = "abcdefghijklmnopqrstuvwxyz0123456789_"){ static If ((IsLabel(Func) || IsFunc(Func)) && (_func:=func) && (_KeySet:=KeySet)) Loop Parse, _KeySet { If IfWinActive<> Hotkey, IfWinActive, %IfWinActive% HotKey ~*$%A_LoopField%, HotKeyStringDown, B HotKey ~*%A_LoopField% up, HotKeyStringUp, B If IfWinActive<> Hotkey, IfWinActive } Return HotKeyStringDown: StringReplace kd, A_ThisHotKey, ~ StringReplace kd, kd, * StringReplace kd, kd, $ If (StrLen(keys)>1 or kd!=keys) keys .= kd KeyWait, %kd% SetTimer, HotKeyStringClear,-100 Return HotKeyStringUp: If (StrLen(keys)<3) Goto, HotKeyStringClear StringReplace k, A_ThisHotKey, ~ StringReplace k, k, * StringReplace k, k, %A_Space%up pressed=0 Loop,Parse,_KeySet pressed+=GetKeyState(A_LoopField,"P") If (!pressed){ If IsFunc(_Func) %_Func%(keys) else if IsLabel(_Func) && ErrorLevel:=keys Gosub % _Func Goto, HotKeyStringClear } Return HotKeyStringClear: pressed=0 Loop,Parse,_KeySet pressed+=GetKeyState(A_LoopField,"P") If !pressed keys= Return }