Search code examples
dictionaryautohotkey

AutoHotkey - use dictionary/lookup to make hotstring replacements


I am trying to write an AutoHotkey script to give an easy way to input modified Latin characters (e.g. ö, å, é, ñ, etc). Let's say I want to send the combination (vowel) + ; + \ to (vowel) with an umlaut on it. I could do something like

:?*:a;\::ä
:?*:e;\::ë
:?*:i;\::ï
:?*:o;\::ö
:?*:u;\::ü

However, I was wondering if there was a more "slick" or "abstract" way to do it, where I could define a set of mappings beforehand, e.g.

Umlaut = {  a -> ä,  e -> ë,  i -> ï,  o -> ö,  u -> ü  }

and then apply the pattern to all elements of this mapping, e.g.

forall x in Umlaut
  :?*:(x.key);\::(x.value)

Is such a thing possible in AutoHotkey?

If yes, I'm wondering if it's also possible to take the abstraction one level further. E.g. suppose I also wanted to send the combination (vowel) + ' + \ to (vowel) with an acute on it. Then, I'd want to define a mapping such as

Diacritics = {
  ";" ->  {  a -> ä,  e -> ë,  i -> ï,  o -> ö,  u -> ü  },
  "'" ->  {  a -> á,  e -> é,  i -> í,  o -> ó,  u -> ú  },
}

and apply the same pattern

forall x in Diacritics
  forall y in x.value
    :?*:(y.key)(x.key);\::(y.value)

Solution

  • Sure, this is very doable with the Hotstring()(docs) function and a for-loop.

    diacritics := { "a" : "ä"
                  , "e" : "ë"
                  , "i" : "ï"
                  , "o" : "ö"
                  , "u" : "ü" }
    
    for key, value in diacritics
        Hotstring(":?*:" key ";\", value)
    

    Also I wasn't really sure what you meant with your second part of the question, but I'm assuming you mean like
    a + ; + \ produces ä
    and
    a + ' + \ produces á,
    then this code would work:

    diacritics := { ";" : { "a" : "ä"
                          , "e" : "ë"
                          , "i" : "ï"
                          , "o" : "ö"
                          , "u" : "ü" }
                  , "'" : { "a" : "á"
                          , "e" : "é"
                          , "i" : "í"
                          , "o" : "ó"
                          , "u" : "ú" } }
    
    
    for outer_key, value1 in diacritics
        for inner_key, value2 in value1
            Hotstring(":?*:" inner_key outer_key "\", value2)