Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Easy Unicode functions


  • Please log in to reply
12 replies to this topic
ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007
I "discovered" a really easy way to send (paste) Unicode characters from your script. I don't think I've seen this suggested on the forum before, but it's so simple I'm not sure why not. It says right in the manual, that Transform, Unicode accepts literal UTF-8 strings, so all you have to do is save your script in that format and you can embed any character in your script in human-readable form.

;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
}
There is one little catch though. If you save your script in UTF-8, all special characters need to be passed through this function. You can't do something like this anymore:
::(r)::®
You'd have to do this:
::(r)::
PutUni("®")
Return

See also: AutoHotkey_L version.

animeaime
  • Members
  • 1045 posts
  • Last active: Jun 18 2011 04:44 AM
  • Joined: 04 Nov 2008
Works great. One question, what text editor do you use? Both PSPad and Notepad2 don't show the characters correctly (but they are pasted fine). In my browser, I see them correctly though.
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.

ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007

Works great. One question, what text editor do you use? Both PSPad and Notepad2 don't show the characters correctly (but they are pasted fine). In my browser, I see them correctly though.

You're right about that, it doesn't depend on the editor so much as the font though. Any editor that supports UTF-8 should work, but many fonts don't have very complete unicode support.

Krogdor
  • Members
  • 1391 posts
  • Last active: Jun 08 2011 05:31 AM
  • Joined: 18 Apr 2008
Just to let you know, AutoHotkey_L (Lexikos' build) supports sending of unicode characters through
Send, {U+XXXX}
where XXXX is the 4-digit hex code to the character.

ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007

Just to let you know, AutoHotkey_L (Lexikos' build) supports sending of unicode characters through

Send, {U+XXXX}
where XXXX is the 4-digit hex code to the character.

That's fine, but you need to look up the U+XXXX that way. This way you can just put in the literal characters, and saving in UTF-8 takes care of the rest. Of course I'm not claiming this is the be-all and end-all of Unicode methods, but it is easy, like I said in the subject.

ManaUser
  • Members
  • 1121 posts
  • Last active: Dec 07 2016 04:24 PM
  • Joined: 24 May 2007
I thought I'd try making another function with the same premise, only using AutoHotkey_L's Send {U+XXXX} feature.
;IMPORTANT, you must save this script as UTF-8 to make it work.
;IMPORTANT, this script requires AutoHotkey_L

F1::SendUTF8("Смотри я отправки текста на русском языке!", 1)
F2::SendUTF8("^fΩ") ;Find an Omega

SendUTF8(Data, Raw = 0)
{
   SaveFormat := A_FormatInteger
   SetFormat IntegerFast, H
   Loop Parse, Data
   {
      If (BytesLeft > 0)
      {
         CodePoint := (CodePoint << 6) + (Asc(A_LoopField) & 63)
         If (--BytesLeft = 0)
            OutPut .= "{U+" SubStr(CodePoint, 3) "}"
      }
      Else
      {
         If Asc(A_LoopField) < 192
            If (Raw AND InStr("!#+^{}", A_LoopField))
               OutPut .= "{" A_LoopField "}"
            Else
               OutPut .= A_LoopField
         Else If Asc(A_LoopField) < 224
            CodePoint := Asc(A_LoopField) & 31, BytesLeft := 1
         Else If Asc(A_LoopField) < 240
            CodePoint := Asc(A_LoopField) & 15, BytesLeft := 2
         Else
            CodePoint := Asc(A_LoopField) & 7, BytesLeft := 3
      }
   }
   Send %OutPut%
   SetFormat IntegerFast, %SaveFormat%
}


  • Guests
  • Last active:
  • Joined: --
You ROCK
God bless you talented friend
I pray for your long life and Good health!!

this is the unicode solution that i needed :D

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
ManaUser solution rocks. Messing with clipboard is never good. I use CLCL to record every clipboard change.
Posted Image

losang
  • Guests
  • Last active:
  • Joined: --
thanks Manauser! been searching high and low for this solution!

! 4rtist.com#&#9472;&
  • Members
  • 1 posts
  • Last active: Aug 01 2011 11:43 AM
  • Joined: 04 Nov 2009
6REAT scipt

is there a way out to set up linebrakes?

`r doesnt work

﴾͡๏̯͡๏﴿

Montu
  • Members
  • 142 posts
  • Last active: Jan 01 2010 03:10 AM
  • Joined: 11 Feb 2009
forget all this
simply use the newly built UNICODE version of AutoHotkey

unknowingly
  • Members
  • 63 posts
  • Last active: Apr 16 2010 06:08 AM
  • Joined: 21 Nov 2009
Hey manauser,
(the clipboard version works for me, however only sometimes, other times i have to reload the script - it is very buggy for me)
thanks for your version - just wondering though, it doesn't work for me, instead it just replaces my hotstring with nothing.
I am running windows 7 ultimate x64.
Cheers

AmourSpirit
  • Members
  • 93 posts
  • Last active: Mar 01 2016 04:28 PM
  • Joined: 01 Dec 2010

I found another post about this topic. I tried the code and it had a few errors. I can't find the other posting right now so I will post it here as it relates.

I have made corrections and tested. Seems to be working fine.

String=English,Как красивый жопа,你很性感,ทรวงอก

Loop, Parse, String, CSV
{
	HexInfo:= Text2Hex(A_LoopField)
	TxtInfo:= Hex2Text(HexInfo)
	Clipboard := HexInfo
	MsgBox, % "Text:`n" A_LoopField "`n`nHex:`n" HexInfo "`n`nText again:`n" TxtInfo "`n`nLenght:`n" StrLen(A_LoopField)
}
Return

Text2Hex(String) {
	VarSetCapacity(Hex, len*(A_IsUnicode ? 2:1))
	len := StrPutVar(String, Var, "UTF-16")
	pointer:=&var
	f := A_FormatInteger
	SetFormat IntegerFast, H
	Loop % len*2
	{		
		Hex := SubStr(0x100 + NumGet(pointer + A_Index- 0x1, 0x0, "uchar"), -0x1) . Hex
	}
		
	SetFormat IntegerFast, %f%
	Return Hex
}

Hex2Text(Hex) {
	f := A_FormatInteger
	SetFormat IntegerFast, D
	startpos:=1
	n := ""
	Loop % StrLen(Hex)/2
	{
		strHex := "0x" . SubStr(Hex, StartPos , 2) . SubStr(Hex, StartPos + 2 , 2)
		if (strHex = "0x") {
			break
		}
		n .= chr(strHex)
		startpos +=4
	}
	SetFormat IntegerFast, %f%
	Return n
}
	
	

StrPutVar(String, ByRef Var, encoding) {
	VarSetCapacity(Var, StrPut(String, encoding) * ((encoding="UTF-16"||encoding="CP1200") ? 2 : 1))
	Return StrPut(String, &var, encoding)
}