Toggling multiple hotkeys between vanilla and bindings

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SoraHjort
Posts: 8
Joined: 13 Sep 2017, 17:31

Toggling multiple hotkeys between vanilla and bindings

13 Sep 2017, 18:58

I've been writing a script that'll let me type with specific unicode characters (using sendinput {U+xxxx} command). This is to help me expedite a art project I've been working on that requires a lot of typing with Runic, which are also now part of unicode.
And I've completed the primary setup. However I also want it to toggle between what I have the keys set to and to what they were originally. However I've ran into two issues.

1. It sends the first hotkey bind when toggling
2. it breaks after the first toggle cycle (on, then off, then it won't turn back on), though problem 1 still persists afterwards

I am also sure there is a better way to do it than the way I did it.

A side issue is that I can't seem to use "^ScrollLock", hence why I'm using "^Home" below. I would prefer to use scrolllock, because it makes sense to me.

So any help in my two issues, as well as tips and examples of how to have it better setup will be greatly appreciated.
Also, yes, I know I don't need the {} brackets. It's mainly for my visual organization. (As well as being able to collapse trees in N++)

Code: Select all

#InstallKeybdHook
#UseHook
#KeyHistory
#MaxThreadsPerHotkey 10

Runic := 0

^Home::
    ++Runic
    #if Runic = 1 ;Enabling Runic Mode
        {
        ;key - rune - pronounce
        a::sendinput {U+16A8} ;A   -   ᚨ   -   a
        b::sendinput {U+16D2} ;B   -   ᛒ   -   b
        c::sendinput {U+16B2} ;C   -   ᚲ   -   k
        d::sendinput {U+16DE} ;D   -   ᛞ   -   d
        e::sendinput {U+16D6} ;E   -   ᛖ   -   e
        f::sendinput {U+16A0} ;F   -   ᚠ   -   f
        g::sendinput {U+16B7} ;G   -   ᚷ   -   g
        h::sendinput {U+16BA} ;H   -   ᚺ   -   h
        i::sendinput {U+16C1} ;I   -   ᛁ   -   i
        j::sendinput {U+16C3} ;J   -   ᛃ   -   j
        k::sendinput {U+16B2} ;K   -   ᚲ   -   k
        l::sendinput {U+16DA} ;L   -   ᛚ   -   l
        m::sendinput {U+16D7} ;M   -   ᛗ   -   m
        n::sendinput {U+16BE} ;N   -   ᚾ   -   n
        o::sendinput {U+16DF} ;O   -   ᛟ   -   o
        p::sendinput {U+16C8} ;P   -   ᛈ   -   p
        q::sendinput {U+16DC} ;Q   -   ᛜ   -   ng (as in amo'ng' and seei'ng')
        r::sendinput {U+16B1} ;R   -   ᚱ   -   r
        s::sendinput {U+16CB} ;S   -   ᛋ   -   s
        t::sendinput {U+16CF} ;T   -   ᛏ   -   t
        u::sendinput {U+16A2} ;U   -   ᚢ   -   u
        v::sendinput {U+16B9} ;V   -   ᚹ   -   w
        w::sendinput {U+16B9} ;W   -   ᚹ   -   w
        x::sendinput {U+16A6} ;X   -   ᚦ   -   th (as in 'th'e and bro'th')
        y::sendinput {U+16C7} ;Y   -   ᛇ   -   ï (unmodifiable 'eye')
        z::sendinput {U+16C9} ;Z   -   ᛉ   -   R ('-ts' as in ca'ts'. Yes I know it's weird for it to be symbolized as R. I didn't make it up however.)
        return
        }
    #if Runic != 1 ;Disabling Runic Mode
        {
        Runic := 0
        a::a
        b::b
        c::c
        d::d
        e::e
        f::f
        g::g
        h::h
        i::i
        j::j
        k::k
        l::l
        m::m
        n::n
        o::o
        p::p
        q::q
        r::r
        s::s
        t::t
        u::u
        v::v
        w::w
        x::x
        y::y
        z::z
        return
        }
    return
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Toggling multiple hotkeys between vanilla and bindings

13 Sep 2017, 19:53

You're misusing the #If vs If. If is used inside a hotkey. You also don't put the hotkeys created by :: into a hotkey nor wrap them in brackets. #If goes outside of a hotkey, and all hotkeys literally listed below it are affected, up until the next #If.

Your code kind of works by accident. But as it is now

Code: Select all

#if Runic != 1
{
Runic := 0
; rest of code
}
Does not work. There's no thread that ever executes those lines. This is why you have Problem 2 -- Runic is never set to 0. It just keeps counting by one every time you use ++Runic.

I think you'll have a much easier time using the Suspend command to disable the hotkeys in the script and turn them back on. Something as simple as ^Home::Suspend and then listing all the hotkeys for producing the Runic characters would work.

Code: Select all

^Home::Suspend
a::sendinput {U+16A8} ;A   -   ᚨ   -   a
b::sendinput {U+16D2} ;B   -   ᛒ   -   b
c::sendinput {U+16B2} ;C   -   ᚲ   -   k
; rest of hotkeys

If you would like some other approaches, you can see examples on how to toggle with a variable as used in #If expressions in these threads (Note: The first link is from the old forums, the second link is on these forums) http://www.autohotkey.com/board/topic/6 ... re-thread/ or https://autohotkey.com/boards/viewtopic.php?f=7&t=11952 . Using #If can be useful if you have other parts of your script, as Suspend acts globally on every hotkey, not just a subset -- #If let's you affect only a subset.
SoraHjort
Posts: 8
Joined: 13 Sep 2017, 17:31

Re: Toggling multiple hotkeys between vanilla and bindings

20 Sep 2017, 17:19

As I had said, I realized I didn't need the {} brackets, but I used them for organization reasons.

Anyway, as for an update, over the past week I slowly worked on this. And I ended up going down the hotkey route. Which I had trouble finding examples on the forum for because, well, the term is used for more than just the command (understandably). So there was a lot of back and forth.

So here is the final script. It could be cleaned up a little, and streamlined, but it works as is.
In it it translates keyboard keys to Norse Runic. It will detect specific key strokes "ng", "th", and "ts" to do proper runic characters.
it also has a little overlay in the top left corner to display when runic mode is on.
Enable and Disable is toggled via Ctrl-ScrollLock

Hope I have enough comments in the script to make clear on what is going on.

Code: Select all

#InstallKeybdHook
#InstallMouseHook
#UseHook
#KeyHistory
#MaxThreadsPerHotkey 10

Runic := 0
RMR = ᚱᚢᚾᛁᚲ ᛗᛟᛞᛖ ᛖᚾᚨᛒᛚᛖᛞ ;Will not show up correctly if not set to UTF-8 Encoding. "UTF8 without BOM" also does not work.
RME = Runic Mode Enabled

        Gui RuneDisplay:Default ;Setting Active GUI
        Gui +AlwaysOnTop -Caption
        Gui Font, s18 c666666 w600, FreeMono ;Setting Border Color
        ;FreeMono font - https://www.gnu.org/software/freefont/
        ;RMR Text Border
        Gui Add,text,Center BackgroundTrans xm+0 ym+0, %RMR%
        Gui Add,text,Center BackgroundTrans xp+0 yp+2, %RMR%
        Gui Add,text,Center BackgroundTrans xp+2 yp-0, %RMR%
        Gui Add,text,Center BackgroundTrans xp+0 yp-2, %RMR%
        ;RMR Text
        Gui Font, s18 cffffff w600, FreeMono
        Gui Add,text,Center BackgroundTrans xp-1 yp+1, %RMR%
        
        ;RME Border
        Gui Font,c6611cc 
        Gui Add,text,xm+0 ym+26 Center BackgroundTrans, %RME%
        Gui Add,text,xp+0 yp+2 Center BackgroundTrans, %RME%
        Gui Add,text,xp+2 yp+0 Center BackgroundTrans, %RME%
        Gui Add,text,xp+0 yp-2 Center BackgroundTrans, %RME%
        Gui Font,ccc66FF 
        ;RME Text
        Gui Add,text,xp-1 yp+1 Center BackgroundTrans, %RME%
        
		;Making GUI transparent
        Gui Color, 000000
		Gui +LastFound
		WinSet, TransColor, 000000 140

^sc0046:: ;Ctrl-ScrollLock
    ++Runic
    if Runic = 1 ;Enabling Runic
        {
		Gui RuneDisplay:show, x10 y10 autosize NoActivate, Runic ;Showing GUI
        ;Using ~ for detection purposes.
        ;Using * to ignore modifiers. Would prefer just shift
        ;But I'm not seeing a sleek solution for something like this
        hotkey, *a, ansuz, on
        hotkey, *b, berkanan, on
        hotkey, *c, kaunan, on
        hotkey, *d, dagaz, on
        hotkey, *e, ehwaz, on
        hotkey, *f, fehu, on
        hotkey, *~g, gebo, on
        hotkey, *~h, hagalaz, on
        hotkey, *i, isaz, on
        hotkey, *j, jera, on
        hotkey, *k, kaunan, on
        hotkey, *l, laguz, on
        hotkey, *m, mannaz, on
        hotkey, *n, naudiz, on
        hotkey, *o, othila, on
        hotkey, *p, perth, on
        ;Disabling some due to detection methods
        ;Leaving them in as a "incase" measure
        ;hotkey, *q, ingwaz, on
        hotkey, *r, raido, on
        hotkey, *~s, sowilo, on
        hotkey, *t, tiwaz, on
        hotkey, *u, uruz, on
        hotkey, *v, wunjo, on
        hotkey, *w, wunjo, on
        ;hotkey, *x, thurisaz, on
       hotkey, *y, ihwaz, on
        ;hotkey, *z, algiz, on
        return
        }
    if Runic != 1 ;Disabling Runic
        {
        Runic := 0
        gui RuneDisplay:hide ;Hiding GUI
        hotkey, *a, ansuz, off
        hotkey, *b, berkanan, off
        hotkey, *c, kaunan, off
        hotkey, *d, dagaz, off
        hotkey, *e, ehwaz, off
        hotkey, *f, fehu, off
        hotkey, *g, gebo, off
        hotkey, *h, hagalaz, off
        hotkey, *i, isaz, off
        hotkey, *j, jera, off
        hotkey, *k, kaunan, off
        hotkey, *l, laguz, off
        hotkey, *m, mannaz, off
        hotkey, *n, naudiz, off
        hotkey, *o, othila, off
        hotkey, *p, perth, off
        ;hotkey, *q, ingwaz, off
        hotkey, *r, raido, off
        hotkey, *s, sowilo, off
        hotkey, *t, tiwaz, off
        hotkey, *u, uruz, off
        hotkey, *v, wunjo, off
        hotkey, *w, wunjo, off
        ;hotkey, *x, thurisaz, off
        hotkey, *y, ihwaz, off
        ;hotkey, *z, algiz, off
        return
        }
    return
        
   
;==============Defining Runic Bindings==============
;          Labels are the name of the runes
; Additional Detection for Runes for ng, th, and ts
ansuz:
sendinput {U+16A8} ;A   -   ᚨ   -   a
return

berkanan:
sendinput {U+16D2} ;B   -   ᛒ   -   b
return

kaunan:
sendinput {U+16B2} ;C   -   ᚲ   -   k
return

dagaz:
sendinput {U+16DE} ;D   -   ᛞ   -   d
return

ehwaz:
sendinput {U+16D6} ;E   -   ᛖ   -   e
return

fehu:
sendinput {U+16A0} ;F   -   ᚠ   -   f
return

gebo:
send {backspace 1}
sendinput {U+16B7} ;G   -   ᚷ   -   g
return

hagalaz:
send {backspace 1}
sendinput {U+16BA} ;H   -   ᚺ   -   h
return

isaz:
sendinput {U+16C1} ;I   -   ᛁ   -   i
return

jera:
sendinput {U+16C3} ;J   -   ᛃ   -   j
return

laguz:
sendinput {U+16DA} ;L   -   ᛚ   -   l
return

mannaz:
sendinput {U+16D7} ;M   -   ᛗ   -   m
return

naudiz:
sendinput {U+16BE} ;N   -   ᚾ   -   n
input, UserInput, V L1 C,,
    If (UserInput = "g") ;if ng go to ingwaz
        {
        send {backspace 2}
        goto ingwaz
        return
        }
return

othila:
sendinput {U+16DF} ;O   -   ᛟ   -   o
return

perth:
sendinput {U+16C8} ;P   -   ᛈ   -   p
return

ingwaz:
sendinput {U+16DC} ;Q   -   ᛜ   -   ng (as in amo'ng' and seei'ng')
return

raido:
sendinput {U+16B1} ;R   -   ᚱ   -   r
return

sowilo:
send {backspace 1}
sendinput {U+16CA} ;S   -   ᛊ   -   s
return

tiwaz:
sendinput {U+16CF} ;T   -   ᛏ   -   t
input, UserInput, V L1 C,,
    If (UserInput = "h") ;if th then go to thursaz
        {
        send {backspace 2}
        goto thurisaz
        return
        }
    If (UserInput = "s") ;if ts then go to algiz
        {
        send {backspace 2}
        goto algiz
        return
        }
return

uruz:
sendinput {U+16A2} ;U   -   ᚢ   -   u
return

wunjo:
sendinput {U+16B9} ;W   -   ᚹ   -   w
return

thurisaz:
sendinput {U+16A6} ;X   -   ᚦ   -   th (as in 'th'e and bro'th')
return

ihwaz:
sendinput {U+16C7} ;Y   -   ᛇ   -   ï (unmodifiable 'eye')
return

algiz:
sendinput {U+16C9} ;Z   -   ᛉ   -   R ('-ts' as in ca'ts')
return
;================End=================
For anyone having trouble viewing Runic characters, I recommend the FreeFont's FreeMono font and Google's Noto Font Family. The difference between the two FreeMono has all the characters in one font file. Where as Noto has them in separate individual fonts.

The only issue I've ran into is it may trip up if you mash the keyboard. It shouldn't during normal typing.

It's working about how I wanted it to. I may repost this into the Scripts and Functions board later. Gonna give it a few days to make sure there isn't any kinks I haven't ran into. Maybe after I figure out the slight issue when I tried to get "ck" detected and send as a single character, since both C and K are the same character as is. The method I was using for the other sequence detection wasn't working for some reason.

Beyond that, any tips on how clean it up would be nice, as I can be a bit of a messy scripter. So I'm not much in realizing on how to compact things.

Edit 2017-09-21: Reenabled Y, which was erronously disabled when posted. Also corrected S to the correct futhark rune.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], ReyAHK and 263 guests