Upgrade startup.ahk for AutoHotkey v2.0

Upgrade startup.ahk for AutoHotkey v2.0

#Warn
SetWorkingDir A_ScriptDir
; TODO... Menu, Tray, Icon, D:\Miscellaneous\Icons\classic_MyAHKScript.ico

; If (!GetKeyState("NumLock", "T")) {
; 	SendEvent {NumLock}
; }
capslock_count := 0

last_clipboard := ""

on_exit(ExitReason, ExitCode) {
	If ExitReason == "Logoff" or ExitReason == "Shutdown" {
		SetNumLockState false
		SetScrollLockState false
	}
}
OnExit on_exit

title := "Symbol Palette"
key_width4 := 48
key_height := 48

key_width5 := key_width4 * 1.25
key_width6 := key_width4 * 1.5
key_width7 := key_width4 * 1.75
key_width8 := key_width4 * 2
key_width9 := key_width4 * 2.25
key_width11 := key_width4 * 2.75
key_width25 := key_width4 * 6.25

keyboard_gui := Gui(, title)
keyboard_gui.Opt("+AlwaysOnTop -Caption +Owner -Theme")
hwnd := keyboard_gui.Hwnd
keyboard_gui.MarginX := 0
keyboard_gui.MarginY := 0
keyboard_gui.SetFont("s18", "M+ 1c")
vkey_buttons1 := keyboard_gui.AddButton("section x0 y0 w" . key_width4 . " h" . key_height . " vkey_button1").OnEvent("Click", button_handler)
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button2,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button3,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button4,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button5,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button6,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button7,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button8,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button9,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button10,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button11,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button12,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button13,
;Gui, Add, Button,         x+0 ys w%key_width8%  h%key_height% gbutton_handler vkey_button14,
;Gui, Add, Button, section x0 y+0 w%key_width6%  h%key_height% gbutton_handler vkey_button15,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button16,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button17,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button18,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button19,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button20,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button21,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button22,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button23,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button24,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button25,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button26,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button27,
;Gui, Add, Button,         x+0 ys w%key_width6%  h%key_height% gbutton_handler vkey_button28,
;Gui, Add, Button, section x0 y+0 w%key_width7%  h%key_height% gbutton_handler vkey_button29,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button30,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button31,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button32,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button33,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button34,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button35,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button36,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button37,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button38,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button39,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button40,
;Gui, Add, Button,         x+0 ys w%key_width9%  h%key_height% gbutton_handler vkey_button41,
;Gui, Add, Button, section x0 y+0 w%key_width9%  h%key_height% gbutton_handler vkey_button42,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button43,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button44,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button45,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button46,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button47,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button48,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button49,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button50,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button51,
;Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button52,
;Gui, Add, Button,         x+0 ys w%key_width11% h%key_height% gbutton_handler vkey_button53,
;Gui, Add, Button, section x0 y+0 w%key_width5%  h%key_height% gbutton_handler vkey_button54,
;Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button55,
;Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button56,
;Gui, Add, Button,         x+0 ys w%key_width25% h%key_height% gbutton_handler vkey_button57,
;Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button58,
;Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button59,
;Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button60,
;Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button61,
keyboard_gui.Show("xCenter y" . key_height . " Hide")
;SatgosSymbolPalette
;WinSet, AlwaysOnTop, On, ahk_id %hwnd%

; Disable Win key
; Assigning the modifier key to be a “prefix” for at least one other hotkey retains the modifiying function.
LWin:: {
	show_symbol_palette([])
}
RWin:: {
	show_symbol_palette([])
}
LWin & CapsLock:: {
}
RWin & CapsLock:: {
}

CapsLock:: {
	global capslock_count
	capslock_count++
	If (capslock_count > 1) {
		Return
	}
	If (GetKeyState("CapsLock", "T")) {
		SetCapsLockState false
	} Else {
		toggle_ime_convmode()
		;KeyWait, %A_ThisHotkey%, T0.5
		;If (ErrorLevel) {
			;toggle_ime_convmode()
			;SetCapsLockState On
		;}
	}
}

CapsLock Up:: {
	global capslock_count
	capslock_count := 0
}

multi_tap(characters) {
	static count
	If (StrLen(characters) <= 1) {
		count := 1
		SendText characters
	} Else {
		send_string := ""
		If (A_PriorHotkey = A_ThisHotkey && A_TimeSincePriorHotkey < 500) {
			count += 1
			If (count > StrLen(characters)) {
				count := 1
			}
			send_string .= "{Backspace}"
		} Else {
			count := 1
		}
		send_string .= SubStr(characters, count, 1)
		SendText send_string
	}
}

<#+a::multi_tap("ĀÁǍÀÂÄⱭ")
<#a:: multi_tap("āáǎàâäɑ")
<#+e::multi_tap("ĒÉĚÈÊË")
<#e:: multi_tap("ēéěèêë")
<#+i::multi_tap("ĪÍǏÌÎÏ")
<#i:: multi_tap("īíǐìîï")
<#+o::multi_tap("ŌÓǑÒÔÖ")
<#o:: multi_tap("ōóǒòôö")
<#+u::multi_tap("ŪÚǓÙÛ")
<#u:: multi_tap("ūúǔùû")
<#+v::multi_tap("ÜǕǗǙǛ")
<#v:: multi_tap("üǖǘǚǜ")
<#+z::multi_tap("Ẑ")
<#z:: multi_tap("ẑ")
<#+c::multi_tap("ÇĈ")
<#c:: multi_tap("çĉ")
; <#+s::multi_tap("Ŝ")
; <#s:: multi_tap("ŝ")
<#+m::multi_tap("Ḿ")
<#m:: multi_tap("ḿ")
<#+n::multi_tap("Ǹ")
<#n:: multi_tap("ǹ")

>!+a::Send "Α"
>!+b::Send "Β"
>!+c::Send "Ψ"
>!+d::Send "Δ"
>!+e::Send "Ε"
>!+f::Send "Φ"
>!+g::Send "Γ"
>!+h::Send "Η"
>!+i::Send "Ι"
>!+j::Send "Ξ"
>!+k::Send "Κ"
>!+l::Send "Λ"
>!+m::Send "Μ"
>!+n::Send "Ν"
>!+o::Send "Ο"
>!+p::Send "Π"
>!+q::Return
>!+r::Send "Ρ"
>!+s::Send "Σ"
>!+t::Send "Τ"
>!+u::Send "Θ"
>!+v::Send "Ω"
>!+w::Return
>!+x::Send "Χ"
>!+y::Send "Υ"
>!+z::Send "Ζ"

>!a::Send "α"
>!b::Send "β"
>!c::Send "ψ"
>!d::Send "δ"
>!e::Send "ε"
>!f::Send "φ"
>!g::Send "γ"
>!h::Send "η"
>!i::Send "ι"
>!j::Send "ξ"
>!k::Send "κ"
>!l::Send "λ"
>!m::Send "μ"
>!n::Send "ν"
>!o::Send "ο"
>!p::Send "π"
>!q::Return
>!r::Send "ρ"
>!s::Send "σ"
>!t::Send "τ"
>!u::Send "θ"
>!v::Send "ω"
>!w::Send "ς"
>!x::Send "χ"
>!y::Send "υ"
>!z::Send "ζ"

#NumpadSub::Send "−"
#HotIf WinActive("ahk_exe LyX.exe")
#NumpadAdd::Send "\pm{Space}"
#NumpadMult::Send "\times{Space}"
#NumpadDiv::Send "\div{Space}"
#-::Send "--"
#,::Send "\le{Space}"
#+,::Send "\leqslant{Space}"
#.::Send "\ge{Space}"
#+.::Send "\geqslant{Space}"
^1::Send "{^}2{Right}"
^2::Send "\sqrt{Space}"
^/::Send "!mf"
^+/::Send "^+{Left}!mf{Down}"
#HotIf WinActive("ahk_exe Mathematica.exe")
^1::Send "^62{Right}"
#HotIf
#NumpadAdd::Send "±"
#NumpadMult::Send "×"
#NumpadDiv::Send "÷"
#-::Send "–"
#,::Send "≤"
#+,::Send "⩽"
#.::Send "≥"
#+.::Send "⩾"

#HotIf WinActive("ahk_exe winword.exe")
#^0::
#^1::
#^2::
#^3::
#^4::
#^5::
#^6::
#^7::
#^8::
#^9:: {
	c := SubStr(A_ThisHotkey, 3, 1)
	Send "^=" . c . "^="
}

#+0::
#+1::
#+2::
#+3::
#+4::
#+5::
#+6::
#+7::
#+8::
#+9:: {
	c := SubStr(A_ThisHotkey, 3, 1)
	Send "^+=" . c . "^+="
}

#HotIf
#^0::Send "₀"
#^1::Send "₁"
#^2::Send "₂"
#^3::Send "₃"
#^4::Send "₄"
#^5::Send "₅"
#^6::Send "₆"
#^7::Send "₇"
#^8::Send "₈"
#^9::Send "₉"

#+0::Send "⁰"
#+1::Send "¹"
#+2::Send "²"
#+3::Send "³"
#+4::Send "⁴"
#+5::Send "⁵"
#+6::Send "⁶"
#+7::Send "⁷"
#+8::Send "⁸"
#+9::Send "⁹"

#[::Send "〖"
#]::Send "〗"

#HotIf WinExist(keyboard_gui) && False
`::button_keydown(1)
` Up::button_keyup(1)
1::button_keydown(2)
1 Up::button_keyup(2)
2::button_keydown(3)
2 Up::button_keyup(3)
3::button_keydown(4)
3 Up::button_keyup(4)
4::button_keydown(5)
4 Up::button_keyup(5)
5::button_keydown(6)
5 Up::button_keyup(6)
6::button_keydown(7)
6 Up::button_keyup(7)
7::button_keydown(8)
7 Up::button_keyup(8)
8::button_keydown(9)
8 Up::button_keyup(9)
9::button_keydown(10)
9 Up::button_keyup(10)
0::button_keydown(11)
0 Up::button_keyup(11)
-::button_keydown(12)
VKBD Up::button_keyup(12)
=::button_keydown(13)
VKBB Up::button_keyup(13)
q::button_keydown(16)
q Up::button_keyup(16)
w::button_keydown(17)
w Up::button_keyup(17)
e::button_keydown(18)
e Up::button_keyup(18)
r::button_keydown(19)
r Up::button_keyup(19)
t::button_keydown(20)
t Up::button_keyup(20)
y::button_keydown(21)
y Up::button_keyup(21)
u::button_keydown(22)
u Up::button_keyup(22)
i::button_keydown(23)
i Up::button_keyup(23)
o::button_keydown(24)
o Up::button_keyup(24)
p::button_keydown(25)
p Up::button_keyup(25)
[::button_keydown(26)
[ Up::button_keyup(26)
]::button_keydown(27)
] Up::button_keyup(27)
\::button_keydown(28)
\ Up::button_keyup(28)
a::button_keydown(30)
a Up::button_keyup(30)
s::button_keydown(31)
s Up::button_keyup(31)
d::button_keydown(32)
d Up::button_keyup(32)
f::button_keydown(33)
f Up::button_keyup(33)
g::button_keydown(34)
g Up::button_keyup(34)
h::button_keydown(35)
h Up::button_keyup(35)
j::button_keydown(36)
j Up::button_keyup(36)
k::button_keydown(37)
k Up::button_keyup(37)
l::button_keydown(38)
l Up::button_keyup(38)
`;::button_keydown(39)
VKBA Up::button_keyup(39)
'::button_keydown(40)
VKDE Up::button_keyup(40)
z::button_keydown(43)
z Up::button_keyup(43)
x::button_keydown(44)
x Up::button_keyup(44)
c::button_keydown(45)
c Up::button_keyup(45)
v::button_keydown(46)
v Up::button_keyup(46)
b::button_keydown(47)
b Up::button_keyup(47)
n::button_keydown(48)
n Up::button_keyup(48)
m::button_keydown(49)
m Up::button_keyup(49)
,::button_keydown(50)
VKBC Up::button_keyup(50)
.::button_keydown(51)
VKBE Up::button_keyup(51)
/::button_keydown(52)
VKBF Up::button_keyup(52)
#HotIf

#F1:: {
	show_symbol_palette([""
		. "", "∣", "∥", "⟂", "", "", "", "", "", "", "", "∈", "∋", ""
		, "", "=", "<", ">", "≤", "≥", "≲", "≳", "≦", "≧", "⩽", "⩾", "≃", ""
		, "", "∼", "≺", "≻", "⪯", "⪰", "≼", "≽", "≾", "≿", "≅", "≆", ""
		, "", "≈", "⊂", "⊃", "⊆", "⊇", "⫇", "⫈", "⫅", "⫆", "⪋", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F1:: {
	show_symbol_palette([""
		. "", "∤", "∦", "⫽", "", "", "", "", "", "", "", "∉", "∌", ""
		, "", "≠", "≮", "≯", "≰", "≱", "≴", "≵", "", "", "", "", "", ""
		, "", "≁", "⊀", "⊁", "⪱", "⪲", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
#F2:: {
	show_symbol_palette([""
		. "", "¬", "∧", "∨", "⊕", "⊙", "⊼", "", "", "⊤", "⊥", "±", "∓", ""
		, "", "∁", "∩", "∪", "∖", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F2:: {
	show_symbol_palette([""
		. "", "¨", "¯", "<", "≤", "=", "≥", ">", "≠", "∨", "∧", "+", "×", ""
		, "", "?", "⍵", "⍷", "⍴", "∼", "↑", "↓", "⍳", "⍜", "⋆", "←", "→", ""
		, "", "⍺", "⌈", "⌊", "", "⍙", "⍢", "∘", "'", "⎕", "", "", ""
		, "", "⊂", "⊃", "∩", "∪", "⊥", "⊤", "∣", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
#F3:: {
	show_symbol_palette([""
		. "⅟", "½", "↉", "⅓", "⅔", "¼", "¾", "⅕", "⅖", "⅗", "⅘", "⅙", "⅚", ""
		, "", "∵", "∶", "°", "′", "″", "‴", "⁗", "", "", "", "", "", "∎"
		, "", "∴", "∷", "⩵", "≔", "", "", "", "", "", "", "", ""
		, "", "∠", "⋕", "⧣", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F3:: {
	show_symbol_palette([""
		. "⅐", "⅛", "⅜", "⅝", "⅞", "⅑", "⅒", "", "⁺", "⁽", "⁾", "⁻", "⁼", ""
		, "", "", "", "", "", "", "", "", "₊", "₍", "₎", "₋", "₌", ""
		, "", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "⁰", "⁄", ""
		, "", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉", "₀", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
#F4:: {
	show_symbol_palette([""
		. "", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "", "", ""
		, "", "", "ς", "ε", "ρ", "τ", "υ", "θ", "ι", "ο", "π", "", "", ""
		, "", "α", "σ", "δ", "φ", "γ", "η", "ξ", "κ", "λ", "", "", ""
		, "", "ζ", "χ", "ψ", "ω", "β", "ν", "μ", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F4:: {
	show_symbol_palette([""
		. "", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "", "", ""
		, "", "", "Σ", "Ε", "Ρ", "Τ", "Υ", "Θ", "Ι", "Ο", "Π", "", "", ""
		, "", "Α", "Σ", "Δ", "Φ", "Γ", "Η", "Ξ", "Κ", "Λ", "", "", ""
		, "", "Ζ", "Χ", "Ψ", "Ω", "Β", "Ν", "Μ", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
#F5:: {
	show_symbol_palette([""
		. "⓪", "①", "②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "⑩", "", "", ""
		, "", "⑪", "⑫", "⑬", "⑭", "⑮", "⑯", "⑰", "⑱", "⑲", "⑳", "", "", ""
		, "", "㉑", "㉒", "㉓", "㉔", "㉕", "㉖", "㉗", "㉘", "㉙", "㉚", "", ""
		, "", "㉛", "㉜", "㉝", "㉞", "㉟", "㊱", "㊲", "㊳", "㊴", "㊵", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F5:: {
	show_symbol_palette([""
		. "🄋", "➀", "➁", "➂", "➃", "➄", "➅", "➆", "➇", "➈", "➉", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "⓵", "⓶", "⓷", "⓸", "⓹", "⓺", "⓻", "⓼", "⓽", "⓾", "", ""
		, "", "㊶", "㊷", "㊸", "㊹", "㊺", "㊻", "㊼", "㊽", "㊾", "㊿", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
#F6:: {
	show_symbol_palette([""
		. "⓿", "❶", "❷", "❸", "❹", "❺", "❻", "❼", "❽", "❾", "❿", "", "", ""
		, "", "⓫", "⓬", "⓭", "⓮", "⓯", "⓰", "⓱", "⓲", "⓳", "⓴", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F6:: {
	show_symbol_palette([""
		. "🄌", "➊", "➋", "➌", "➍", "➎", "➏", "➐", "➑", "➒", "➓", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
#F7:: {
	show_symbol_palette([""
		. "●", "◎", "⊙", "○", "■", "◆", "★", "□", "◇", "☆", "", "", "", ""
		, "", "↖", "↑", "↗", "◤", "▲", "◥", "◸", "△", "◹", "░", "▒", "▓", "█"
		, "", "←", "↓", "→", "◀", "", "▶", "◁", "", "▷", "", "", ""
		, "", "↙", "↓", "↘", "◣", "▼", "◢", "◺", "▽", "◿", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F7:: {
	show_symbol_palette([""
		. "", "▁", "▂", "▃", "▄", "▅", "▅", "▆", "▇", "█", "", "", "", ""
		, "", "▘", "▔", "▝", "▛", "▀", "▜", "▉", "▊", "▋", "▌", "▍", "▎", "▏"
		, "", "▏", "▞", "▕", "▌", "█", "▐", "", "▚", "", "", "", ""
		, "", "▖", "▁", "▗", "▙", "▄", "▟", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
#F8:: {
	show_symbol_palette([""
		. "", "─", "│", "", "━", "┃", "", "═", "║", "", "", "", "", ""
		, "", "┌", "┬", "┐", "┏", "┳", "┓", "╔", "╦", "╗", "╭", "╮", "╳", "╲"
		, "", "├", "┼", "┤", "┣", "╋", "┫", "╠", "╬", "╣", "╰", "╯", ""
		, "", "└", "┴", "┘", "┗", "┻", "┛", "╚", "╩", "╝", "╱", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F8:: {
	show_symbol_palette([""
		. "", "⏜", "⏝", "⎴", "⎵", "⏞", "⏟", "⎪", "⎰", "", "", "", "", ""
		, "", "⎛", "⎞", "⎡", "⎤", "⎧", "⎫", "⌠", "⎱", "", "", "", "", ""
		, "", "⎜", "⎟", "⎢", "⎥", "⎨", "⎬", "⎮", "⎲", "", "", "", ""
		, "", "⎝", "⎠", "⎣", "⎦", "⎩", "⎭", "⌡", "⎳", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
#F9:: {
	show_symbol_palette([""
		. "🄀", "⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "", "", ""
		, "", "⒒", "⒓", "⒔", "⒕", "⒖", "⒗", "⒘", "⒙", "⒚", "⒛", "", "", ""
		, "", "⑴", "⑵", "⑶", "⑷", "⑸", "⑹", "⑺", "⑻", "⑼", "⑽", "", ""
		, "", "⑾", "⑿", "⒀", "⒁", "⒂", "⒃", "⒄", "⒅", "⒆", "⒇", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F9:: {
	show_symbol_palette([""
		. "‥", "⿰", "⿱", "⿲", "⿳", "⿴", "⿵", "⿶", "⿷", "⿸", "⿹", "⿺", "⿻", ""
		, "", "︻", "︵", "﹇", "︷", "﹃", "﹁", "︱", "￤", "︳", "︴", "‖", "︰", ""
		, "", "︼", "︶", "﹈", "︸", "﹄", "﹂", "￣", "﹉", "﹊", "﹋", "﹌", ""
		, "", "︹", "︺", "︽", "︾", "︿", "﹀", "", "", "﹍", "﹎", "﹏", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
#F10:: {
	show_symbol_palette([""
		. "", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ", ""
		, "", "ⅰ", "ⅱ", "ⅲ", "ⅳ", "ⅴ", "ⅵ", "ⅶ", "ⅷ", "ⅸ", "ⅹ", "ⅺ", "ⅻ", ""
		, "", "㈠", "㈡", "㈢", "㈣", "㈤", "㈥", "㈦", "㈧", "㈨", "㈩", "", ""
		, "", "㊀", "㊁", "㊂", "㊃", "㊄", "㊅", "㊆", "㊇", "㊈", "㊉", "",
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F10:: {
	show_symbol_palette([""
		. "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "⚀", "⚁", "⚂", "⚃", "⚄", "⚅", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "☰", "☱", "☲", "☳", "☴", "☵", "☶", "☷", "⚊", "⚋", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
#F11:: {
	show_symbol_palette([""
		. "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "§", "№", "℡", "〓", "℃", "℉", "ℓ", "", "", "", "♀", "♂", ""
		, "", "〃", "々", "〆", "〒", "※", "", "", "", "", "", "", ""
		, "", "•", "‣", "⁎", "⁑", "†", "‡", "⹋", "℮", "‧", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}
+#F11:: {
	show_symbol_palette([""
		. "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}

#F21:: {
	show_symbol_palette([""
		. "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
}

#F12:: {
	show_symbol_palette([""
		. "``", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "⌫"
		, "↹", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\"
		, "⇬", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "↵"
		, "⇧", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "⇧"
		, "^", "⌘", "⌥", " ", "⌥", "⌘", "❖", "^"
	. ""])
}
+#F12:: {
	show_symbol_palette([""
		. "``", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "⌫"
		, "↹", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\"
		, "⇬", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "↵"
		, "⇧", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "⇧"
		, "^", "⌘", "⌥", " ", "⌥", "⌘", "❖", "^"
	. ""])
}

button_keydown(number) {
	ControlClick "x1 y1", "key_button" . number, , , , "D"
	button_event(number)
}

button_keyup(number) {
	ControlClick "x1 y1", "key_button" . number, , , , "U"
}

button_handler(control, info) {
	button_event(SubStr(control.Name, 11))
}

button_event(number) {
	;GuiControlGet, symbol, , key_button%number%, Text
	Send "%"
}

show_symbol_palette(symbols) {
	global hwnd
	If (symbols.Length > 0) {
		For index, symbol in symbols {
			;GuiControlGet, button_enabled, Enabled, key_button%index%
			button_enabled := 0
			If (button_enabled) {
				;GuiControl, Text, key_button%index%, %symbol%
			}
		}
		keyboard_gui.Show("NA")
		;WinSet, Transparent, 192, ahk_id %hwnd%
	} Else {
		keyboard_gui.Hide()
	}
}

set_next_input_language() {
	WinExist("A")
	PostMessage 0x50, 4, , ControlGetFocus()
}

toggle_ime_convmode() {
	current_convmode := get_ime_convmode()
	; 默认为英文状态，则转换模式为268435456。
	If (current_convmode = 0 || current_convmode = 0x10000000) {
		set_ime_convmode(1)
	} Else {
		set_ime_convmode(0)
	}
}

set_ime_convmode(mode) {
	SendMessage 0x0283 ; Message = WM_IME_CONTROL
		, 0x0002 ; wParam = IMC_SETCONVERSIONMODE
		, mode ; lParam = CONVERSIONMODE
		, DllCall("imm32\ImmGetDefaultIMEWnd", "UInt", WinExist("A"))
}

get_ime_convmode() {
	Return SendMessage(0x0283 ; Message = WM_IME_CONTROL
		, 0x0001 ; wParam = IMC_GETCONVERSIONMODE
		, 0 ; lParam = 0
		, DllCall("imm32\ImmGetDefaultIMEWnd", "Uint", WinExist("A")))
}

text_to_tex(str) {
	; StrReplace不好用，因为它不知道区不区分大小写
	str := RegExReplace(str, "“", "````")
	str := RegExReplace(str, "”", "''")
	str := RegExReplace(str, "‘", "``")
	str := RegExReplace(str, "’", "'")
	str := RegExReplace(str, "\bLaTeX\b", "\LaTeX{}")
	str := RegExReplace(str, "TeX\b", "\TeX{}")
	str := RegExReplace(str, "\bTeXbook\b", "\TeX book")
	str := RegExReplace(str, "\bMETAFONT\b", "\MF{}")
	str := RegExReplace(str, "\bMETAFONTbook\b", "\MF book")
	str := RegExReplace(str, "−", "--")
	str := RegExReplace(str, "—", "---")
	str := RegExReplace(str, "i)\bPh\.\s?D\. ", "Ph.D.\ ")
	str := RegExReplace(str, "\b(Miss|Ms|Mrs?|cf|v\.?s)\.\s", "$1.~")
	str := RegExReplace(str, Chr(160), "~")
	str := RegExReplace(str, "%", "\%")
	str := RegExReplace(str, "i)(?:https?|s?ftps?|git|telnet)://[\w~!@#\$%\^&\*\(\)\[\]\{\}<>,\./\?=\+:;`"'-]{4,2083}", "\url{$0}")
	str := RegExReplace(str, "(\w)\s&\s(\w)", "$1 \& $2")
	str := RegExReplace(str, "$(\d)", "\$$$1")
	str := RegExReplace(str, "#(\d)", "\#$1")
	str := RegExReplace(str, "<em>(.+?)</em>", "\emph{$1}")
	Return str
}

XButton1::Return
XButton2::Return

Launch_App1::Return
Launch_App2::Return
Media_Next::Return
Media_Prev::Return
Media_Play_Pause::Return
Media_Stop:: {
	If ProcessExist("baidunetdisk.exe") + ProcessExist("uget.exe") + ProcessExist("uTorrent.exe") {
		result := MsgBox, 0x131, "睡眠", "还有下载任务。睡眠吗？"
	} Else {
		result := MsgBox, 0x1, "睡眠", "睡眠吗？"
	}
	If result == "OK" {
		; Parameter #1: Pass 1 instead of 0 to hibernate rather than suspend.
		; Parameter #2: Pass 1 instead of 0 to suspend immediately rather than asking each application for permission.
		; Parameter #3: Pass 1 instead of 0 to disable all wake events.
		DllCall("PowrProf\SetSuspendState", "Int", 0, "Int", 0, "Int", 0)
	}
}
Browser_Back::Send "{Volume_Down}"
Browser_Forward::Send "{Volume_Up}"
Browser_Home::Return
Launch_Mail:: {
	global last_clipboard
	If (last_clipboard == Clipboard) {
		ToolTip "已经转换过了。"
	} Else {
		last_clipboard := text_to_tex(Clipboard)
		Clipboard := last_clipboard
		ToolTip "将剪贴板文本转换为 LaTeX 格式。"
	}
	SetTimer RemoveToolTip, -1000
}

RemoveToolTip() {
	ToolTip
}

#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
Menu, Tray, Icon, D:\Miscellaneous\Icons\classic_MyAHKScript.ico

; If (!GetKeyState("NumLock", "T")) {
; 	SendEvent {NumLock}
; }
SetScrollLockState, On
capslock_count := 0

last_clipboard := ""

on_exit(ExitReason, ExitCode) {
	If ExitReason in Logoff,Shutdown
	{
		; SetNumLockState, Off
		SetScrollLockState, Off
	}
	Return 0
}
OnExit("on_exit")

title = Symbol Palette
key_width4 := 48
key_height := 48
font_family = M+ 1c
font_size := 18

key_width5 := key_width4 * 1.25
key_width6 := key_width4 * 1.5
key_width7 := key_width4 * 1.75
key_width8 := key_width4 * 2
key_width9 := key_width4 * 2.25
key_width11 := key_width4 * 2.75
key_width25 := key_width4 * 6.25

Gui, +AlwaysOnTop -Caption +Owner +Hwndhwnd -Theme
Gui, Margin, 0, 0
Gui, Font, s%font_size%, %font_family%
Gui, Add, Button, section x0 y0  w%key_width4%  h%key_height% gbutton_handler vkey_button1,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button2,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button3,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button4,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button5,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button6,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button7,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button8,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button9,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button10,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button11,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button12,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button13,
Gui, Add, Button,         x+0 ys w%key_width8%  h%key_height% gbutton_handler vkey_button14,
Gui, Add, Button, section x0 y+0 w%key_width6%  h%key_height% gbutton_handler vkey_button15,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button16,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button17,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button18,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button19,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button20,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button21,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button22,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button23,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button24,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button25,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button26,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button27,
Gui, Add, Button,         x+0 ys w%key_width6%  h%key_height% gbutton_handler vkey_button28,
Gui, Add, Button, section x0 y+0 w%key_width7%  h%key_height% gbutton_handler vkey_button29,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button30,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button31,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button32,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button33,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button34,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button35,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button36,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button37,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button38,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button39,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button40,
Gui, Add, Button,         x+0 ys w%key_width9%  h%key_height% gbutton_handler vkey_button41,
Gui, Add, Button, section x0 y+0 w%key_width9%  h%key_height% gbutton_handler vkey_button42,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button43,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button44,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button45,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button46,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button47,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button48,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button49,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button50,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button51,
Gui, Add, Button,         x+0 ys w%key_width4%  h%key_height% gbutton_handler vkey_button52,
Gui, Add, Button,         x+0 ys w%key_width11% h%key_height% gbutton_handler vkey_button53,
Gui, Add, Button, section x0 y+0 w%key_width5%  h%key_height% gbutton_handler vkey_button54,
Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button55,
Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button56,
Gui, Add, Button,         x+0 ys w%key_width25% h%key_height% gbutton_handler vkey_button57,
Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button58,
Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button59,
Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button60,
Gui, Add, Button,         x+0 ys w%key_width5%  h%key_height% gbutton_handler vkey_button61,
Gui, Show, xCenter y%key_height% Hide, SatgosSymbolPalette
WinSet, AlwaysOnTop, On, ahk_id %hwnd%

; Disable Win key
; Assigning the modifier key to be a “prefix” for at least one other hotkey retains the modifiying function.
LWin::
RWin::
	show_symbol_palette([])
	Return
LWin & CapsLock::
	Return
RWin & CapsLock::
	Return

CapsLock::
	capslock_count++
	If (capslock_count > 1) {
		Return
	}
	If (GetKeyState("CapsLock", "T")) {
		SetCapsLockState Off
	} Else {
		toggle_ime_convmode()
		;KeyWait, %A_ThisHotkey%, T0.5
		;If (ErrorLevel) {
			;toggle_ime_convmode()
			;SetCapsLockState On
		;}
	}
	Return

CapsLock Up::
	capslock_count := 0
	Return

multi_tap(characters) {
	static count
	If (StrLen(characters) <= 1) {
		count := 1
		Send {Text}%characters%
	} Else {
		send_string := ""
		If (A_PriorHotkey = A_ThisHotkey && A_TimeSincePriorHotkey < 500) {
			count += 1
			If (count > StrLen(characters)) {
				count := 1
			}
			send_string .= "{Backspace}"
		} Else {
			count := 1
		}
		send_string .= "{Text}" . SubStr(characters, count, 1)
		Send %send_string%
	}
}

<#+a::multi_tap("ĀÁǍÀÂÄⱭ")
<#a:: multi_tap("āáǎàâäɑ")
<#+e::multi_tap("ĒÉĚÈÊË")
<#e:: multi_tap("ēéěèêë")
<#+i::multi_tap("ĪÍǏÌÎÏ")
<#i:: multi_tap("īíǐìîï")
<#+o::multi_tap("ŌÓǑÒÔÖ")
<#o:: multi_tap("ōóǒòôö")
<#+u::multi_tap("ŪÚǓÙÛ")
<#u:: multi_tap("ūúǔùû")
<#+v::multi_tap("ÜǕǗǙǛ")
<#v:: multi_tap("üǖǘǚǜ")
<#+z::multi_tap("Ẑ")
<#z:: multi_tap("ẑ")
<#+c::multi_tap("ÇĈ")
<#c:: multi_tap("çĉ")
<#+s::multi_tap("Ŝ")
<#s:: multi_tap("ŝ")
<#+m::multi_tap("Ḿ")
<#m:: multi_tap("ḿ")
<#+n::multi_tap("Ǹ")
<#n:: multi_tap("ǹ")

>#+a::Send Α
>#+b::Send Β
>#+c::Send Ψ
>#+d::Send Δ
>#+e::Send Ε
>#+f::Send Φ
>#+g::Send Γ
>#+h::Send Η
>#+i::Send Ι
>#+j::Send Ξ
>#+k::Send Κ
>#+l::Send Λ
>#+m::Send Μ
>#+n::Send Ν
>#+o::Send Ο
>#+p::Send Π
>#+q::Return
>#+r::Send Ρ
>#+s::Send Σ
>#+t::Send Τ
>#+u::Send Θ
>#+v::Send Ω
>#+w::Return
>#+x::Send Χ
>#+y::Send Υ
>#+z::Send Ζ

>#a::Send α
>#b::Send β
>#c::Send ψ
>#d::Send δ
>#e::Send ε
>#f::Send φ
>#g::Send γ
>#h::Send η
>#i::Send ι
>#j::Send ξ
>#k::Send κ
>#l::Send λ
>#m::Send μ
>#n::Send ν
>#o::Send ο
>#p::Send π
>#q::Return
>#r::Send ρ
>#s::Send σ
>#t::Send τ
>#u::Send θ
>#v::Send ω
>#w::Send ς
>#x::Send χ
>#y::Send υ
>#z::Send ζ

#NumpadSub::Send −
#IfWinActive ahk_exe LyX.exe
#NumpadAdd::Send \pm{Space}
#NumpadMult::Send \times{Space}
#NumpadDiv::Send \div{Space}
#-::Send --
#,::Send \le{Space}
#+,::Send \leqslant{Space}
#.::Send \ge{Space}
#+.::Send \geqslant{Space}
^1::Send {^}2{Right}
^2::Send \sqrt{Space}
^/::Send !mf
^+/::Send ^+{Left}!mf{Down}
#IfWinActive ahk_exe Mathematica.exe
^1::Send ^62{Right}
#IfWinActive
#NumpadAdd::Send ±
#NumpadMult::Send ×
#NumpadDiv::Send ÷
#-::Send –
#,::Send ≤
#+,::Send ⩽
#.::Send ≥
#+.::Send ⩾

#IfWinActive ahk_exe winword.exe
#^0::
#^1::
#^2::
#^3::
#^4::
#^5::
#^6::
#^7::
#^8::
#^9::
	c := SubStr(A_ThisHotkey, 3, 1)
	Send % "^=" . c . "^="
	Return

#+0::
#+1::
#+2::
#+3::
#+4::
#+5::
#+6::
#+7::
#+8::
#+9::
	c := SubStr(A_ThisHotkey, 3, 1)
	Send % "^+=" . c . "^+="
	Return

#IfWinActive
#^0::Send ₀
#^1::Send ₁
#^2::Send ₂
#^3::Send ₃
#^4::Send ₄
#^5::Send ₅
#^6::Send ₆
#^7::Send ₇
#^8::Send ₈
#^9::Send ₉

#+0::Send ⁰
#+1::Send ¹
#+2::Send ²
#+3::Send ³
#+4::Send ⁴
#+5::Send ⁵
#+6::Send ⁶
#+7::Send ⁷
#+8::Send ⁸
#+9::Send ⁹

#[::Send 〖
#]::Send 〗

#IfWinExist SatgosSymbolPaletteShown
`::button_keydown(1)
` Up::button_keyup(1)
1::button_keydown(2)
1 Up::button_keyup(2)
2::button_keydown(3)
2 Up::button_keyup(3)
3::button_keydown(4)
3 Up::button_keyup(4)
4::button_keydown(5)
4 Up::button_keyup(5)
5::button_keydown(6)
5 Up::button_keyup(6)
6::button_keydown(7)
6 Up::button_keyup(7)
7::button_keydown(8)
7 Up::button_keyup(8)
8::button_keydown(9)
8 Up::button_keyup(9)
9::button_keydown(10)
9 Up::button_keyup(10)
0::button_keydown(11)
0 Up::button_keyup(11)
-::button_keydown(12)
VKBD Up::button_keyup(12)
=::button_keydown(13)
VKBB Up::button_keyup(13)
q::button_keydown(16)
q Up::button_keyup(16)
w::button_keydown(17)
w Up::button_keyup(17)
e::button_keydown(18)
e Up::button_keyup(18)
r::button_keydown(19)
r Up::button_keyup(19)
t::button_keydown(20)
t Up::button_keyup(20)
y::button_keydown(21)
y Up::button_keyup(21)
u::button_keydown(22)
u Up::button_keyup(22)
i::button_keydown(23)
i Up::button_keyup(23)
o::button_keydown(24)
o Up::button_keyup(24)
p::button_keydown(25)
p Up::button_keyup(25)
[::button_keydown(26)
[ Up::button_keyup(26)
]::button_keydown(27)
] Up::button_keyup(27)
\::button_keydown(28)
\ Up::button_keyup(28)
a::button_keydown(30)
a Up::button_keyup(30)
s::button_keydown(31)
s Up::button_keyup(31)
d::button_keydown(32)
d Up::button_keyup(32)
f::button_keydown(33)
f Up::button_keyup(33)
g::button_keydown(34)
g Up::button_keyup(34)
h::button_keydown(35)
h Up::button_keyup(35)
j::button_keydown(36)
j Up::button_keyup(36)
k::button_keydown(37)
k Up::button_keyup(37)
l::button_keydown(38)
l Up::button_keyup(38)
`;::button_keydown(39)
VKBA Up::button_keyup(39)
'::button_keydown(40)
VKDE Up::button_keyup(40)
z::button_keydown(43)
z Up::button_keyup(43)
x::button_keydown(44)
x Up::button_keyup(44)
c::button_keydown(45)
c Up::button_keyup(45)
v::button_keydown(46)
v Up::button_keyup(46)
b::button_keydown(47)
b Up::button_keyup(47)
n::button_keydown(48)
n Up::button_keyup(48)
m::button_keydown(49)
m Up::button_keyup(49)
,::button_keydown(50)
VKBC Up::button_keyup(50)
.::button_keydown(51)
VKBE Up::button_keyup(51)
/::button_keydown(52)
VKBF Up::button_keyup(52)
#IfWinExist

#F1::
	show_symbol_palette([""
		. "", "∣", "∥", "⟂", "", "", "", "", "", "", "", "∈", "∋", ""
		, "", "=", "<", ">", "≤", "≥", "≲", "≳", "≦", "≧", "⩽", "⩾", "≃", ""
		, "", "∼", "≺", "≻", "⪯", "⪰", "≼", "≽", "≾", "≿", "≅", "≆", ""
		, "", "≈", "⊂", "⊃", "⊆", "⊇", "⫇", "⫈", "⫅", "⫆", "⪋", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F1::
	show_symbol_palette([""
		. "", "∤", "∦", "⫽", "", "", "", "", "", "", "", "∉", "∌", ""
		, "", "≠", "≮", "≯", "≰", "≱", "≴", "≵", "", "", "", "", "", ""
		, "", "≁", "⊀", "⊁", "⪱", "⪲", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
#F2::
	show_symbol_palette([""
		. "", "¬", "∧", "∨", "⊕", "⊙", "⊼", "", "", "⊤", "⊥", "±", "∓", ""
		, "", "∁", "∩", "∪", "∖", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F2::
	show_symbol_palette([""
		. "", "¨", "¯", "<", "≤", "=", "≥", ">", "≠", "∨", "∧", "+", "×", ""
		, "", "?", "⍵", "⍷", "⍴", "∼", "↑", "↓", "⍳", "⍜", "⋆", "←", "→", ""
		, "", "⍺", "⌈", "⌊", "", "⍙", "⍢", "∘", "'", "⎕", "", "", ""
		, "", "⊂", "⊃", "∩", "∪", "⊥", "⊤", "∣", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
#F3::
	show_symbol_palette([""
		. "⅟", "½", "↉", "⅓", "⅔", "¼", "¾", "⅕", "⅖", "⅗", "⅘", "⅙", "⅚", ""
		, "", "∵", "∶", "°", "′", "″", "‴", "⁗", "", "", "", "", "", "∎"
		, "", "∴", "∷", "⩵", "≔", "", "", "", "", "", "", "", ""
		, "", "∠", "⋕", "⧣", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F3::
	show_symbol_palette([""
		. "⅐", "⅛", "⅜", "⅝", "⅞", "⅑", "⅒", "", "⁺", "⁽", "⁾", "⁻", "⁼", ""
		, "", "", "", "", "", "", "", "", "₊", "₍", "₎", "₋", "₌", ""
		, "", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", "⁰", "⁄", ""
		, "", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉", "₀", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
#F4::
	show_symbol_palette([""
		. "", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "", "", ""
		, "", "", "ς", "ε", "ρ", "τ", "υ", "θ", "ι", "ο", "π", "", "", ""
		, "", "α", "σ", "δ", "φ", "γ", "η", "ξ", "κ", "λ", "", "", ""
		, "", "ζ", "χ", "ψ", "ω", "β", "ν", "μ", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F4::
	show_symbol_palette([""
		. "", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "", "", ""
		, "", "", "Σ", "Ε", "Ρ", "Τ", "Υ", "Θ", "Ι", "Ο", "Π", "", "", ""
		, "", "Α", "Σ", "Δ", "Φ", "Γ", "Η", "Ξ", "Κ", "Λ", "", "", ""
		, "", "Ζ", "Χ", "Ψ", "Ω", "Β", "Ν", "Μ", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
#F5::
	show_symbol_palette([""
		. "⓪", "①", "②", "③", "④", "⑤", "⑥", "⑦", "⑧", "⑨", "⑩", "", "", ""
		, "", "⑪", "⑫", "⑬", "⑭", "⑮", "⑯", "⑰", "⑱", "⑲", "⑳", "", "", ""
		, "", "㉑", "㉒", "㉓", "㉔", "㉕", "㉖", "㉗", "㉘", "㉙", "㉚", "", ""
		, "", "㉛", "㉜", "㉝", "㉞", "㉟", "㊱", "㊲", "㊳", "㊴", "㊵", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F5::
	show_symbol_palette([""
		. "🄋", "➀", "➁", "➂", "➃", "➄", "➅", "➆", "➇", "➈", "➉", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "⓵", "⓶", "⓷", "⓸", "⓹", "⓺", "⓻", "⓼", "⓽", "⓾", "", ""
		, "", "㊶", "㊷", "㊸", "㊹", "㊺", "㊻", "㊼", "㊽", "㊾", "㊿", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
#F6::
	show_symbol_palette([""
		. "⓿", "❶", "❷", "❸", "❹", "❺", "❻", "❼", "❽", "❾", "❿", "", "", ""
		, "", "⓫", "⓬", "⓭", "⓮", "⓯", "⓰", "⓱", "⓲", "⓳", "⓴", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F6::
	show_symbol_palette([""
		. "🄌", "➊", "➋", "➌", "➍", "➎", "➏", "➐", "➑", "➒", "➓", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
#F7::
	show_symbol_palette([""
		. "●", "◎", "⊙", "○", "■", "◆", "★", "□", "◇", "☆", "", "", "", ""
		, "", "↖", "↑", "↗", "◤", "▲", "◥", "◸", "△", "◹", "░", "▒", "▓", "█"
		, "", "←", "↓", "→", "◀", "", "▶", "◁", "", "▷", "", "", ""
		, "", "↙", "↓", "↘", "◣", "▼", "◢", "◺", "▽", "◿", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F7::
	show_symbol_palette([""
		. "", "▁", "▂", "▃", "▄", "▅", "▅", "▆", "▇", "█", "", "", "", ""
		, "", "▘", "▔", "▝", "▛", "▀", "▜", "▉", "▊", "▋", "▌", "▍", "▎", "▏"
		, "", "▏", "▞", "▕", "▌", "█", "▐", "", "▚", "", "", "", ""
		, "", "▖", "▁", "▗", "▙", "▄", "▟", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
#F8::
	show_symbol_palette([""
		. "", "─", "│", "", "━", "┃", "", "═", "║", "", "", "", "", ""
		, "", "┌", "┬", "┐", "┏", "┳", "┓", "╔", "╦", "╗", "╭", "╮", "╳", "╲"
		, "", "├", "┼", "┤", "┣", "╋", "┫", "╠", "╬", "╣", "╰", "╯", ""
		, "", "└", "┴", "┘", "┗", "┻", "┛", "╚", "╩", "╝", "╱", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F8::
	show_symbol_palette([""
		. "", "⏜", "⏝", "⎴", "⎵", "⏞", "⏟", "⎪", "⎰", "", "", "", "", ""
		, "", "⎛", "⎞", "⎡", "⎤", "⎧", "⎫", "⌠", "⎱", "", "", "", "", ""
		, "", "⎜", "⎟", "⎢", "⎥", "⎨", "⎬", "⎮", "⎲", "", "", "", ""
		, "", "⎝", "⎠", "⎣", "⎦", "⎩", "⎭", "⌡", "⎳", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
#F9::
	show_symbol_palette([""
		. "🄀", "⒈", "⒉", "⒊", "⒋", "⒌", "⒍", "⒎", "⒏", "⒐", "⒑", "", "", ""
		, "", "⒒", "⒓", "⒔", "⒕", "⒖", "⒗", "⒘", "⒙", "⒚", "⒛", "", "", ""
		, "", "⑴", "⑵", "⑶", "⑷", "⑸", "⑹", "⑺", "⑻", "⑼", "⑽", "", ""
		, "", "⑾", "⑿", "⒀", "⒁", "⒂", "⒃", "⒄", "⒅", "⒆", "⒇", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F9::
	show_symbol_palette([""
		. "‥", "⿰", "⿱", "⿲", "⿳", "⿴", "⿵", "⿶", "⿷", "⿸", "⿹", "⿺", "⿻", ""
		, "", "︻", "︵", "﹇", "︷", "﹃", "﹁", "︱", "￤", "︳", "︴", "‖", "︰", ""
		, "", "︼", "︶", "﹈", "︸", "﹄", "﹂", "￣", "﹉", "﹊", "﹋", "﹌", ""
		, "", "︹", "︺", "︽", "︾", "︿", "﹀", "", "", "﹍", "﹎", "﹏", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
#F10::
	show_symbol_palette([""
		. "", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ", ""
		, "", "ⅰ", "ⅱ", "ⅲ", "ⅳ", "ⅴ", "ⅵ", "ⅶ", "ⅷ", "ⅸ", "ⅹ", "ⅺ", "ⅻ", ""
		, "", "㈠", "㈡", "㈢", "㈣", "㈤", "㈥", "㈦", "㈧", "㈨", "㈩", "", ""
		, "", "㊀", "㊁", "㊂", "㊃", "㊄", "㊅", "㊆", "㊇", "㊈", "㊉", "",
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F10::
	show_symbol_palette([""
		. "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "⚀", "⚁", "⚂", "⚃", "⚄", "⚅", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "☰", "☱", "☲", "☳", "☴", "☵", "☶", "☷", "⚊", "⚋", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
#F11::
	show_symbol_palette([""
		. "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "§", "№", "℡", "〓", "℃", "℉", "ℓ", "", "", "", "♀", "♂", ""
		, "", "〃", "々", "〆", "〒", "※", "", "", "", "", "", "", ""
		, "", "•", "‣", "⁎", "⁑", "†", "‡", "⹋", "℮", "‧", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return
+#F11::
	show_symbol_palette([""
		. "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return

#F21::
	show_symbol_palette([""
		. "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", "", "", "", "", "", "", "", ""
		, "", "", "", "", " ", "", "", ""
	. ""])
	Return

#F12::
	show_symbol_palette([""
		. "`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "⌫"
		, "↹", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "[", "]", "\"
		, "⇬", "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "↵"
		, "⇧", "z", "x", "c", "v", "b", "n", "m", ",", ".", "/", "⇧"
		, "^", "⌘", "⌥", " ", "⌥", "⌘", "❖", "^"
	. ""])
	Return
+#F12::
	show_symbol_palette([""
		. "`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "⌫"
		, "↹", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\"
		, "⇬", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "↵"
		, "⇧", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "⇧"
		, "^", "⌘", "⌥", " ", "⌥", "⌘", "❖", "^"
	. ""])
	Return

button_keydown(number) {
	ControlClick, X1 Y1, key_button%number%, , , , D
	button_event(number)
}

button_keyup(number) {
	ControlClick, X1 Y1, key_button%number%, , , , U
}

button_handler() {
	number := SubStr(A_GuiControl, 11)
	button_event(number)
}

button_event(number) {
	GuiControlGet, symbol, , key_button%number%, Text
	Send % symbol
}

show_symbol_palette(symbols) {
	global hwnd
	If (symbols.Length() > 0) {
		For index, symbol in symbols {
			GuiControlGet, button_enabled, Enabled, key_button%index%
			If (button_enabled) {
				GuiControl, Text, key_button%index%, %symbol%
			}
		}
		Gui, Show, NA, SatgosSymbolPaletteShown
		WinSet, Transparent, 192, ahk_id %hwnd%
	} Else {
		Gui, Show, Hide, SatgosSymbolPalette
	}
}

set_next_input_language() {
	WinExist("A")
	ControlGetFocus, CtrlInFocus
	PostMessage, 0x50, 4, , %CtrlInFocus%
}

toggle_ime_convmode() {
	current_convmode := get_ime_convmode()
	; 默认为英文状态，则转换模式为268435456
	If (current_convmode = 0 || current_convmode = 0x10000000) {
		set_ime_convmode(1)
	} Else {
		set_ime_convmode(0)
	}
	Return
}

set_ime_convmode(mode) {
	ControlGet, ahwnd, Hwnd, , , A
	Return DllCall("SendMessage"
		, "UInt", DllCall("imm32\ImmGetDefaultIMEWnd", "UInt", ahwnd)
		, "UInt", 0x0283 ; Message = WM_IME_CONTROL
		, "Int", 0x0002 ; wParam = IMC_SETCONVERSIONMODE
		, "Int", mode) ; lParam = CONVERSIONMODE
}

get_ime_convmode() {
	ControlGet, ahwnd, Hwnd, , , A
	ptrSize := !A_PtrSize ? 4 : A_PtrSize
	cbSize := 4 + 4 + (PtrSize * 6) + 16
	VarSetCapacity(stGTI, cbSize, 0)
	NumPut(cbSize, stGTI, 0, "UInt")  ; DWORD cbSize;
	ahwnd := DllCall("GetGUIThreadInfo", "Uint", 0, "Uint", &stGTI)
		? NumGet(stGTI, 8 + PtrSize, "UInt") : ahwnd
	Return DllCall("SendMessage"
		, "UInt", DllCall("imm32\ImmGetDefaultIMEWnd", "Uint", ahwnd)
		, "UInt", 0x0283 ; Message = WM_IME_CONTROL
		,  "Int", 0x0001 ; wParam = IMC_GETCONVERSIONMODE
		,  "Int", 0) ; lParam = 0
}

text_to_tex(str) {
	; StrReplace不好用，因为它不知道区不区分大小写
	str := RegExReplace(str, "“", "`" + "`")
	str := RegExReplace(str, "”", "''")
	str := RegExReplace(str, "‘", "`")
	str := RegExReplace(str, "’", "'")
	str := RegExReplace(str, "\bLaTeX\b", "\LaTeX{}")
	str := RegExReplace(str, "TeX\b", "\TeX{}")
	str := RegExReplace(str, "\bTeXbook\b", "\TeX book")
	str := RegExReplace(str, "\bMETAFONT\b", "\MF{}")
	str := RegExReplace(str, "\bMETAFONTbook\b", "\MF book")
	str := RegExReplace(str, "−", "--")
	str := RegExReplace(str, "—", "---")
	str := RegExReplace(str, "i)\bPh\.\s?D\. ", "Ph.D.\ ")
	str := RegExReplace(str, "\b(Miss|Ms|Mrs?|cf|v\.?s)\.\s", "$1.~")
	str := RegExReplace(str, Chr(160), "~")
	str := RegExReplace(str, "%", "\%")
	str := RegExReplace(str, "i)(?:https?|s?ftps?|git|telnet)://[\w~!@#\$%\^&\*\(\)\[\]\{\}<>,\./\?=\+:;""'-]{4,2083}", "\url{$0}")
	str := RegExReplace(str, "(\w)\s&\s(\w)", "$1 \& $2")
	str := RegExReplace(str, "$(\d)", "\$$$1")
	str := RegExReplace(str, "#(\d)", "\#$1")
	str := RegExReplace(str, "<em>(.+?)</em>", "\emph{$1}")
	Return str
}

XButton1::Return
XButton2::Return

Launch_App1::Return
Launch_App2::
	Return
Media_Next::Return
Media_Prev::Return
Media_Play_Pause::Return
Media_Stop::
	downloading := 0
	Process, Exist, baidunetdisk.exe
	downloading += ErrorLevel
	Process, Exist, uget.exe
	downloading += ErrorLevel
	Process, Exist, uTorrent.exe
	downloading += ErrorLevel
	If (downloading) {
		MsgBox, 0x131, 睡眠, 还有下载任务。睡眠吗？
	} Else {
		MsgBox, 0x1, 睡眠, 睡眠吗？
	}
	IfMsgBox OK
	{
		; Parameter #1: Pass 1 instead of 0 to hibernate rather than suspend.
		; Parameter #2: Pass 1 instead of 0 to suspend immediately rather than asking each application for permission.
		; Parameter #3: Pass 1 instead of 0 to disable all wake events.
		DllCall("PowrProf\SetSuspendState", "Int", 0, "Int", 0, "Int", 0)
	}
	Return
Browser_Back::Send {Volume_Down}
Browser_Forward::Send {Volume_Up}
Browser_Home::Return
Launch_Mail::
	If (last_clipboard == Clipboard) {
		ToolTip, 已经转换过了。
	} Else {
		last_clipboard := text_to_tex(Clipboard)
		Clipboard := last_clipboard
		ToolTip, 将剪贴板文本转换为 LaTeX 格式。
	}
	SetTimer, RemoveToolTip, -1000
	Return

RemoveToolTip:
	ToolTip
	Return

MenuHandler:
	str := A_ThisMenuItem
	pos := InStr(str, " ")
	If (pos) {
		str := SubStr(str, pos + 1)
	}
	Send %str%
	Return


Adds ability to disable overlay

Adds ability to disable overlay

; Uncomment the next line if you don't want this script appearing in your system tray
;#NoTrayIcon
#SingleInstance Force
#Persistent
Initialize()
return

;;;;;;;;;;;
; Desktop names.  Will appear in the overlay while you switch desktops.
DesktopNames() {
    global
    desktopName := Object()
    ; Example: desktopName[4] := "Work Documents"
    desktopName[0] := ""
    desktopName[1] := ""
    desktopName[2] := ""
    desktopName[3] := ""
    desktopName[4] := ""
    desktopName[5] := ""
    desktopName[6] := ""
    desktopName[7] := ""
    desktopName[8] := ""
}

Options() {
    global
    overlayEnabled := 1 ; 1 = enabled, 0 = disabled
}

;;;;;;;
; Hotkeys.  Customize these to your liking.

^#Left::SendCommand(GO_LEFT)
^#Right::SendCommand(GO_RIGHT)
^#Down::SendCommand(GO_DOWN)
^#Up::SendCommand(GO_UP)
#!Left::SendCommand(MOVE_LEFT)
#!Right::SendCommand(MOVE_RIGHT)
#!Down::SendCommand(MOVE_DOWN)
#!Up::SendCommand(MOVE_UP)
^#F1::SendCommand(GO_TO, 0)
^#F2::SendCommand(GO_TO, 1)
^#F3::SendCommand(GO_TO, 2)
^#F4::SendCommand(GO_TO, 3)
^#F5::SendCommand(GO_TO, 4)
^#F6::SendCommand(GO_TO, 5)
^#F7::SendCommand(GO_TO, 6)
^#F8::SendCommand(GO_TO, 7)
^#F9::SendCommand(GO_TO, 8)
^#F10::SendCommand(GO_TO, 9)
^#F11::SendCommand(GO_TO, 10)
^#F12::SendCommand(GO_TO, 11)
#!F1::SendCommand(MOVE_TO, 0)
#!F2::SendCommand(MOVE_TO, 1)
#!F3::SendCommand(MOVE_TO, 2)
#!F4::SendCommand(MOVE_TO, 3)
#!F5::SendCommand(MOVE_TO, 4)
#!F6::SendCommand(MOVE_TO, 5)
#!F7::SendCommand(MOVE_TO, 6)
#!F8::SendCommand(MOVE_TO, 7)
#!F9::SendCommand(MOVE_TO, 8)
#!F10::SendCommand(MOVE_TO, 9)
#!F11::SendCommand(MOVE_TO, 10)
#!F12::SendCommand(MOVE_TO, 11)
^#A::ToggleAlwaysOnTop()
^#S::SendCommand(TOGGLE_STICKY)
^#D::SendCommand(DEBUG_SHOW_CURRENT_WINDOW_HWND)
#If overlayVisible
; When either Ctrl or Win is released, the overlay is hidden.
~*Ctrl Up::HideOverlay()
~*LWin Up::HideOverlay()
#If

; Called once when the script is launched.
Initialize() {
    global
    CommandConstants()
    DesktopNames()
    Options()

    MsgNum := DllCall("RegisterWindowMessage", Str, "VIRTUALDESKTOPGRIDSWITCHER_COMMAND")

    exeName := "VirtualDesktopGridSwitcher.exe"

    ; Launch VirtualDesktopGridManager if it's not already running.
    DetectHiddenWindows, On
    If !WinExist("ahk_exe " . exeName) {
        Run "../VirtualDesktopGridSwitcher.exe"
    }

    ; Run function when this script is exited
    OnExit, % OnExitFn

    overlayVisible := 0

    ; Register listener to receive messages from .exe
    OnMessage(MsgNum, "ReceiveMessage")
    myHwnd := HexToDec(A_ScriptHwnd)
    SendCommand(SET_HWND_MESSAGE_TARGET, myHwnd)
}

CommandConstants() {
    global
    ; All commands
    GO_UP := 1
    GO_LEFT := 2
    GO_RIGHT := 3
    GO_DOWN := 4
    MOVE_UP := 5
    MOVE_LEFT := 6
    MOVE_RIGHT := 7
    MOVE_DOWN := 8
    TOGGLE_STICKY := 10
    DEBUG_SHOW_CURRENT_WINDOW_HWND := 11
    SET_HWND_MESSAGE_TARGET := 12
    SWITCHED_DESKTOP := 13
    QUIT := 14
    GO_TO := 15
    MOVE_TO := 16
}

; Return an HWND that will receive our commands.
GetTargetHwnd() {
    global exeName
    DetectHiddenWindows, On
    WinGet, a, List, ahk_exe %exeName%
    Loop, %a% {
        id := a%A_Index%
        WinGetClass, cls, ahk_id %id%
        ; Skip the "GDI+" window
        If(RegExMatch(cls, "^GDI") = 0) {
            return id
        }
    }
}

; Send a command to VirtualDesktopGridSwitcher
SendCommand(cmd, value := 0) {
    ; All variables are global unless declared to be local
    global
    local id
    id := GetTargetHwnd()
    PostMessage, % MsgNum, % cmd, % value, , ahk_id %id%
}

; Called when VirtualDesktopGridSwitcher sends us a message
ReceiveMessage(cmd, value, msg, hwnd) {
    ; All variables are global unless declared to be local
    global
    local num
    if (cmd = SWITCHED_DESKTOP) {
        num := value + 1
        ShowOverlay("Desktop " + num, desktopName[value])
    } else {
        MsgBox, % cmd, % value
    }
}

HexToDec(hex) {
    result := hex + 0
    ; result .= ""
    return result
}

ShowOverlay(title, message) {
    global overlayVisible, overlayEnabled
    if(!overlayEnabled) {
        return
    }
    overlayVisible := 1
    SysGet, primary, MonitorPrimary
    SysGet, monitor, Monitor, % primary
    width := 200
    height := 50
    padding := 100
    x := monitorLeft + ((monitorRight - monitorLeft) / 2) - (width / 2)
    y := monitorBottom - height - padding
    SplashImage, Icons/1.ico, BX%x%Y%y%W%width%H%height%, % message, % title, ,
}

HideOverlay() {
    global overlayVisible
    overlayVisible := 0
    SplashImage, Off
}

ToggleAlwaysOnTop(hwnd := "") {
    if(hwnd == "") {
        hwnd := WinExist("A")
    }
    WinSet, AlwaysOnTop, Toggle, ahk_id %hwnd%
    ;WinGet, ExStyle, ExStyle, ahk_id %hwnd%
    ;if(ExStyle & 0x8) {
    ;    message := "Always on Top Enabled"
    ;} else {
    ;    message := "Always on Top Disabled"
    ;}
    ;TrayTip, , %message%, 1, 0x10
}

OnExitFn() {
    ; TODO kill the .exe
}

; Uncomment the next line if you don't want this script appearing in your system tray
;#NoTrayIcon
#SingleInstance Force
#Persistent
Initialize()
return

;;;;;;;;;;;
; Desktop names.  Will appear in the overlay while you switch desktops.
DesktopNames() {
    global
    desktopName := Object()
    ; Example: desktopName[4] := "Work Documents"
    desktopName[0] := ""
    desktopName[1] := ""
    desktopName[2] := ""
    desktopName[3] := ""
    desktopName[4] := ""
    desktopName[5] := ""
    desktopName[6] := ""
    desktopName[7] := ""
    desktopName[8] := ""
}

;;;;;;;
; Hotkeys.  Customize these to your liking.

^#Left::SendCommand(GO_LEFT)
^#Right::SendCommand(GO_RIGHT)
^#Down::SendCommand(GO_DOWN)
^#Up::SendCommand(GO_UP)
#!Left::SendCommand(MOVE_LEFT)
#!Right::SendCommand(MOVE_RIGHT)
#!Down::SendCommand(MOVE_DOWN)
#!Up::SendCommand(MOVE_UP)
^#F1::SendCommand(GO_TO, 0)
^#F2::SendCommand(GO_TO, 1)
^#F3::SendCommand(GO_TO, 2)
^#F4::SendCommand(GO_TO, 3)
^#F5::SendCommand(GO_TO, 4)
^#F6::SendCommand(GO_TO, 5)
^#F7::SendCommand(GO_TO, 6)
^#F8::SendCommand(GO_TO, 7)
^#F9::SendCommand(GO_TO, 8)
^#F10::SendCommand(GO_TO, 9)
^#F11::SendCommand(GO_TO, 10)
^#F12::SendCommand(GO_TO, 11)
#!F1::SendCommand(MOVE_TO, 0)
#!F2::SendCommand(MOVE_TO, 1)
#!F3::SendCommand(MOVE_TO, 2)
#!F4::SendCommand(MOVE_TO, 3)
#!F5::SendCommand(MOVE_TO, 4)
#!F6::SendCommand(MOVE_TO, 5)
#!F7::SendCommand(MOVE_TO, 6)
#!F8::SendCommand(MOVE_TO, 7)
#!F9::SendCommand(MOVE_TO, 8)
#!F10::SendCommand(MOVE_TO, 9)
#!F11::SendCommand(MOVE_TO, 10)
#!F12::SendCommand(MOVE_TO, 11)
^#A::ToggleAlwaysOnTop()
^#S::SendCommand(TOGGLE_STICKY)
^#D::SendCommand(DEBUG_SHOW_CURRENT_WINDOW_HWND)
#If overlayVisible
; When either Ctrl or Win is released, the overlay is hidden.
~*Ctrl Up::HideOverlay()
~*LWin Up::HideOverlay()
#If

; Called once when the script is launched.
Initialize() {
    global
    CommandConstants()
    DesktopNames()

    MsgNum := DllCall("RegisterWindowMessage", Str, "VIRTUALDESKTOPGRIDSWITCHER_COMMAND")

    exeName := "VirtualDesktopGridSwitcher.exe"

    ; Launch VirtualDesktopGridManager if it's not already running.
    DetectHiddenWindows, On
    If !WinExist("ahk_exe " . exeName) {
        Run "../VirtualDesktopGridSwitcher.exe"
    }

    ; Run function when this script is exited
    OnExit, % OnExitFn

    overlayVisible := 0

    ; Register listener to receive messages from .exe
    OnMessage(MsgNum, "ReceiveMessage")
    myHwnd := HexToDec(A_ScriptHwnd)
    SendCommand(SET_HWND_MESSAGE_TARGET, myHwnd)
}

CommandConstants() {
    global
    ; All commands
    GO_UP := 1
    GO_LEFT := 2
    GO_RIGHT := 3
    GO_DOWN := 4
    MOVE_UP := 5
    MOVE_LEFT := 6
    MOVE_RIGHT := 7
    MOVE_DOWN := 8
    TOGGLE_STICKY := 10
    DEBUG_SHOW_CURRENT_WINDOW_HWND := 11
    SET_HWND_MESSAGE_TARGET := 12
    SWITCHED_DESKTOP := 13
    QUIT := 14
    GO_TO := 15
    MOVE_TO := 16
}

; Return an HWND that will receive our commands.
GetTargetHwnd() {
    global exeName
    DetectHiddenWindows, On
    WinGet, a, List, ahk_exe %exeName%
    Loop, %a% {
        id := a%A_Index%
        WinGetClass, cls, ahk_id %id%
        ; Skip the "GDI+" window
        If(RegExMatch(cls, "^GDI") = 0) {
            return id
        }
    }
}

; Send a command to VirtualDesktopGridSwitcher
SendCommand(cmd, value := 0) {
    ; All variables are global unless declared to be local
    global
    local id
    id := GetTargetHwnd()
    PostMessage, % MsgNum, % cmd, % value, , ahk_id %id%
}

; Called when VirtualDesktopGridSwitcher sends us a message
ReceiveMessage(cmd, value, msg, hwnd) {
    ; All variables are global unless declared to be local
    global
    local num
    if (cmd = SWITCHED_DESKTOP) {
        num := value + 1
        ShowOverlay("Desktop " + num, desktopName[value])
    } else {
        MsgBox, % cmd, % value
    }
}

HexToDec(hex) {
    result := hex + 0
    ; result .= ""
    return result
}

ShowOverlay(title, message) {
    global overlayVisible
    overlayVisible := 1
    SysGet, primary, MonitorPrimary
    SysGet, monitor, Monitor, % primary
    width := 200
    height := 50
    padding := 100
    x := monitorLeft + ((monitorRight - monitorLeft) / 2) - (width / 2)
    y := monitorBottom - height - padding
    SplashImage, Icons/1.ico, BX%x%Y%y%W%width%H%height%, % message, % title, ,
}

HideOverlay() {
    global overlayVisible
    overlayVisible := 0
    SplashImage, Off
}

ToggleAlwaysOnTop(hwnd := "") {
    if(hwnd == "") {
        hwnd := WinExist("A")
    }
    WinSet, AlwaysOnTop, Toggle, ahk_id %hwnd%
    ;WinGet, ExStyle, ExStyle, ahk_id %hwnd%
    ;if(ExStyle & 0x8) {
    ;    message := "Always on Top Enabled"
    ;} else {
    ;    message := "Always on Top Disabled"
    ;}
    ;TrayTip, , %message%, 1, 0x10
}

OnExitFn() {
    ; TODO kill the .exe
}


update ahk

update ahk

DetectHiddenWindows, On
SetDefaultMouseSpeed, 0
CoordMode, Mouse, Client
target=ahk_class Diablo II

WinGet, hwnd, ID, %target%
if (!hwnd) {
    Run, "Diablo II.exe" -w, C:\Diablo II
    Sleep, 5000
    WinGet, hwnd, ID, %target%
    if (hwnd) {
        WinActivate, ahk_id %hwnd%
        IfWinActive, ahk_id %hwnd%
        {
            BlockInput On
            MouseMove, 400, 308, 0
            MouseClick, left
            Sleep, 100
            MouseClick, left
            Sleep, 5000
            MouseMove, 175, 125, 0
            MouseClick, left
            MouseClick, left
            Sleep, 5000
            BlockInput Off
        }
    }
}

DetectHiddenWindows, On
SetDefaultMouseSpeed, 0
CoordMode, Mouse, Client
target=ahk_class Diablo II

WinGet, hwnd, ID, %target%
if (!hwnd) {
    Run, "Diablo II.exe" -w, C:\Diablo II
    Sleep, 5000
    WinGet, hwnd, ID, %target%
    if (hwnd) {
        WinActivate, ahk_id %hwnd%
        IfWinActive, ahk_id %hwnd%
        {
            BlockInput On
            MouseMove, 400, 308, 0
            MouseClick, left
            Sleep, 5000
            MouseMove, 175, 125, 0
            MouseClick, left
            MouseClick, left
            Sleep, 5000
            BlockInput Off
        }
    }
}


AutoHotkey.ahk, from darwin11

AutoHotkey.ahk, from darwin11

; Tips {{{
; #: win
; !: alt
; ^: ctrl
; +: shift
; &: and
; <: only left, >: only right
; *: wildecard
; ~: pass through
; }}}

; Auto-execute section {{{
; Auto execute section is the region before any return/hotkey

; Windows size
TMargin=5
BMargin=5
LMargin=5
RMargin=155 ; sidebar = 150px
MinSize=0.7

; For Terminal/Vim
GroupAdd Terminal, ahk_class PuTTY
GroupAdd Terminal, ahk_class mintty ; cygwin
GroupAdd TerminalVim, ahk_group Terminal
GroupAdd TerminalVim, ahk_class Vim

#Include  %A_ScriptDir%\vim.ahk

Return
; }}}

; Basic Settings, HotKeys, Functions {{{
; Settings

#UseHook On ; Make it a bit slow, but can avoid infinitude loop
            ; Same as "$" for each hotkey
#InstallKeybdHook ; For checking key history
                  ; Use ~500kB memory?

#HotkeyInterval 2000 ; Hotkey inteval (default 2000 milliseconds).
#MaxHotkeysPerInterval 70 ; Max hotkeys perinterval (default 50).

; For HHK
;vkFFsc079::RAlt
;vkFFsc07b::LAlt
vkFFsc079::RCtrl
vkFFsc07b::LCtrl

;; Ref for IME: http://www6.atwiki.jp/eamat/pages/17.html
;; Get IME Status. 0: Off, 1: On
;IME_GET(WinTitle="A")  {
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if (WinActive(WinTitle)) {
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;        ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  Return DllCall("SendMessage"
;      , UInt, DllCall("imm32\ImmGetDefaultIMEWnd", Uint,hwnd)
;      , UInt, 0x0283  ;Message : WM_IME_CONTROL
;      ,  Int, 0x0005  ;wParam  : IMC_GETOPENSTATUS
;      ,  Int, 0)      ;lParam  : 0
;}
;; Get input status. 1: Converting, 2: Have converting window, 0: Others
;IME_GetConverting(WinTitle="A",ConvCls="",CandCls="") {
;  ; Input windows, candidate windows (Add new IME with "|")
;  ConvCls .= (ConvCls ? "|" : "")                 ;--- Input Window ---
;    .  "ATOK\d+CompStr"                           ; ATOK
;    .  "|imejpstcnv\d+"                           ; MS-IME
;    .  "|WXGIMEConv"                              ; WXG
;    .  "|SKKIME\d+\.*\d+UCompStr"                 ; SKKIME Unicode
;    .  "|MSCTFIME Composition"                    ; Google IME
;  CandCls .= (CandCls ? "|" : "")                 ;--- Candidate Window ---
;    .  "ATOK\d+Cand"                              ; ATOK
;    .  "|imejpstCandList\d+|imejpstcand\d+"       ; MS-IME 2002(8.1)XP
;    .  "|mscandui\d+\.candidate"                  ; MS Office IME-2007
;    .  "|WXGIMECand"                              ; WXG
;    .  "|SKKIME\d+\.*\d+UCand"                    ; SKKIME Unicode
; CandGCls := "GoogleJapaneseInputCandidateWindow" ; Google IME
;
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if (WinActive(WinTitle)) {
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;      ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  WinGet, pid, PID,% "ahk_id " hwnd
;  tmm:=A_TitleMatchMode
;  SetTitleMatchMode, RegEx
;  ret := WinExist("ahk_class " . CandCls . " ahk_pid " pid) ? 2
;      :  WinExist("ahk_class " . CandGCls                 ) ? 2
;      :  WinExist("ahk_class " . ConvCls . " ahk_pid " pid) ? 1
;      :  0
;  SetTitleMatchMode, %tmm%
;  Return ret
;}
;; Set IME, SetSts=0: Off, 1: On, return 0 for success, others for non-success
;IME_SET(SetSts=0, WinTitle="A")    {
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if(WinActive(WinTitle)){
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;        ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  Return DllCall("SendMessage"
;    , UInt, DllCall("imm32\ImmGetDefaultIMEWnd", Uint,hwnd)
;    , UInt, 0x0283  ;Message : WM_IME_CONTROL
;    ,  Int, 0x006   ;wParam  : IMC_SETOPENSTATUS
;    ,  Int, SetSts) ;lParam  : 0 or 1
;}

; }}}

; Terminal/Vim {{{
; ESC + IME
#IfWInActive, ahk_group TerminalVim
Esc:: ; Just send Esc at converting.
  if (IME_GET(A)) {
    if (IME_GetConverting(A)) {
      Send,{Esc}
    } else {
      IME_SET()
    }
  } else {
    Send,{Esc}
  }
  Return
^[:: ; Go to Normal mode (for vim) with IME off even at converting.
  if (IME_GET(A)) {
    Send,{Esc}
    Sleep 1 ; wait 1 ms (Need to stop converting)
    IME_SET()
    Send,{Esc}
  } else {
    Send,{Esc}
  }
  Return
#IfWInActive

; Paste
#IfWInActive, ahk_group Terminal
!v::
  StringSplit, strout, clipboard, `n
  If(strout0>1 or InStr(clipboard, "sudo")>0) {
    MsgBox, 308, Clipboard, %clipboard%`n`n Do you want to paste?
    IfMsgBox, No
    {
      Return
    }
  }
  MouseClick, Right, 50, 50, 1
  Return
#IfWInActive
#IfWInActive, ahk_class Vim
!v::Send,{Alt}ep
#IfWInActive

; Other than Terminal/Vim
#IfWInNotActive, ahk_group TerminalVim
^[::Send,{Esc}             ; Always C-[ to ESC, like vim
#IfWInNotActive
; }}} Terminal/Vim

; Power Point {{{
#IfWInActive, ahk_class PP12FrameClass
; Select one character
^Space::Send,{Right}+{Left}

; Subscript/Superscript
^=::Send,^tb{Enter}
^+=::Send,^tp{Enter}
; For JP keyboards
;^-::Send,^tb{Enter}
;^+-::Send,^tp{Enter}

; Fonts
^+s::Send,^tfsymbol{Enter}
^+a::Send,^tfarial{Enter}
^+h::Send,^tfhelvetica{Enter}
#IfWInActive
; }}} Power Point

; Word {{{
#IfWInActive, ahk_class OpusApp
^Space::Send,{Right}+{Left}
^=::Send,^tb{Enter}
^+=::Send,^tp{Enter}
; for JP keyboards
;^-::Send,^tb{Enter}
;^+-::Send,^tp{Enter}
#IfWInActive
; }}} Word

; Explorer {{{
#IfWInActive, ahk_class CabinetWClas
; Next/Previous page
^i::Send,!{Right}
^o::Send,!{Left}
#IfWInActive
; }}} Explorer

; CLCL {{{
;#IfWInActive, ahk_class CLCLMain
#IfWInActive, CLCL ; both window/title do not working...
j::
  Msgbox, clcl_down
  Send,{Down}
  Return
K::Send,{Up}
#IfWInActive
; }}} Explorer

; Everthing {{{
#IfWInActive, ahk_class EVERYTHING
Enter::
  Send,{Enter}
  sleep,200
  IfWinActive, ahk_class EVERYTHING
  {
    Send,{Enter}
    sleep,200
  }
  WinActivate, ahk_class EVERYTHING
  sleep,100
  Send,{Esc}
  Return
#IfWInActive
; }}} Everything

; Global Settings {{{
#if
; Basic Settings {{{

; Suspend
;Esc & Tab::
;  Suspend, Toggle
;  Return
; For Alt-Ctrl Swap (swapped by KeySwap)
LCtrl & Tab::AltTab
Alt & Tab::
  if GetKeyState("Shift","P") {
    Send,^+{taB} ; dosen't work ()
  }else{
    Send,^{Tab}
  }
  Return
;!m::Send,^m
;^m::Send,!m
^m::Send,!m
Ctrl & Left::Send,!{Left}
Ctrl & Right::Send,!{Right}

; Other basic settings
!Space::Send,{vkF3sc029}   ; IME by A-Space
^Space::Send,{vkF3sc029}   ; IME by C-Space
; {vkF3sc029}              ; 変換
; {vk1Dsc07B}              ; 無変換
^h::Send,{BS}              ; Always BS with C-h
!a::Send,^{Space}n        ; Minimize window
!4::Send,!{F4}             ; Close window
^4::Send,!{F4}             ; Close window
!d::Send,{Del}             ; Always Delete with A-d
;^o::Send,!{Left}           ; Go to previous page
;^i::Send,!{Right}          ; Go to nexe page
^Space::!`                 ; IME

; For HHK
; ESC to ` like normal keyboard (ESC is placed on the left of 1 in HHK)
!1::!`                ; IME
; }}} Basic Settings

; Cursor, Mouse, Window move/size {{{
; Cursor {{{
!^h::Send,{Left}
!^j::Send,{Down}
!^k::Send,{Up}
!^l::Send,{Right}
; }}} Cursor

; Mouse {{{
; Mouse Move
!^y::MouseMove, -10, 0, 0, R
!^u::MouseMove, 0, 10, 0, R
!^i::MouseMove, 0, -10, 0, R
!^o::MouseMove, 10, 0, 0, R

; Click
!^n::MouseClick, Left
!^p::MouseClick, Right
; Right click on current window

; Mouse wheel
!^m::MouseClick, WheelDown,,,  2
!^,::MouseClick, WheelUp,,,  2
; }}} Mouse

; Window move {{{
^+y::
  WinGetPos, X, Y, , , A ;A for Active Window
  ;Msgbox, Pos At %X% %Y%
  WinMove, A, ,X-20, Y,
  ;Msgbox, Moved At %X%-20 %Y%
  Return ; Necessary for mapping which has at least 2 commands
^+u::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X, Y+20
  Return
^+i::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X, Y-20
  Return
^+o::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X+20, Y
  Return
; }}} Window move

; Window size {{{
^!Enter::
  ;WinGetPos, X, Y, W, H, Program Manager ; Full Desktop
  ;Msgbox, Pos At %X% %Y% %W% %H%
  SysGet, MWA, MonitorWorkArea ; w/o Taskbar
  ;Msgbox, %MWALeft% %MWATop% %MWARight% %MWABottom%
  WinMove, A, ,MWALeft+LMargin, MWATop+TMargin
    , MWARight-MWALeft-LMargin-RMargin
    , MWABottom-MWATop-TMargin-BMargin
  Return
^!+Enter::
  ;WinGetPos, X, Y, W, H, Program Manager ; Full Desktop
  ;Msgbox, Pos At %X% %Y% %W% %H%
  SysGet, MWA, MonitorWorkArea ; w/o Taskbar
  ;Msgbox, %MWALeft% %MWATop% %MWARight% %MWABottom%
  WinMove, A, ,MWALeft+LMargin, MWATop+TMargin
    , (MWARight-MWALeft-LMargin-RMargin)*minsize
    , (MWABottom-MWATop-TMargin-BMargin)*minsize
  Return
; }}} Window size
; }}} Cursor, Mouse, Window move
; }}} Global Settings

; Tips {{{
; #: win
; !: alt
; ^: ctrl
; +: shift
; &: and
; <: only left, >: only right
; *: wildecard
; ~: pass through
; }}}

; Auto-execute section {{{
; Auto execute section is the region before any return/hotkey

; Windows size
TMargin=5
BMargin=5
LMargin=5
RMargin=155 ; sidebar = 150px
MinSize=0.7

; For Terminal/Vim
GroupAdd Terminal, ahk_class PuTTY
GroupAdd Terminal, ahk_class mintty ; cygwin
GroupAdd Terminal, ahk_class Vim

#Include  %A_ScriptDir%\vim.ahk

Return
; }}}

; Basic Settings, HotKeys, Functions {{{
; Settings

#UseHook On ; Make it a bit slow, but can avoid infinitude loop
            ; Same as "$" for each hotkey
#InstallKeybdHook ; For checking key history
                  ; Use ~500kB memory?

#HotkeyInterval 2000 ; Hotkey inteval (default 2000 milliseconds).
#MaxHotkeysPerInterval 70 ; Max hotkeys perinterval (default 50).

; For HHK
;vkFFsc079::RAlt
;vkFFsc07b::LAlt
vkFFsc079::RCtrl
vkFFsc07b::LCtrl

;; Ref for IME: http://www6.atwiki.jp/eamat/pages/17.html
;; Get IME Status. 0: Off, 1: On
;IME_GET(WinTitle="A")  {
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if (WinActive(WinTitle)) {
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;        ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  Return DllCall("SendMessage"
;      , UInt, DllCall("imm32\ImmGetDefaultIMEWnd", Uint,hwnd)
;      , UInt, 0x0283  ;Message : WM_IME_CONTROL
;      ,  Int, 0x0005  ;wParam  : IMC_GETOPENSTATUS
;      ,  Int, 0)      ;lParam  : 0
;}
;; Get input status. 1: Converting, 2: Have converting window, 0: Others
;IME_GetConverting(WinTitle="A",ConvCls="",CandCls="") {
;  ; Input windows, candidate windows (Add new IME with "|")
;  ConvCls .= (ConvCls ? "|" : "")                 ;--- Input Window ---
;    .  "ATOK\d+CompStr"                           ; ATOK
;    .  "|imejpstcnv\d+"                           ; MS-IME
;    .  "|WXGIMEConv"                              ; WXG
;    .  "|SKKIME\d+\.*\d+UCompStr"                 ; SKKIME Unicode
;    .  "|MSCTFIME Composition"                    ; Google IME
;  CandCls .= (CandCls ? "|" : "")                 ;--- Candidate Window ---
;    .  "ATOK\d+Cand"                              ; ATOK
;    .  "|imejpstCandList\d+|imejpstcand\d+"       ; MS-IME 2002(8.1)XP
;    .  "|mscandui\d+\.candidate"                  ; MS Office IME-2007
;    .  "|WXGIMECand"                              ; WXG
;    .  "|SKKIME\d+\.*\d+UCand"                    ; SKKIME Unicode
; CandGCls := "GoogleJapaneseInputCandidateWindow" ; Google IME
;
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if (WinActive(WinTitle)) {
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;      ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  WinGet, pid, PID,% "ahk_id " hwnd
;  tmm:=A_TitleMatchMode
;  SetTitleMatchMode, RegEx
;  ret := WinExist("ahk_class " . CandCls . " ahk_pid " pid) ? 2
;      :  WinExist("ahk_class " . CandGCls                 ) ? 2
;      :  WinExist("ahk_class " . ConvCls . " ahk_pid " pid) ? 1
;      :  0
;  SetTitleMatchMode, %tmm%
;  Return ret
;}
;; Set IME, SetSts=0: Off, 1: On, return 0 for success, others for non-success
;IME_SET(SetSts=0, WinTitle="A")    {
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if(WinActive(WinTitle)){
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;        ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  Return DllCall("SendMessage"
;    , UInt, DllCall("imm32\ImmGetDefaultIMEWnd", Uint,hwnd)
;    , UInt, 0x0283  ;Message : WM_IME_CONTROL
;    ,  Int, 0x006   ;wParam  : IMC_SETOPENSTATUS
;    ,  Int, SetSts) ;lParam  : 0 or 1
;}

; }}}

; Terminal/Vim {{{
; On Terminal/Vim
#IfWInActive, ahk_group Terminal
Esc:: ; Just send Esc at converting.
  if (IME_GET(A)) {
    if (IME_GetConverting(A)) {
      Send,{Esc}
    } else {
      IME_SET()
    }
  } else {
    Send,{Esc}
  }
  Return
^[:: ; Go to Normal mode (for vim) with IME off even at converting.
  if (IME_GET(A)) {
    Send,{Esc}
    Sleep 1 ; wait 1 ms (Need to stop converting)
    IME_SET()
    Send,{Esc}
  } else {
    Send,{Esc}
  }
  Return
#IfWInActive

; Other than Terminal/Vim
#IfWInNotActive, ahk_group Terminal
^[::Send,{Esc}             ; Always C-[ to ESC, like vim
#IfWInNotActive
; }}} Terminal/Vim

; Power Point {{{
#IfWInActive, ahk_class PP12FrameClass
; Select one character
^Space::Send,{Right}+{Left}

; Subscript/Superscript
^=::Send,^tb{Enter}
^+=::Send,^tp{Enter}
; For JP keyboards
;^-::Send,^tb{Enter}
;^+-::Send,^tp{Enter}

; Fonts
^+s::Send,^tfsymbol{Enter}
^+a::Send,^tfarial{Enter}
^+h::Send,^tfhelvetica{Enter}
#IfWInActive
; }}} Power Point

; Word {{{
#IfWInActive, ahk_class OpusApp
^Space::Send,{Right}+{Left}
^=::Send,^tb{Enter}
^+=::Send,^tp{Enter}
; for JP keyboards
;^-::Send,^tb{Enter}
;^+-::Send,^tp{Enter}
#IfWInActive
; }}} Word

; Explorer {{{
#IfWInActive, ahk_class CabinetWClas
; Next/Previous page
^i::Send,!{Right}
^o::Send,!{Left}
#IfWInActive
; }}} Explorer

; CLCL {{{
;#IfWInActive, ahk_class CLCLMain
#IfWInActive, CLCL ; both window/title do not working...
j::
  Msgbox, clcl_down
  Send,{Down}
  Return
K::Send,{Up}
#IfWInActive
; }}} Explorer

; Everthing {{{
#IfWInActive, ahk_class EVERYTHING
Enter::
  Send,{Enter}
  sleep,200
  IfWinActive, ahk_class EVERYTHING
  {
    Send,{Enter}
    sleep,200
  }
  WinActivate, ahk_class EVERYTHING
  sleep,100
  Send,{Esc}
  Return
#IfWInActive
; }}} Everything

; Global Settings {{{
#if
; Basic Settings {{{

; Suspend
;Esc & Tab::
;  Suspend, Toggle
;  Return
; For Alt-Ctrl Swap (swapped by KeySwap)
LCtrl & Tab::AltTab
Alt & Tab::
  if GetKeyState("Shift","P") {
    Send,^+{Tab} ; dosen't work ()
  }else{
    Send,^{Tab}
  }
  Return

;!m::Send,^m
;^m::Send,!m
^m::Send,!m
Ctrl & Left::Send,!{Left}
Ctrl & Right::Send,!{Right}

; Other basic settings
!Space::Send,{vkF3sc029}   ; IME by A-Space
^Space::Send,{vkF3sc029}   ; IME by C-Space
; {vkF3sc029}              ; 変換
; {vk1Dsc07B}              ; 無変換
^h::Send,{BS}              ; Always BS with C-h
!a::Send,^{Space}n        ; Minimize window
!4::Send,!{F4}             ; Close window
^4::Send,!{F4}             ; Close window
!d::Send,{Del}             ; Always Delete with A-d
;^o::Send,!{Left}           ; Go to previous page
;^i::Send,!{Right}          ; Go to nexe page
^Space::!`                 ; IME

; For HHK
; ESC to ` like normal keyboard (ESC is placed on the left of 1 in HHK)
!1::!`                ; IME
; }}} Basic Settings

; Cursor, Mouse, Window move/size {{{
; Cursor {{{
!^h::Send,{Left}
!^j::Send,{Down}
!^k::Send,{Up}
!^l::Send,{Right}
; }}} Cursor

; Mouse {{{
; Mouse Move
!^y::MouseMove, -10, 0, 0, R
!^u::MouseMove, 0, 10, 0, R
!^i::MouseMove, 0, -10, 0, R
!^o::MouseMove, 10, 0, 0, R

; Click
!^n::MouseClick, Left
!^p::MouseClick, Right
; Right click on current window
!v::
  MouseMove, 50, 50, 0
  MouseClick, Right
  Return

; Mouse wheel
!^m::MouseClick, WheelDown,,,  2
!^,::MouseClick, WheelUp,,,  2
; }}} Mouse

; Window move {{{
^+y::
  WinGetPos, X, Y, , , A ;A for Active Window
  ;Msgbox, Pos At %X% %Y%
  WinMove, A, ,X-20, Y,
  ;Msgbox, Moved At %X%-20 %Y%
  Return ; Necessary for mapping which has at least 2 commands
^+u::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X, Y+20
  Return
^+i::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X, Y-20
  Return
^+o::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X+20, Y
  Return
; }}} Window move

; Window size {{{
^!Enter::
  ;WinGetPos, X, Y, W, H, Program Manager ; Full Desktop
  ;Msgbox, Pos At %X% %Y% %W% %H%
  SysGet, MWA, MonitorWorkArea ; w/o Taskbar
  ;Msgbox, %MWALeft% %MWATop% %MWARight% %MWABottom%
  WinMove, A, ,MWALeft+LMargin, MWATop+TMargin
    , MWARight-MWALeft-LMargin-RMargin
    , MWABottom-MWATop-TMargin-BMargin
  Return
^!+Enter::
  ;WinGetPos, X, Y, W, H, Program Manager ; Full Desktop
  ;Msgbox, Pos At %X% %Y% %W% %H%
  SysGet, MWA, MonitorWorkArea ; w/o Taskbar
  ;Msgbox, %MWALeft% %MWATop% %MWARight% %MWABottom%
  WinMove, A, ,MWALeft+LMargin, MWATop+TMargin
    , (MWARight-MWALeft-LMargin-RMargin)*MinSize
    , (MWABottom-MWATop-TMargin-BMargin)*MinSize
  Return
; }}} Window size
; }}} Cursor, Mouse, Window move
; }}} Global Settings


AutoHotkey.ahk, from darwin11

AutoHotkey.ahk, from darwin11

; Tips {{{
; #: win
; !: alt
; ^: ctrl
; +: shift
; &: and
; <: only left, >: only right
; *: wildecard
; ~: pass through
; }}}

; Auto-execute section {{{
; Auto execute section is the region before any return/hotkey

; Windows size
TMargin=5
BMargin=5
LMargin=5
RMargin=155 ; sidebar = 150px
MinSize=0.7

; For Terminal/Vim
GroupAdd Terminal, ahk_class PuTTY
GroupAdd Terminal, ahk_class mintty ; cygwin
GroupAdd TerminalVim, ahk_group Terminal
GroupAdd TerminalVim, ahk_class Vim

#Include  %A_ScriptDir%\vim.ahk

Return
; }}}

; Basic Settings, HotKeys, Functions {{{
; Settings

#UseHook On ; Make it a bit slow, but can avoid infinitude loop
            ; Same as "$" for each hotkey
#InstallKeybdHook ; For checking key history
                  ; Use ~500kB memory?

#HotkeyInterval 2000 ; Hotkey inteval (default 2000 milliseconds).
#MaxHotkeysPerInterval 70 ; Max hotkeys perinterval (default 50).

; For HHK
;vkFFsc079::RAlt
;vkFFsc07b::LAlt
vkFFsc079::RCtrl
vkFFsc07b::LCtrl

;; Ref for IME: http://www6.atwiki.jp/eamat/pages/17.html
;; Get IME Status. 0: Off, 1: On
;IME_GET(WinTitle="A")  {
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if (WinActive(WinTitle)) {
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;        ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  Return DllCall("SendMessage"
;      , UInt, DllCall("imm32\ImmGetDefaultIMEWnd", Uint,hwnd)
;      , UInt, 0x0283  ;Message : WM_IME_CONTROL
;      ,  Int, 0x0005  ;wParam  : IMC_GETOPENSTATUS
;      ,  Int, 0)      ;lParam  : 0
;}
;; Get input status. 1: Converting, 2: Have converting window, 0: Others
;IME_GetConverting(WinTitle="A",ConvCls="",CandCls="") {
;  ; Input windows, candidate windows (Add new IME with "|")
;  ConvCls .= (ConvCls ? "|" : "")                 ;--- Input Window ---
;    .  "ATOK\d+CompStr"                           ; ATOK
;    .  "|imejpstcnv\d+"                           ; MS-IME
;    .  "|WXGIMEConv"                              ; WXG
;    .  "|SKKIME\d+\.*\d+UCompStr"                 ; SKKIME Unicode
;    .  "|MSCTFIME Composition"                    ; Google IME
;  CandCls .= (CandCls ? "|" : "")                 ;--- Candidate Window ---
;    .  "ATOK\d+Cand"                              ; ATOK
;    .  "|imejpstCandList\d+|imejpstcand\d+"       ; MS-IME 2002(8.1)XP
;    .  "|mscandui\d+\.candidate"                  ; MS Office IME-2007
;    .  "|WXGIMECand"                              ; WXG
;    .  "|SKKIME\d+\.*\d+UCand"                    ; SKKIME Unicode
; CandGCls := "GoogleJapaneseInputCandidateWindow" ; Google IME
;
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if (WinActive(WinTitle)) {
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;      ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  WinGet, pid, PID,% "ahk_id " hwnd
;  tmm:=A_TitleMatchMode
;  SetTitleMatchMode, RegEx
;  ret := WinExist("ahk_class " . CandCls . " ahk_pid " pid) ? 2
;      :  WinExist("ahk_class " . CandGCls                 ) ? 2
;      :  WinExist("ahk_class " . ConvCls . " ahk_pid " pid) ? 1
;      :  0
;  SetTitleMatchMode, %tmm%
;  Return ret
;}
;; Set IME, SetSts=0: Off, 1: On, return 0 for success, others for non-success
;IME_SET(SetSts=0, WinTitle="A")    {
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if(WinActive(WinTitle)){
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;        ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  Return DllCall("SendMessage"
;    , UInt, DllCall("imm32\ImmGetDefaultIMEWnd", Uint,hwnd)
;    , UInt, 0x0283  ;Message : WM_IME_CONTROL
;    ,  Int, 0x006   ;wParam  : IMC_SETOPENSTATUS
;    ,  Int, SetSts) ;lParam  : 0 or 1
;}

; }}}

; Terminal/Vim {{{
; ESC + IME
#IfWInActive, ahk_group TerminalVim
Esc:: ; Just send Esc at converting.
  if (VIM_IME_GET(A)) {
    if (VIM_IME_GetConverting(A)) {
      Send,{Esc}
    } else {
      VIM_IME_SET()
    }
  } else {
    Send,{Esc}
  }
  Return
^[:: ; Go to Normal mode (for vim) with IME off even at converting.
  if (VIM_IME_GET(A)) {
    Send,{Esc}
    Sleep 1 ; wait 1 ms (Need to stop converting)
    VIM_IME_SET()
    Send,{Esc}
  } else {
    Send,{Esc}
  }
  Return
#IfWInActive

; Paste
#IfWInActive, ahk_group Terminal
!v::
  StringSplit, strout, clipboard, `n
  If(strout0>1 or InStr(clipboard, "sudo")>0) {
    MsgBox, 308, Clipboard, %clipboard%`n`n Do you want to paste?
    IfMsgBox, No
    {
      Return
    }
  }
  Sleep,200
  MouseClick, Right, 50, 50, 1
  Return
#IfWInActive
#IfWInActive, ahk_class Vim
!v::Send,{Alt}ep
#IfWInActive

; Other than Terminal/Vim
#IfWInNotActive, ahk_group TerminalVim
^[::Send,{Esc}             ; Always C-[ to ESC, like vim
^d::Send,{Del}             ; Always Delete with C-d
#IfWInNotActive
; }}} Terminal/Vim

; Power Point {{{
#IfWInActive, ahk_class PP12FrameClass
; Select one character
^Space::Send,{Right}+{Left}

; Subscript/Superscript
^=::Send,^tb{Enter}
^+=::Send,^tp{Enter}
; For JP keyboards
;^-::Send,^tb{Enter}
;^+-::Send,^tp{Enter}

; Fonts
^+s::Send,^tfsymbol{Enter}
^+a::Send,^tfarial{Enter}
^+h::Send,^tfhelvetica{Enter}
#IfWInActive
; }}} Power Point

; Word {{{
#IfWInActive, ahk_class OpusApp
^Space::Send,{Right}+{Left}
^=::Send,^tb{Enter}
^+=::Send,^tp{Enter}
; for JP keyboards
;^-::Send,^tb{Enter}
;^+-::Send,^tp{Enter}
#IfWInActive
; }}} Word

; Explorer {{{
#IfWInActive, ahk_class CabinetWClass
; Next/Previous page
^i::Send,!{Right}
^o::Send,!{Left}
#IfWInActive
; }}} Explorer

; CLCL {{{
;#IfWInActive, ahk_class CLCLMain
#IfWInActive, CLCL ; both window/title do not working...
j::
  Msgbox, clcl_down
  Send,{Down}
  Return
K::Send,{Up}
#IfWInActive
; }}} Explorer

; Everthing {{{
#IfWInActive, ahk_class EVERYTHING
Return::
  Send,{Enter}
  Sleep,200
  IfWinActive, ahk_class EVERYTHING
  {
    Send,{Enter}
    Sleep,500
  }
  WinActivate, ahk_class EVERYTHING
  Sleep,200
  Send,{Esc}
  Return
#IfWInActive
; }}} Everything

; Command Prompt {{{
#IfWinActive ahk_class ConsoleWindowClass
^v::
  SendInput %clipboard%
  Return
#IfWInActive
; }}} Command Prompt

; Global Settings {{{
#if
; Basic Settings {{{

; Suspend
;Esc & Tab::
;  Suspend, Toggle
;  Return
; For Alt-Ctrl Swap (swapped by KeySwap)
LCtrl & Tab::AltTab
Alt & Tab::
  if GetKeyState("Shift","P") {
    Send,^+{taB} ; dosen't work ()
  }else{
    Send,^{Tab}
  }
  Return
;!m::Send,^m
;^m::Send,!m
^m::Send,!m
Ctrl & Left::Send,!{Left}
Ctrl & Right::Send,!{Right}

; Other basic settings
!Space::Send,{vkF3sc029}   ; IME by A-Space
^Space::Send,{vkF3sc029}   ; IME by C-Space
; {vkF3sc029}              ; 変換
; {vk1Dsc07B}              ; 無変換
^h::Send,{BS}              ; Always BS with C-h
!a::Send,^{Space}n        ; Minimize window
!4::Send,!{F4}             ; Close window
^4::Send,!{F4}             ; Close window
!d::Send,{Del}             ; Always Delete with A-d
;^o::Send,!{Left}           ; Go to previous page
;^i::Send,!{Right}          ; Go to nexe page
^Space::!`                 ; IME

; For HHK
; ESC to ` like normal keyboard (ESC is placed on the left of 1 in HHK)
!1::!`                ; IME
; }}} Basic Settings

; Cursor, Mouse, Window move/size {{{
; Cursor {{{
!^h::Send,{Left}
!^j::Send,{Down}
!^k::Send,{Up}
!^l::Send,{Right}
; }}} Cursor

; Mouse {{{
; Mouse Move
!^y::MouseMove, -10, 0, 0, R
!^u::MouseMove, 0, 10, 0, R
!^i::MouseMove, 0, -10, 0, R
!^o::MouseMove, 10, 0, 0, R

; Click
!^n::MouseClick, Left
!^p::MouseClick, Right
; Right click on current window

; Mouse wheel
!^m::MouseClick, WheelDown,,,  2
!^,::MouseClick, WheelUp,,,  2
; }}} Mouse

; Window move {{{
^+y::
  WinGetPos, X, Y, , , A ;A for Active Window
  ;Msgbox, Pos At %X% %Y%
  WinMove, A, ,X-20, Y,
  ;Msgbox, Moved At %X%-20 %Y%
  Return ; Necessary for mapping which has at least 2 commands
^+u::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X, Y+20
  Return
^+i::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X, Y-20
  Return
^+o::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X+20, Y
  Return
; }}} Window move

; Window size {{{
^!Enter::
  ;WinGetPos, X, Y, W, H, Program Manager ; Full Desktop
  ;Msgbox, Pos At %X% %Y% %W% %H%
  SysGet, MWA, MonitorWorkArea ; w/o Taskbar
  ;Msgbox, %MWALeft% %MWATop% %MWARight% %MWABottom%
  WinMove, A, ,MWALeft+LMargin, MWATop+TMargin
    , MWARight-MWALeft-LMargin-RMargin
    , MWABottom-MWATop-TMargin-BMargin
  Return
^!+Enter::
  ;WinGetPos, X, Y, W, H, Program Manager ; Full Desktop
  ;Msgbox, Pos At %X% %Y% %W% %H%
  SysGet, MWA, MonitorWorkArea ; w/o Taskbar
  ;Msgbox, %MWALeft% %MWATop% %MWARight% %MWABottom%
  WinMove, A, ,MWALeft+LMargin, MWATop+TMargin
    , (MWARight-MWALeft-LMargin-RMargin)*minsize
    , (MWABottom-MWATop-TMargin-BMargin)*minsize
  Return
; }}} Window size
; }}} Cursor, Mouse, Window move
; }}} Global Settings

; Tips {{{
; #: win
; !: alt
; ^: ctrl
; +: shift
; &: and
; <: only left, >: only right
; *: wildecard
; ~: pass through
; }}}

; Auto-execute section {{{
; Auto execute section is the region before any return/hotkey

; Windows size
TMargin=5
BMargin=5
LMargin=5
RMargin=155 ; sidebar = 150px
MinSize=0.7

; For Terminal/Vim
GroupAdd Terminal, ahk_class PuTTY
GroupAdd Terminal, ahk_class mintty ; cygwin
GroupAdd TerminalVim, ahk_group Terminal
GroupAdd TerminalVim, ahk_class Vim

#Include  %A_ScriptDir%\vim.ahk

Return
; }}}

; Basic Settings, HotKeys, Functions {{{
; Settings

#UseHook On ; Make it a bit slow, but can avoid infinitude loop
            ; Same as "$" for each hotkey
#InstallKeybdHook ; For checking key history
                  ; Use ~500kB memory?

#HotkeyInterval 2000 ; Hotkey inteval (default 2000 milliseconds).
#MaxHotkeysPerInterval 70 ; Max hotkeys perinterval (default 50).

; For HHK
;vkFFsc079::RAlt
;vkFFsc07b::LAlt
vkFFsc079::RCtrl
vkFFsc07b::LCtrl

;; Ref for IME: http://www6.atwiki.jp/eamat/pages/17.html
;; Get IME Status. 0: Off, 1: On
;IME_GET(WinTitle="A")  {
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if (WinActive(WinTitle)) {
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;        ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  Return DllCall("SendMessage"
;      , UInt, DllCall("imm32\ImmGetDefaultIMEWnd", Uint,hwnd)
;      , UInt, 0x0283  ;Message : WM_IME_CONTROL
;      ,  Int, 0x0005  ;wParam  : IMC_GETOPENSTATUS
;      ,  Int, 0)      ;lParam  : 0
;}
;; Get input status. 1: Converting, 2: Have converting window, 0: Others
;IME_GetConverting(WinTitle="A",ConvCls="",CandCls="") {
;  ; Input windows, candidate windows (Add new IME with "|")
;  ConvCls .= (ConvCls ? "|" : "")                 ;--- Input Window ---
;    .  "ATOK\d+CompStr"                           ; ATOK
;    .  "|imejpstcnv\d+"                           ; MS-IME
;    .  "|WXGIMEConv"                              ; WXG
;    .  "|SKKIME\d+\.*\d+UCompStr"                 ; SKKIME Unicode
;    .  "|MSCTFIME Composition"                    ; Google IME
;  CandCls .= (CandCls ? "|" : "")                 ;--- Candidate Window ---
;    .  "ATOK\d+Cand"                              ; ATOK
;    .  "|imejpstCandList\d+|imejpstcand\d+"       ; MS-IME 2002(8.1)XP
;    .  "|mscandui\d+\.candidate"                  ; MS Office IME-2007
;    .  "|WXGIMECand"                              ; WXG
;    .  "|SKKIME\d+\.*\d+UCand"                    ; SKKIME Unicode
; CandGCls := "GoogleJapaneseInputCandidateWindow" ; Google IME
;
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if (WinActive(WinTitle)) {
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;      ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  WinGet, pid, PID,% "ahk_id " hwnd
;  tmm:=A_TitleMatchMode
;  SetTitleMatchMode, RegEx
;  ret := WinExist("ahk_class " . CandCls . " ahk_pid " pid) ? 2
;      :  WinExist("ahk_class " . CandGCls                 ) ? 2
;      :  WinExist("ahk_class " . ConvCls . " ahk_pid " pid) ? 1
;      :  0
;  SetTitleMatchMode, %tmm%
;  Return ret
;}
;; Set IME, SetSts=0: Off, 1: On, return 0 for success, others for non-success
;IME_SET(SetSts=0, WinTitle="A")    {
;  ControlGet,hwnd,HWND,,,%WinTitle%
;  if(WinActive(WinTitle)){
;    ptrSize := !A_PtrSize ? 4 : A_PtrSize
;    VarSetCapacity(stGTI, cbSize:=4+4+(PtrSize*6)+16, 0)
;    NumPut(cbSize, stGTI,  0, "UInt")   ;   DWORD   cbSize;
;    hwnd := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
;        ? NumGet(stGTI,8+PtrSize,"UInt") : hwnd
;  }
;
;  Return DllCall("SendMessage"
;    , UInt, DllCall("imm32\ImmGetDefaultIMEWnd", Uint,hwnd)
;    , UInt, 0x0283  ;Message : WM_IME_CONTROL
;    ,  Int, 0x006   ;wParam  : IMC_SETOPENSTATUS
;    ,  Int, SetSts) ;lParam  : 0 or 1
;}

; }}}

; Terminal/Vim {{{
; ESC + IME
#IfWInActive, ahk_group TerminalVim
Esc:: ; Just send Esc at converting.
  if (VIM_IME_GET(A)) {
    if (VIM_IME_GetConverting(A)) {
      Send,{Esc}
    } else {
      VIM_IME_SET()
    }
  } else {
    Send,{Esc}
  }
  Return
^[:: ; Go to Normal mode (for vim) with IME off even at converting.
  if (VIM_IME_GET(A)) {
    Send,{Esc}
    Sleep 1 ; wait 1 ms (Need to stop converting)
    VIM_IME_SET()
    Send,{Esc}
  } else {
    Send,{Esc}
  }
  Return
#IfWInActive

; Paste
#IfWInActive, ahk_group Terminal
!v::
  StringSplit, strout, clipboard, `n
  If(strout0>1 or InStr(clipboard, "sudo")>0) {
    MsgBox, 308, Clipboard, %clipboard%`n`n Do you want to paste?
    IfMsgBox, No
    {
      Return
    }
  }
  Sleep,200
  MouseClick, Right, 50, 50, 1
  Return
#IfWInActive
#IfWInActive, ahk_class Vim
!v::Send,{Alt}ep
#IfWInActive

; Other than Terminal/Vim
#IfWInNotActive, ahk_group TerminalVim
^[::Send,{Esc}             ; Always C-[ to ESC, like vim
^d::Send,{Del}             ; Always Delete with C-d
#IfWInNotActive
; }}} Terminal/Vim

; Power Point {{{
#IfWInActive, ahk_class PP12FrameClass
; Select one character
^Space::Send,{Right}+{Left}

; Subscript/Superscript
^=::Send,^tb{Enter}
^+=::Send,^tp{Enter}
; For JP keyboards
;^-::Send,^tb{Enter}
;^+-::Send,^tp{Enter}

; Fonts
^+s::Send,^tfsymbol{Enter}
^+a::Send,^tfarial{Enter}
^+h::Send,^tfhelvetica{Enter}
#IfWInActive
; }}} Power Point

; Word {{{
#IfWInActive, ahk_class OpusApp
^Space::Send,{Right}+{Left}
^=::Send,^tb{Enter}
^+=::Send,^tp{Enter}
; for JP keyboards
;^-::Send,^tb{Enter}
;^+-::Send,^tp{Enter}
#IfWInActive
; }}} Word

; Explorer {{{
#IfWInActive, ahk_class CabinetWClass
; Next/Previous page
^i::Send,!{Right}
^o::Send,!{Left}
#IfWInActive
; }}} Explorer

; CLCL {{{
;#IfWInActive, ahk_class CLCLMain
#IfWInActive, CLCL ; both window/title do not working...
j::
  Msgbox, clcl_down
  Send,{Down}
  Return
K::Send,{Up}
#IfWInActive
; }}} Explorer

; Everthing {{{
#IfWInActive, ahk_class EVERYTHING
Enter::
  Send,{Enter}
  Sleep,200
  IfWinActive, ahk_class EVERYTHING
  {
    Send,{Enter}
    Sleep,200
  }
  WinActivate, ahk_class EVERYTHING
  Sleep,100
  Send,{Esc}
  Return
#IfWInActive
; }}} Everything

; Command Prompt {{{
#IfWinActive ahk_class ConsoleWindowClass
^v::
  SendInput %clipboard%
  Return
#IfWInActive
; }}} Command Prompt

; Global Settings {{{
#if
; Basic Settings {{{

; Suspend
;Esc & Tab::
;  Suspend, Toggle
;  Return
; For Alt-Ctrl Swap (swapped by KeySwap)
LCtrl & Tab::AltTab
Alt & Tab::
  if GetKeyState("Shift","P") {
    Send,^+{taB} ; dosen't work ()
  }else{
    Send,^{Tab}
  }
  Return
;!m::Send,^m
;^m::Send,!m
^m::Send,!m
Ctrl & Left::Send,!{Left}
Ctrl & Right::Send,!{Right}

; Other basic settings
!Space::Send,{vkF3sc029}   ; IME by A-Space
^Space::Send,{vkF3sc029}   ; IME by C-Space
; {vkF3sc029}              ; 変換
; {vk1Dsc07B}              ; 無変換
^h::Send,{BS}              ; Always BS with C-h
!a::Send,^{Space}n        ; Minimize window
!4::Send,!{F4}             ; Close window
^4::Send,!{F4}             ; Close window
!d::Send,{Del}             ; Always Delete with A-d
;^o::Send,!{Left}           ; Go to previous page
;^i::Send,!{Right}          ; Go to nexe page
^Space::!`                 ; IME

; For HHK
; ESC to ` like normal keyboard (ESC is placed on the left of 1 in HHK)
!1::!`                ; IME
; }}} Basic Settings

; Cursor, Mouse, Window move/size {{{
; Cursor {{{
!^h::Send,{Left}
!^j::Send,{Down}
!^k::Send,{Up}
!^l::Send,{Right}
; }}} Cursor

; Mouse {{{
; Mouse Move
!^y::MouseMove, -10, 0, 0, R
!^u::MouseMove, 0, 10, 0, R
!^i::MouseMove, 0, -10, 0, R
!^o::MouseMove, 10, 0, 0, R

; Click
!^n::MouseClick, Left
!^p::MouseClick, Right
; Right click on current window

; Mouse wheel
!^m::MouseClick, WheelDown,,,  2
!^,::MouseClick, WheelUp,,,  2
; }}} Mouse

; Window move {{{
^+y::
  WinGetPos, X, Y, , , A ;A for Active Window
  ;Msgbox, Pos At %X% %Y%
  WinMove, A, ,X-20, Y,
  ;Msgbox, Moved At %X%-20 %Y%
  Return ; Necessary for mapping which has at least 2 commands
^+u::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X, Y+20
  Return
^+i::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X, Y-20
  Return
^+o::
  WinGetPos, X, Y, , , A
  WinMove, A, ,X+20, Y
  Return
; }}} Window move

; Window size {{{
^!Enter::
  ;WinGetPos, X, Y, W, H, Program Manager ; Full Desktop
  ;Msgbox, Pos At %X% %Y% %W% %H%
  SysGet, MWA, MonitorWorkArea ; w/o Taskbar
  ;Msgbox, %MWALeft% %MWATop% %MWARight% %MWABottom%
  WinMove, A, ,MWALeft+LMargin, MWATop+TMargin
    , MWARight-MWALeft-LMargin-RMargin
    , MWABottom-MWATop-TMargin-BMargin
  Return
^!+Enter::
  ;WinGetPos, X, Y, W, H, Program Manager ; Full Desktop
  ;Msgbox, Pos At %X% %Y% %W% %H%
  SysGet, MWA, MonitorWorkArea ; w/o Taskbar
  ;Msgbox, %MWALeft% %MWATop% %MWARight% %MWABottom%
  WinMove, A, ,MWALeft+LMargin, MWATop+TMargin
    , (MWARight-MWALeft-LMargin-RMargin)*minsize
    , (MWABottom-MWATop-TMargin-BMargin)*minsize
  Return
; }}} Window size
; }}} Cursor, Mouse, Window move
; }}} Global Settings