Autohotkey: send 5-digit Unicode hexadecimal characters

I'm trying to find a way to reassign the keyboard and send five-digit Unicode hexadecimal characters, the method described here: ahk Send only supports 4-digit hexadecimal codes {U + nnnn}, I know that in the past autohotkey did not support unicode initially, so some were needed for this functions, maybe this is the solution for me.

Example:

#If GetKeyState("CapsLock","T")
+u::Send {U+1D4B0}

The results are from ν’° instead of 𝒰, and the code for ν’° is {U + D4B0}, which means that AHK reads only the last 4 digits. How can I fix this, even if I need to make new functions to achieve this?

thank

-Mark

+4
source share
4 answers

Unicode values ​​greater than 0xFFFF must be encoded as two pairs of surrogates:

+u:: SendInput ,{U+D835}{U+DCB0}

Here is the Unicode code point conversion algorithm, ranging from 0x10000 to 0x10FFFF, to surrogate pairs paraphrased from wikipedia :

First subtract 0x10000 from the code point to get a number in the range 0xFFFFF.

Then shift the right number by 10 bits and add 0xD800 to it to get a high surrogate.

Take the low ten bits of a number and add 0xDC00 to it to get a low surrogate

+3
source

I took the 2501 solution from above and turned it into a working ahk script. I have been looking for this solution for several months!

+u:: FUNC_SEND_UNICODE( 0x1D4B0 )

FUNC_SEND_UNICODE(func_args)
{

    /*
    ;commented out proof of concept code:
    str := ""
    u1  := Chr( 0xD835 )
    u2  := Chr( 0xDCB0 )
    str := u1 . u2

    bk := clipboard
    clipboard := str
    send, ^v

    sleep,200
    clipboard := bk
    */

    ;bk == "clipboard [B]ac[K]up
    bk := clipboard


    ;chunk of data that needs to be cut into
    ;two chunks.
    ful := func_args + 0


    /*  commented out testing code, using original
        sample input of Qaru post
    ;msgbox,ful:[%ful%]
    ;for testing. Expecting input to be equal to
    ;the value in the post.
    if(ful != 0x1D4B0 ){
        msgbox,"[WHATTHEHECK]"
    }
    */


    ;Subtract 0x10000 from ful, gets number 
    ;in range(rng) 0x0 to 0xFFFFFF inclusive
    rng := ful - 0x10000
    if(rng > 0xFFFFF)
    {
        msgBox,[Step1 Resulted In Out Of Range]
    }

    ;Do shifting and masking, then check to make
    ;sure the value is in the expected range:
    big_shif := (rng >>   10)
    lit_mask := (rng & 0x3FF)
    if(big_shif > 0x3FF)
    {
        msgBox,[MATH_ERROR:big_shif]
    }
    if(lit_mask > 0x3FF)
    {
        msgBox,[MATH_ERROR:lit_mask]
    }

    big := big_shif + 0xD800
    lit := lit_mask + 0xDC00
    if(big < 0xD800 || big >= 0xDBFF){
        msgBox, [HIGH_SURROGATE_OUT_OF_BOUNDS]
    }
    if(lit < 0xDC00 || lit >= 0xDFFF){
        msgBox, [LOW_SURROGATE_OUT_OF_BOUNDS]
    }

    ;Convert code points to actual characters:
    u1 := Chr( big )
    u2 := Chr( lit )

    ;concatentate those two characters to
    ;create our final surrogate output:
    str := u1 . u2

    ;set it equal to clipboard, and send
    ;the clipboard. This is a hack.
    ;send,%str% works fine in google chrome,
    ;but fails in notepad++ for some reason.
    ;tried appending EOF, STX, ETX control codes
    ;along with output, but to no effect.
    clipboard := str
    send, ^v

    ;Must sleep before restoring clipboard,
    ;otherwise, the clipboard will get
    ;overwritten before ctrl+v has the chance
    ;to output the character. You'll end up just
    ;pasting whatever was originally on the
    ;clipboard.
    sleep,200
    clipboard := bk

    return
}
0
source

2501 .

  1. ;u
  2. (, ; Return)
  3. , 1 D 4 B 0
  4. , ;
  5. Unicode , 65536,
  6. Unicode

:?:';u::
    Input, keys, , ';
    if (StrLen(keys) < 5)
        Send {U+%keys%}
    else {
        keys := "0x" + keys
        num := keys - 0x10000
        w1 := Format("{:x}", (num >> 10) + 0xD800)
        w2 := Format("{:x}", (num & 1023) + 0xDC00)
        Send {U+%w1%}{U+%w2%}
    }
    return

:

0

- : sendunicodechar(0x1D4B0)

SendUnicodeChar(charCode)
{
    VarSetCapacity(ki, 28 * 2, 0)
    EncodeInteger(&ki + 0, 1)
    EncodeInteger(&ki + 6, charCode)
    EncodeInteger(&ki + 8, 4)
    EncodeInteger(&ki +28, 1)
    EncodeInteger(&ki +34, charCode)
    EncodeInteger(&ki +36, 4|2)

    DllCall("SendInput", "UInt", 2, "UInt", &ki, "Int", 28)
}

EncodeInteger(ref, val)
{
    DllCall("ntdll\RtlFillMemoryUlong", "Uint", ref, "Uint", 4, "Uint", val)
}

. , downvoted bc . , . , .

-1

All Articles