Keystring iterators

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Keystring iterators

22 Aug 2017, 22:04

Is there any way in AHK to specify an iterator in a keystring, so that all or part of the string is repeated a number of times?

If not, is there any reasonably common convention for this?

And yes, I do know that I can use Loop for this.

Andrew
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Keystring iterators

22 Aug 2017, 23:02

It's not entirely clear if you want a character repeated for use in Send/SendInput or not. Just in case:

Code: Select all

F12:: SendInput, {a 12}
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Re: Keystring iterators

23 Aug 2017, 04:22

Thanks for that, for a start - I didn't actually know that one, but tracking it down in the Help it looks like all I'll get.
I was hoping for something more general, like 8x[{Down}^d].

Andrew
Guest

Re: Keystring iterators

23 Aug 2017, 04:26

Code: Select all

Loop, 8
	Send keys
Or create a function if you have many
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Re: Keystring iterators

23 Aug 2017, 04:35

Yep, I think I'll need to do something like that to send it.

My main concern at the moment is the higher level where I *define* a string with iterations, which is parsed by AHK. I'm really looking for a relatively simple but flexible shorthand for writing such definitions. I'm sure plenty have been there before me - I'd rather use an elegant shorthand defined by someone with lots of experience in such things, than define some klunky representation of my own,

Andrew
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Keystring iterators

23 Aug 2017, 08:49

Hmm, what are your specifications? Let's say you want to invent your own shorthand, then you could limit yourself not to use all characters possible. Like invent a shorthand that takes <> as a marker for the string that you want to repeat. If your user-defined function finds such a term with the number of repetitions it replaces it accordingly. See RLE for more inspiration.

Example: <abc 4>{Left 3}qwer<t 5>y{Enter} => abcabcabcabc{Left}{Left}{Left}qwerttttty{Enter}

To answer the question: I don't know of anybody who has come up with a RLE style shorthand and thoroughly tested it. I guess the way keys with names can take an optional Count argument is such a shorthand by the developer, and my suggestion is to make your own similar to that.

The example above is obviously not working for sending data to a XML document, so you could maybe write your function to take an optional second argument that allows you to choose from <>, [] and() or any other (rarely used) combination of characters.

I am interested to write such a functions, but it would be untested. Or more like mildly tested.

Try this:

Code: Select all

; attempt #1

TXT = <abc 4>{Left 3}qwer<t 5>y{Enter} ; string to expand
SendInput, % RLExpand(TXT)
ExitApp



;-------------------------------------------------------------------------------
RLExpand(Text, Style := "<>") { ; expand Text where Style encoding is present
;-------------------------------------------------------------------------------
    ; Style must be one of <>,() or []
    ;---------------------------------------------------------------------------
    If (Style = "<>")
        _Beg := "<", _End := ">"

    Else If (Style = "()")
        _Beg := "\(", _End := "\)"

    Else If (Style = "[]")
        _Beg := "\[", _End := "\]"

    Else {
        MsgBox, 16, Error in RLExpand(), unsupported style: %Style%
        Return
    }

    Result := Text, Pos := 1,
    Needle = %_Beg%(?P<STR>[^\s%_End%]+?) (?P<REP>\d+)%_End%

    While Pos := RegExMatch(Result, Needle, Match_, Pos) {
        Replacement := ""
        Loop, %Match_REP%
            Replacement .= Match_STR
        Result := StrReplace(Result, Match_, Replacement, 1)
    }

    Return, Result
}
I hope that helps.

Deal with the function as you please. The greatest benefit for the community is probably when you test it thoroughly and report any bugs you find, with or without any possible fixes.
Have fun. :)
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Keystring iterators

23 Aug 2017, 11:01

Maybe something like this:

Code: Select all

q::
SendInput, % "{Up}{Left 3}a{b 3}" StrRept("{Down}^d", 8) "{Down}{Right 3}c{d 3}"
return

StrRept(vText, vNum)
{
	return StrReplace(Format("{:" vNum "}","")," ",vText)
	;return StrReplace(Format("{:0" vNum "}",0),0,vText)
}
Although depending on how often I was writing code, and what I was doing, I do like wolf_II's replace text approach. You could pick some rare combination like << or >>, and replace those strings with Chr(1), and then parse the text using Chr(1) as a delimiter, manipulating every even-numbered string in the loop into something that SendInput can handle, and appending text to a new string for use with SendInput.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Re: Keystring iterators

23 Aug 2017, 20:08

Thanks guys, particularly for the code. A

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], gabelynn1, watashiakilla and 342 guests