Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

HotKeyString() - Do you need more hotkeys?


  • Please log in to reply
9 replies to this topic
HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
Inspired by Chording keyboard: strings sent at key combinations, I have worked out a way to perform tons of actions without conflicting with existent hotkeys and hotstrings.

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%
Return
A 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
}


entropic
  • Members
  • 181 posts
  • Last active: Nov 27 2011 03:15 AM
  • Joined: 21 Dec 2008
I can't believe this didn't get any replies, this is great, thanks HotKeyIt.

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

I can't believe this didn't get any replies, this is great, thanks HotKeyIt.

Thank you entropic, I am glad you found it useful ;)
Probably most users are satisfied with hotkeys and hotstrings, but not me :D

Delusion
  • Members
  • 272 posts
  • Last active: Jul 13 2014 09:04 PM
  • Joined: 16 Jul 2008
omg i cant believe i never saw this!
that is just awesome
many thanks for your great work :D
QuickSubs | Popcorn Movie Catalog
All my scripts are just in AutoHotkey v1.0.48.05

RevmarkBrown
  • Guests
  • Last active:
  • Joined: --
my handicap kids will benefit, thaxs gazilion

Chicken Pie 4 Tea
  • Members
  • 379 posts
  • Last active: Dec 12 2014 06:46 PM
  • Joined: 18 Aug 2009
HMM I am trying the optional function (where you dont have to hold a key down) it doesn't work UNLESS I hold the first keydown, i.e. if I type btw nothing happens but if I hold down the b and type tw it works.

I used the simple example as a test, was this a mistake of mine?
"Choose your parents wisely"

HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008
That is how it should work. Only fire if b is hold and tw pressed or bt pressed t hod and w pressed.
The other example shows how to do for calc where you can't hold c and type alc but type ca hold a and type lc.

TheGreatSwami Woo
  • Members
  • 237 posts
  • Last active: Jan 22 2012 03:31 PM
  • Joined: 26 May 2011
ok I was just confused when you said

Optional Function
This one does not require you to hold any key down, just press in sequence



HotKeyIt
  • Moderators
  • 7439 posts
  • Last active: Jun 22 2016 09:14 PM
  • Joined: 18 Jun 2008

ok I was just confused when you said

Optional Function
This one does not require you to hold any key down, just press in sequence

Sorry, now I was confused, I forgot about that function already :oops:

I have corrected the explanation. Sorry for any confusion.

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.



Kupiko
  • Members
  • 3 posts
  • Last active: Dec 18 2015 02:48 PM
  • Joined: 21 Jul 2015

Hello everyone,

 

i'm working with AHK for a while now and found out about this script which i really like the idea of. Except i can't make it work.

I can't really configure it to suit my needs cause i don't understand advanced commands in the hotkeyscript.ahk

 

When i use examples in notepad. Backspacing deletes not only my hotkey like 'btw' but even things that were written before.

Also i don't know how to make it run when i press combinations of F1..F12 keys..Which i want to use because they are not really used for much anyway.

One more thing i can't figure out is how to adapt it to not replace string but run function (like for example when i type 'btw' it will do few clicks, send some text and so on?

 

I'm not really sure i'll be understood now :) because my terminology might not be exact.

But if anyone could have a look and bring me closer to the solution i'll be most thankful.