 	PostMessage, 0x50, 0, %LangId%,, A
 }
 
-ToggleLanguage()
-{
-	Lang := LastLangId & 0xFFFF
-	If (Lang = Lang_Eng)
-	{
-		SetKeyboard(Keybd_Pes_Std)
-	}
-	Else If (Lang = Lang_Pes Or Lang = Lang_Arb)
-	{
-		SetKeyboard(Keybd_Eng_DV)
-	}
-	Else
-	{
-		MsgBox Unknown language.
-	}
-}
-
-ToggleKeyboard(Direction)
-{
-	Lang := LastLangId & 0xFFFF
-	If (Lang = Lang_Eng)
-	{
-		If (Direction <> "")
-		{
-			If (Direction = "Left")
-			{
-				SetKeyboard(Keybd_Eng_DVL)
-			}
-			Else If (Direction = "Right")
-			{
-				SetKeyboard(Keybd_Eng_DVR)
-			}
-			Else
-			{
-				MsgBox Invalid Direction.
-			}
-		}
-		Else
-		{
-			If (LastLangId = Keybd_Eng_US)
-			{
-				SetKeyboard(Keybd_Eng_DV)
-			}
-			Else If (LastLangId = Keybd_Eng_DV Or LastLangId = Keybd_Eng_DVL Or LastLangId = Keybd_Eng_DVR)
-			{
-				SetKeyboard(Keybd_Eng_US)
-			}
-			Else
-			{
-				MsgBox Unknown keyboard.
-			}
-		}
-	}
-	Else If (Lang = Lang_Pes Or Lang = Lang_Arb)
-	{
-		If (Direction <> "")
-		{
-			MsgBox No directional keyboard defined.
-		}
-		Else
-		{
-			If (LastLangId = Keybd_Pes_Std)
-			{
-				SetKeyboard(Keybd_Pes)
-			}
-			Else If (LastLangId = Keybd_Pes)
-			{
-				SetKeyboard(Keybd_Arb_101)
-			}
-			Else If (LastLangId = Keybd_Arb_101)
-			{
-				SetKeyboard(Keybd_Pes_Std)
-			}
-			Else
-			{
-				MsgBox Unknown keyboard.
-			}
-		}
-	}
-	Else
-	{
-		MsgBox Unknown language.
-	}
-}
+ToggleKeyboard(Kind)
+{
+	Loop % KeyboardsData.Length()
+	{
+		I := A_Index
+		D := KeyboardsData[I]
+		If (FindKey(D.AllKeyboards, LastLangId))
+		{
+			Break
+		}
+	}
+
+	If (!I)
+	{
+		MsgBox, Invalid script state.
+		Return
+	}
+
+	If (!Kind)
+	{
+		I := Mod(I, KeyboardsData.Length()) + 1
+		D := KeyboardsData[I]
+		SetKeyboard(D.Keyboards["Normal"][1])
+		Return
+	}
+
+	S := D.Keyboards[Kind]
+
+	If (S.Length() = 0)
+	{
+		MsgBox, No keyboards for '%Kind%'.
+	}
+
+	J := FindKey(S, LastLangId)
+	If (!J)
+	{
+		J := 0
+	}
+	J := Mod(J, S.Length()) + 1
+	SetKeyboard(S[J])
+}
+
+InitKeyboardsData()
 
 ; ====================================================================
 

#SingleInstance Force
SetTitleMatchMode, RegEx
SetCapsLockState, AlwaysOff

; ====================================================================


Keybd_Pes_Std := 0xF03A0429
Keybd_Arb_101 := 0x04010401

KeyboardsData := [{}
	,{Keyboards: {Normal: [Keybd_Eng_DV, Keybd_Eng_US]
			,Left: [Keybd_Eng_DVL]
			,Right: [Keybd_Eng_DVR]}}
	,{Keyboards: {Normal: [Keybd_Pes_Std, Keybd_Pes, Keybd_Arb_101]
			,Left: []
			,Right: []}}
,{}]

LastLangId := 0

Global KeyboardsData
Global LastLangId

InitKeyboardsData()
{
	KeyboardsData.RemoveAt(1)
	KeyboardsData.RemoveAt(KeyboardsData.Length())

	Loop % KeyboardsData.Length()
	{
		D := KeyboardsData[A_Index]

		D.AllKeyboards := []

		If (Not (D.Keyboards.HasKey("Normal") And D.Keyboards.HasKey("Left") And D.Keyboards.HasKey("Right") And Count(D.Keyboards) = 3))
		{
			MsgBox, % "Keyboards does not have all the 'Normal', 'Left' and 'Right' keys or has extra keys."
			ExitApp, 1
		}

		For K, L In D.Keyboards
		{
			PushRange(D.AllKeyboards, L)
		}
	}

	SetKeyboard(KeyboardsData[1].Keyboards["Normal"][1])
}

SetKeyboard(LangId)
{
