Dynamic Hotstrings

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Menixator
Posts: 69
Joined: 30 Sep 2013, 04:10

Dynamic Hotstrings

11 Apr 2014, 02:26

Code: Select all

#Include Hotstring.ahk

Hotstring("i)((d|w)on)(t)", "$1'$3",3) 
;DONT -> DON'T
;dont -> don't
;dOnt -> dOn't
;WONT -> WON'T
;and so on.

Hotstring("i)colou?rs","$0 $0 everywhere!", 3) ; Regex, Case insensitive
; colors -> 'colors colors everywhere!'
; colours -> 'colours colours everywhere!'
You can find some more examples here
Hotstring.ahk
Status Update (6/2/17) As I have moved over to linux, this script has been unmaintained ever since. If anyone is interested in maintaining/updating this script, please email/pm me. I'll transfer the ownership of the GitHub repo over to them.
Last edited by Menixator on 16 Feb 2017, 17:09, edited 5 times in total.
lexikos
Posts: 9552
Joined: 30 Sep 2013, 04:07
Contact:

Re: Dynamic Hotstrings

11 Apr 2014, 03:06

How does this compare to hotstrings()? I remember that one had some issues/limitations, but can't remember what they were.
User avatar
Menixator
Posts: 69
Joined: 30 Sep 2013, 04:10

Re: Dynamic Hotstrings

11 Apr 2014, 03:57

I don't know about the limitations, but by default, this scripts treat the trigger as a string rather than a regex. You'd have to set the mode parameter to 3 if you wish to use regex.
As for another difference, I never got this to work with titan's function:

Code: Select all

hotstrings(":\)", ":smiley_face:")
It most certainly works in mine:

Code: Select all

Hotstring(":\)", "smiley_face",3)
;or
Hotstring(":)", "smiley_face") 
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Dynamic Hotstrings

14 Apr 2014, 15:48

I really like the added usage possibilities over the static hotstrings, but I have a small peeve with both Hotstring() and Hotstrings(). Both create hotkeys for every key monitored, which might interfere with other personal hotkeys unrelated to the script.

So I was thinking about replacing those hotkeys with a looping non-blocking Input command in a separate thread, with all keys of interest configured as an endkey and sending those to the callback section. The only problem I can think of is that there is a small timeframe between the input command finishing, the script processing the key and issuing the input command again. In that timeframe keypresses may be missed, although that may only be a problem with scripted input or keyrepeats and not manual typing.

Before I try this, have you tried something like this yet, or can you think of something that would prevent this from working properly?
User avatar
Menixator
Posts: 69
Joined: 30 Sep 2013, 04:10

Re: Dynamic Hotstrings

15 Apr 2014, 04:38

Nextron wrote:Both create hotkeys for every key monitored, which might interfere with other personal hotkeys unrelated to the script.
The hotkeys that are used for monitoring are prefixed with ~.
~: When the hotkey fires, its key's native function will not be blocked (hidden from the system).
So, I don't think it will be an issue.

on a different note, I fixed a bug in the library.
Both the library on top and has been both updated.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Dynamic Hotstrings

15 Apr 2014, 07:08

With 'might interfere' I didn't mean that your script blocks keys, because indeed, the ~ prevents blocking.

What does happen is that the Hotstring() function overwrites any pre-existing single button hotkeys, like F1::Send Help or LButton::DropDoubleClicks(). The Input command wouldn't have this drawback, but before I try to implement this modification I'm trying to think of any disadvantages of using Input.
lexikos
Posts: 9552
Joined: 30 Sep 2013, 04:07
Contact:

Re: Dynamic Hotstrings

15 Apr 2014, 19:59

As a workaround, you could use #if true or #IfWinNotActive something never active to keep your hotkeys separate and give them priority. (Context sensitive hotkeys take precedence over global hotkeys even if the context is always active.) However, then Hotstring() won't detect those keys.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Dynamic Hotstrings

17 Apr 2014, 18:57

Thanks, that's something to remember. I don't have many single button hotkeys, so it would be easy to implement, but then it would have a negative affect on the hotstring function. For example not resetting the trigger after a mouse click because of a hotkeyed LButton.

So I did modify the script to use input instead of hotkeys. Unlike my expectations, the loop is quick enough to not miss any keys.
Hotstring using input
While looking more careful at the function (and appreciating it even more), I did notice some double keys defined in the breakkeys variable and also a small bug when using conditional hotstrings: When a trigger's condition isn't satisfied, the trigger gets reset too, preventing any extended triggers from working.

Code: Select all

Hotstring("test","replacement",1,1,"Condition") ;Condition() returns false
Hotstring("testing","other_replacement") ;Will not work because %trigger% gets reset after typing 'test'
This should fix it:

Code: Select all

; At line 114, change:
if (!A_LoopRetVal){
    ; If the function returns a non-true value.
    continue
}
;to
if (!A_LoopRetVal){
    ; If the function returns a non-true value.
    matched := false
    continue
}
User avatar
Menixator
Posts: 69
Joined: 30 Sep 2013, 04:10

Re: Dynamic Hotstrings

18 Apr 2014, 00:25

Nextron wrote: While looking more careful at the function (and appreciating it even more), I did notice some double keys defined in the breakkeys variable and also a small bug when using conditional hotstrings: When a trigger's condition isn't satisfied, the trigger gets reset too, preventing any extended triggers from working.

Code: Select all

Hotstring("test","replacement",1,1,"Condition") ;Condition() returns false
Hotstring("testing","other_replacement") ;Will not work because %trigger% gets reset after typing 'test'
This should fix it:

Code: Select all

; At line 114, change:
if (!A_LoopRetVal){
    ; If the function returns a non-true value.
    continue
}
;to
if (!A_LoopRetVal){
    ; If the function returns a non-true value.
    matched := false
    continue
}
Fixed.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Dynamic Hotstrings

22 Oct 2014, 06:58

Line 86: typed .= effects[numKey1] ==> typed .= effect[numKey1]
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
Guest

Re: Dynamic Hotstrings

25 Oct 2014, 17:20

The output of example Hotstring("i)colou?rs","$0 $0 everywhere!",3) ; Regex, Case insensitive is exactly that, $0 $0 everywhere! while it should be colours colours everywhere! - The other examples do work.

AHK v1.1.16.05, 64bit
Guest

Re: Dynamic Hotstrings

10 Dec 2014, 05:25

Hey Menixator. I've been trying to get this to work in my script but I can't get the it call the function I give it. The manually written hotstrings work fine but not the one created with the Hotstring function - nothing happens when I type it. This is my code:

Code: Select all

#Include C:\Users\lowfisun\Documents\AutoHotkey Scripts\ahk-repo\Hotstring\Hotstring.ahk

#IfWinActive ahk_class Star Wars Galactic Battlegrounds: Clone Campaigns

Hotstring("t1", "moveToTrigger")

:*:t2::
{
	moveToTrigger("t2")
	return
}
:*:t3::
{
	moveToTrigger("t3")
	return
}

moveToTrigger(str){
	;MsgBox %str%
	FoundPos := RegExMatch(str, "\d+", foundNum)
	;MsgBox % "foundNum1 = " . foundNum
	MouseGetPos, xpos, ypos
	if foundNum = 1
		Click 110, 90
	if foundNum = 2
		Click 110, 110
	if foundNum = 3
		Click 110, 130
	if number = 4
		Click 110, 150
	if number = 5
		Click 110, 170
	if number = 6
		Click 110, 190
	if number = 7
		Click 110, 210
	if number = 8
		Click 110, 240
	if number = 9
		Click 110, 260
	if number = 10
		Click 110, 290
	if number = 11
		Click 110, 310
	if number = 12
		Click 110, 330
	MouseMove, xpos, ypos
	return
}
Guest

Re: Dynamic Hotstrings

10 Dec 2014, 05:26

I forgot to mention I'm using Version v1.1.13.01
Guest

Re: Dynamic Hotstrings

13 Dec 2014, 09:05

I was the guest who posted last ^. The problem was that Hotstring() function wasn't getting called because there was a label with a return statement preceding it. The function works great. Thanks!
-_+
Posts: 70
Joined: 06 Dec 2014, 12:43

Re: Dynamic Hotstrings

21 Dec 2014, 13:57

This lib doesn't play well with other keyboard locales: hitting same buttons in any locale - will trigger the regex which would be typed if locale was english.
burton666
Posts: 49
Joined: 09 Dec 2014, 15:26

Re: Dynamic Hotstrings

11 Mar 2015, 03:54

Can anyone help me with this:

if I enter A010 I want PLOCK_LOW.A.01.0
and D110 I want PLOCK_LOW.D.11.0

If I enter A011 I want PLOCK.A.01.1
and E302 I want PLOCK.E.30.2

So the string is always four letter/digits and always starting with a single letter ranging form "A" to "O"

The last digit is 0,1 or 2 and determines the start of the replaced string. If ending with "0" it should start with "PLOCK_LOW"
and if last digit is 1 or 2 it should start with "PLOCK"
Guest

Re: Dynamic Hotstrings

11 Mar 2015, 09:34

Try this

Code: Select all

#SingleInstance, force
#Persistent
#NoEnv
SetBatchLines, -1
SendMode, Input
SetKeyDelay, -1

Hotstring("([A-O])(\d)(\d)([012])\s","expand",3)
Return

Expand:
Send % ($.value(4) = 0 ? "PLOCK_LOW." : "PLOCK.") $.value(1) "." $.value(2) $.value(3) "." $.value(4)
Return

#include Hotstring.ahk
; download it from here https://github.com/menixator/ahk-repo/blob/master/Hotstring/Hotstring.ahk
burton666
Posts: 49
Joined: 09 Dec 2014, 15:26

Re: Dynamic Hotstrings

11 Mar 2015, 13:49

Guest wrote:Try this

Code: Select all

#SingleInstance, force
#Persistent
#NoEnv
SetBatchLines, -1
SendMode, Input
SetKeyDelay, -1

Hotstring("([A-O])(\d)(\d)([012])\s","expand",3)
Return

Expand:
Send % ($.value(4) = 0 ? "PLOCK_LOW." : "PLOCK.") $.value(1) "." $.value(2) $.value(3) "." $.value(4)
Return

#include Hotstring.ahk
; download it from here https://github.com/menixator/ahk-repo/blob/master/Hotstring/Hotstring.ahk

Thanks, I managed to figure out how to make the match case insensitive with (?i) but how can I alter it so that it always outputs the new string in uppercase? I fiddled around with \u but did not get it to work.
Guest

Re: Dynamic Hotstrings

11 Mar 2015, 13:57

Try

Send % ($.value(4) = 0 ? "PLOCK_LOW." : "PLOCK.") format("{:U}",$.value(1)) "." $.value(2) $.value(3) "." $.value(4)

You do need AHK v1.1.20+ for the Uppercase Format() to work - http://ahkscript.org/docs/commands/Format.htm
burton666
Posts: 49
Joined: 09 Dec 2014, 15:26

Re: Dynamic Hotstrings

17 Mar 2015, 04:05

why does not this work?

Code: Select all

Hotstring("((?i)MAX|max)(\d)(\d)\s","expand2",3)
Return

Expand2:

Send %  format("{:U}",$.value(1)) "." $.value(2) $.value(3) "."  
send {enter}
Return
I am trying to match MAX01, MAX23, max02, max20 and similar. What am I doing wrong?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 103 guests