30

I am trying to make two hyphens trigger a short dash, and three hyphens trigger a long dash; like

:*?:---=::—
:*?:--=::–

except working. Here is where I have gotten to:

:*?:11::

SendLevel 1

Send 2 

return



SendLevel 0

:*?:21::3 

this works (11 produces 2, 111 produces 3)

:*?:--::

SendLevel 1

Send –

return



SendLevel 0

:*?:–-::—

This is the same script, but ‘1’ has been replaced by ‘-’, ‘2’ has been replaced by ‘–’, and ‘3’ has been replaced by ‘—’—“--” should produce “–” and “---” should produce “—”, but it doesn't work because Unicode is not fully supported in the version I am using (AutoHotKey_L v1.1.09.04 from autohotkey.com).

Eamon Moloney
  • 1,723
  • 4
  • 18
  • 22

4 Answers4

60

The .ahk text file needed to be saved with UTF8-BOM encoding rather than UTF8

As pointed out in this comment, posting as an answer for more visibility.

Community
  • 1
  • 1
laggingreflex
  • 26,275
  • 28
  • 123
  • 172
  • 2
    The best answer, from my point of view. Also, this is by design. Proof-quote ([source](https://www.autohotkey.com/docs/AHKL_ChangeLog.htm#v1.1.08.00)): "Changed the default script codepage to ANSI, as the previous behaviour was a common source of confusion. UTF-8 files must now have a byte order mark (BOM) to be recognized correctly." – john c. j. Apr 20 '17 at 03:38
  • 1
    Thanks a lot! Saved my day. I fed up with AHK "codepage" that does not provite any result to auto-replace. – Dimiano Jun 26 '17 at 13:00
  • How can the file encoding be changed in Notepad++? – Stevoisiak Sep 07 '17 at 13:33
  • 3
    @StevenVascellaro in Notepad++ open the menu labeled "Encoding", then select the desired encoding option. – Ro Yo Mi Sep 12 '17 at 16:35
  • 1
    If you're using regular Microsoft Notepad, then `UTF8-BOM` doesn't show up as an encoding option. Using `Unicode` format worked for me. – Kes Perron Feb 07 '18 at 14:21
  • @SMPerron `Unicode` is UTF-16. In most Notepad versions, `UTF-8` is UTF-8 with BOM. Windows 10 version 1903 makes the encoding names more accurate, with `UTF-8` being without BOM, and the new default. AutoHotkey still needs the BOM, however. – Lexikos Sep 27 '19 at 22:27
3

Click Save as, and change the encoding scheme as shown below:

enter image description here

Sebastian Nielsen
  • 2,268
  • 2
  • 13
  • 30
  • This answer adds to that of https://stackoverflow.com/a/39379877/7123519 by explicitly explaining how to change the encoding of a file. – Sebastian Nielsen Jun 13 '20 at 12:05
1

I copied the code below from the AutoHotKey forums:

;IMPORTANT, you must save this script as UTF-8 to make it work.

::!?::
::?!::
PutUni("‽")
Return

::neko::
PutUni("猫")
Return

:?:damn::
PutUni("✩☠#‼")
Return

;Paste UTF8 string (Hex encoded or not) as unicode.
;If you don't use Hex encoding, you must save your script as UTF8
PutUni(DataIn)
{
    SavedClip := ClipBoardAll
    ClipBoard =
    If RegExMatch(DataIn, "^[0-9a-fA-F]+$")
    {
        Loop % StrLen(DataIn) / 2
        UTF8Code .= Chr("0x" . SubStr(DataIn, A_Index * 2 - 1, 2))
    }
    Else
        UTF8Code := DataIn

    Transform, ClipBoard, Unicode, %UTF8Code%
    Send ^v
    Sleep 100 ;Generous, less wait or none will often work.
    ClipBoard := SavedClip
    Return
}

The PutUni function will "translate" the desired input to the desired Unicode output.

John Willemse
  • 6,386
  • 7
  • 28
  • 44
  • 8
    I just used the `Send` command, but it worked only after I changed the encoding from UTF8 to UTF8+BOM. – berezovskyi Dec 24 '14 at 18:53
  • I usually use the [ishida Unicode converter](https://r12a.github.io/app-conversion/), paste at top, get the relevant result in `JS/Java/C`, with minor change of surroundings brackets and leading zero, i.e. `\u{3B3}`→`Send {U+03BF}` – Frank Nocke Apr 05 '20 at 15:30
  • 06-13-2020: Doesn't work. Error: "Parameter #2 invalid. Specifically: Unicode." https://gyazo.com/09218ffc4171756817df9546890b6062 And, yes, I did save the file in UTF-8. – Sebastian Nielsen Jun 13 '20 at 11:45
1

EDIT: don't bother reading my answer, follow Udo Klein instruction, it's much easier and it works as it should.

how to send unicode characters using the last autohotkey version? (no need of previous unicode-compatible version to work)

Quite hard to find clear information. So to makes it clear for the beginners (like me):

  1. copy/past the "code A" that at the end of your script (it should be in encoded in ANSI)
  2. copy/past the "code B" on the top of your script
  3. find your unicode character code here http://www.utf8-chartable.de/unicode-utf8-table.pl
  4. copy the 4 figures after the U+
  5. in the code B (on the top of your script): change the key you need (before the "::")
  6. in the code B (on the top of your script): past the unicode you found on 2. AFTER the 0x (instead of the "2260")
  7. save your script
  8. double click on the icon of your script, it will replace/update the previous version

Code A:

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)}

Code B:

!+^D::  ; when press CTRL+ALT+SHIFT will output "≠"
{ 
SendUnicodeChar(0x2260) 
}
return

(watch the space!)

Improvement needed:

somehow this script doesn't work for all the unicode given by this website http://unicode-table.com/ , but someone will maybe be kind enough to tell us why some unicode are working and other don't, and maybe how to make it work for any unicode characters of this website. This one for instance http://unicode-table.com/en/0609/ doesn't work. Any idea why?

JinSnow
  • 1,171
  • 4
  • 23
  • 45