How to program a "hard space"

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
NimRinon
Posts: 5
Joined: 08 Mar 2023, 18:53

How to program a "hard space"

21 May 2024, 22:06

In my line of work, I need there to be a hard space (ctrl+shift+space) after people's titles (eg Ms, Dr, etc). I want a script which changes a normal space into a hard space, but only after the use of a title.

I've tried creating this code:

Code: Select all

::Mr{space}::
send,Mr{Shift down}{space}{shift up}
return
and varieties using ^! but so far none of them have had the desired effect.

Any ideas?


[Mod edit: Added [code][/code] tags. Please use them yourself when posting code!]
User avatar
boiler
Posts: 17298
Joined: 21 Dec 2014, 02:44

Re: How to program a "hard space"

21 May 2024, 22:18

@NimRinon — The moderators have added code tags to each your posts so far and have added comments asking you to do the same. Please don’t post code again without putting [code]code[/code] tags around it. Thank you.
User avatar
Datapoint
Posts: 303
Joined: 18 Mar 2018, 17:06

Re: How to program a "hard space"

21 May 2024, 22:43

Instead of the keyboard shortcut, maybe send the character itself. No-Break Space is U+00A0. So maybe:

Code: Select all

:B0C*:Mr. ::
Send % "{Backspace 1}" Chr(0x00A0)
return
NimRinon
Posts: 5
Joined: 08 Mar 2023, 18:53

Re: How to program a "hard space"

21 May 2024, 23:50

Datapoint wrote:
21 May 2024, 22:43
Instead of the keyboard shortcut, maybe send the character itself. No-Break Space is U+00A0. So maybe:

Code: Select all

:B0C*:Mr. ::
Send % "{Backspace 1}" Chr(0x00A0)
return
It worked! Thank you so much! :D
User avatar
CoffeeChaton
Posts: 37
Joined: 11 May 2024, 10:50

Re: How to program a "hard space"

22 May 2024, 00:07

Code: Select all

:B0C*O:Mr ::
    ;        ^ normal space
    ; B0 https://www.autohotkey.com/docs/v1/Hotstrings.htm#b0
    ; C https://www.autohotkey.com/docs/v1/Hotstrings.htm#C
    ; * https://www.autohotkey.com/docs/v1/Hotstrings.htm#Asterisk
    ; O https://www.autohotkey.com/docs/v1/Hotstrings.htm#O
    Send, % Chr(0x00A0)
return

replace normal as chr(0x00A0) ?
Rohwedder
Posts: 7736
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to program a "hard space"

22 May 2024, 01:36

Hallo,
or:

Code: Select all

:B0C*:Mr ::{Bs}{U+A0}
NimRinon
Posts: 5
Joined: 08 Mar 2023, 18:53

Re: How to program a "hard space"

23 May 2024, 01:33

I have a question related to this, because it involves a hard space but r after any number.

So for example, "I've worked here 45 years", there would be a hard space after 45. I know that if I'm searching a Word document with ctrl+F, there are codes I can use for "any number" or "any letter", is there something like this for AHK?
Rohwedder
Posts: 7736
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to program a "hard space"

23 May 2024, 03:33

See RegEx Dynamic Hotstrings

Code: Select all

hotstrings("(\d+) ", "%$1%" Chr(0xA0))
hotstrings("([a-zA-Z]{11,})\s", "Fuck! ")
Return ; end of auto-execute section
/*
    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(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
}
A group of 1 or more digits followed by a space is replaced by mentioned group followed by a no-break space.
Any word with more than 10 letters, i.e. too long for US Marines, is replaced.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: a_riva, MSN [Bot], peter_ahk and 195 guests