This snippet is for those who type texts in different languages and have to switch keyboard layouts often. The examples below are for Hungarian, but the concept is valid for any other language.
I usually type lots of Hungarian texts for which I use the Hungarian keyboard layout. In order to type Hungarian characters the makers of the layouts had to replace some of the less-used characters with Hungarian ones.
For example, the character [ is used to type the Hungarian character ő.
This is problematic when I do some programming and have to type special characters like [ in regular expressions. For prgramming I use the English layout, but sometimes I forgot to swtich to it after letter writing and I type something else than what I wanted.
If that happens then I have to go back, delete the incorrect characters, switch the keyboard layout and type the text again. This is what this script does.
First you need to describe the mapping between the English and Hungarian characters:
Code:
eng_chars := ";:'""``~=+-_0)]}\|[{"
fileread, hun_chars, hunchars
The Hungarian characters need to be read from a file because Autohotkey cannot handle Unicode in the script. The file in my case contains this:
Code:
éÉáÁíÍóÓüÜöÖúÚűŰőŐ
For keyboard layout switching I use the right control key which also stores the current layout setting for the current window (it assumes the default layout is English):
Code:
RControl::
gosub langswitch
return
langswitch:
wingetclass class, A
Send, {altdown}{shift}{altup}
var = english%class%
if (%var% == "" or %var% == true)
%var% := false
else
%var% := true
Return
When I want to fix a piece of text then I select the faulty part (I don't think this step could be automated) and then press shift+right control which fixes characters in the selected part and switches the keyboard layout:
Code:
+RControl::
oldclip := clipboard
wingetclass class, A
var = english%class%
if (%var% == "" or %var% == true)
english := true
else
english := false
if (class == "Emacs")
SendInput {ctrldown}w{ctrlup}}
else
{
SendInput {ctrldown}c{ctrlup}
SendInput {delete}
}
transform, input, unicode
len := strlen(input)
i := 1
utf_prefix1 := 197 ; őű
utf_prefix2 := 195 ; others
loop
{
if (i > len)
break
char := substr(input, i, 1)
if (char == chr(utf_prefix1) or char == chr(utf_prefix2))
{
char := substr(input, i, 2)
i := i + 2
}
else
i := i + 1
if (english)
pos := instr(eng_chars, char)
else
pos := instr(hun_chars, char)
if (pos == 0)
sendinput % char
else
{
if (english)
transform clipboard, unicode, % substr(hun_chars, (pos * 2) - 1, 2)
else
clipboard := substr(eng_chars, (pos + 1) / 2, 1)
if (class == "Emacs")
sendinput {ctrldown}y{ctrlup}
else
sendinput {ctrldown}v{ctrlup}
}
}
clipboard := oldclip
gosub langswitch
return
Note that you may need to add more utf_prefix* variables if your language needs it.
That's it. It really makes my life on the computer easier. Sometimes the fixing of text doesn't work correctly, but I don't yet know why. If somebody can find it out then let me know.
