Page 1 of 1

Convert units as you type?

Posted: 02 Jul 2018, 01:39
by barkoosh
Hello

Is there an easy way to convert units while typing with Autohotkey? For example, if I type 11in then space, I get 27.94 cm; if I type -12.1F then space, I get -24.5 C.

Thanks

Re: Convert units as you type?

Posted: 02 Jul 2018, 09:02
by trust_me
You could use regex dynamic hotstrings . https://autohotkey.com/board/topic/1147 ... otstrings/

Here is a proof of concept :

Just type 11in

updated to use decimal numbers like 11.5

Code: Select all


/*
    Function: HotStrings
        Dynamically adds regular expression hotstrings.

    Parameters:
        c - regular expression hotstring
        a - (optional) text to replace hotstring with or a label to goto,
            leave blank to remove hotstring definition from triggering an action

    Examples:
> hotstrings("(B|b)tw\s", "%$1%y the way") ; type 'btw' followed by space, tab or return
> hotstrings("i)omg", "oh my god!") ; type 'OMG' in any case, upper, lower or mixed
> hotstrings("\bcolou?r", "rgb(128, 255, 0);") ; '\b' prevents matching with anything before the word, e.g. 'multicololoured'

    License:
        - RegEx Dynamic Hotstrings: Modified version by Edd  
        - Original: <http://www.autohotkey.net/~polyethene/#hotstrings>
        - Dedicated to the public domain (CC0 1.0) <http://creativecommons.org/publicdomain/zero/1.0/>
*/

hotstrings("(\d+\.?\d?+)in", "convert") 

Gui, Add, Edit, w300 h100
Gui, Show
Return


GuiEscape:
GuiClose:
ExitApp


convert:
p:=$1 * 2.54
Send, %p%  cm
Return



hotstrings(k, a = "", Options:="")
{
    static z, m = "~$", m_ = "*~$", s, t, w = 2000, sd, d = "Left,Right,Up,Down,Home,End,RButton,LButton", f = "!,+,^,#", f_="{,}"
    global $
    If z = ; init
    {
        RegRead, sd, HKCU, Control Panel\International, sDecimal
        Loop, 94
        {
            c := Chr(A_Index + 32)
            If A_Index between 33 and 58
                Hotkey, %m_%%c%, __hs
            else If A_Index not between 65 and 90
                Hotkey, %m%%c%, __hs
        }
        e = 0,1,2,3,4,5,6,7,8,9,Dot,Div,Mult,Add,Sub,Enter
        Loop, Parse, e, `,
            Hotkey, %m%Numpad%A_LoopField%, __hs
        e = BS,Shift,Space,Enter,Return,Tab,%d%
        Loop, Parse, e, `,
            Hotkey, %m%%A_LoopField%, __hs
        z = 1
    }
    If (a == "" and k == "") ; poll
    {
        q:=RegExReplace(A_ThisHotkey, "\*\~\$(.*)", "$1")
        q:=RegExReplace(q, "\~\$(.*)", "$1")
        If q = BS
        {
            If (SubStr(s, 0) != "}")
                StringTrimRight, s, s, 1
        }
        Else If q in %d%
            s =
        Else
        {
            If q = Shift
            return
            Else If q = Space
                q := " "
            Else If q = Tab
                q := "`t"
            Else If q in Enter,Return,NumpadEnter
                q := "`n"
            Else If (RegExMatch(q, "Numpad(.+)", n))
            {
                q := n1 == "Div" ? "/" : n1 == "Mult" ? "*" : n1 == "Add" ? "+" : n1 == "Sub" ? "-" : n1 == "Dot" ? sd : ""
                If n1 is digit
                    q = %n1%
            }
            Else If (GetKeyState("Shift") ^ !GetKeyState("CapsLock", "T"))
                StringLower, q, q
            s .= q
        }
        Loop, Parse, t, `n ; check
        {
            StringSplit, x, A_LoopField, `r
            If (RegExMatch(s, x1 . "$", $)) ; match
            {
                StringLen, l, $
                StringTrimRight, s, s, l
                if !(x3~="i)\bNB\b")        ; if No Backspce "NB"
                    SendInput, {BS %l%}
                If (IsLabel(x2))
                    Gosub, %x2%
                Else
                {
                    Transform, x0, Deref, %x2%
                    Loop, Parse, f_, `,
                        StringReplace, x0, x0, %A_LoopField%, ¥%A_LoopField%¥, All
                    Loop, Parse, f_, `,
                        StringReplace, x0, x0, ¥%A_LoopField%¥, {%A_LoopField%}, All
                    Loop, Parse, f, `,
                        StringReplace, x0, x0, %A_LoopField%, {%A_LoopField%}, All
                    SendInput, %x0%
                }
            }
        }
        If (StrLen(s) > w)
            StringTrimLeft, s, s, w // 2
    }
    Else ; assert
    {
        StringReplace, k, k, `n, \n, All ; normalize
        StringReplace, k, k, `r, \r, All
        Loop, Parse, t, `n
        {
            l = %A_LoopField%
            If (SubStr(l, 1, InStr(l, "`r") - 1) == k)
                StringReplace, t, t, `n%l%
        }
        If a !=
            t = %t%`n%k%`r%a%`r%Options%
    }
    Return
    __hs: ; event
    hotstrings("", "", Options)
    Return
}

Re: Convert units as you type?

Posted: 04 Sep 2018, 02:24
by tryhard
I'm trying to do something similar.

I've tried adapting the code from trust_me (thanks!) to convert different units based on the measurement involved (i.e. liters, meters, etc.).
But I got stuck: I need to round the resulting converted number to 2 decimal places.
What am I doing wrong?

This works:

Code: Select all

hotstrings("(\d+\.?\d?+)( m )", "convertm") 

convertm:
p:=$1 * 3.28083989501
Send, %$1% m (%p% ft)
Return
But this doesn't:

Code: Select all

hotstrings("(\d+\.?\d?+)( m )", "convertm") 

convertm:
p:=$1 * 3.28083989501
r:= Round(%p%, 2)
Send, %$1% m (%r% ft)
Return
Any help would be greatly appreciated!

Re: Convert units as you type?

Posted: 04 Sep 2018, 02:33
by Guest
Remove the %% around variables when you use them in a function call, so it is Round(p, 2)

See https://autohotkey.com/docs/FAQ.htm#percent

Re: Convert units as you type?

Posted: 04 Sep 2018, 04:16
by tryhard
Oh man... I was sure I already tried that...
Thanks! That did the trick!

Re: Convert units as you type?

Posted: 30 Sep 2018, 10:39
by barkoosh
Thank you guys. I finally managed to add multiple units to convert. I also changed (\d+\.?\d?+) to (\d+\.?\d*) to allow for more than one decimal. But how to lose that small text window?

Thanks again.