Что нового

[Мышь, клавиатура] Как узнать текущую раскладку клавиатуры (En, Ru, Uz...) и выбрать нужное?

akmal18

Новичок
Сообщения
11
Репутация
0
Здравствуйте! Вообщем, вопрос как и в названии темы. Как узнать текущую раскладку клавиатуры (En, Ru, Uz...) и при необходимости, выбрать нужное? Поиском по форуму мы не нашли общий язык, так как тот упорно отказался мне помочь. :scratch:
Заранее спасибо.
 

kasper_sky

Antivirus
Сообщения
116
Репутация
1
akmal18
Почитайте в WinAPIEx (http://autoit-script.ru/index.php?topic=47.0):
Код:
_WinAPI_GetKeyboardLayout()
_WinAPI_SetKeyboardLayout()


Я с помощью этих функций написал мини-скрипт, который при переключении раскладки отображает окошко с языками наподобие Windows 8:
Код:
#region INCLUDES
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#Include <WinAPIEx.au3>
#Include <APIConstants.au3>
#include <Misc.au3>
#include <Array.au3>
#endregion
#region WRAPPER
#AutoIt3Wrapper_Res_Language=1049
#AutoIt3Wrapper_Res_FileVersion=1.0
#AutoIt3Wrapper_Res_Description=Переключатель языка в стиле Windows 8
#AutoIt3Wrapper_Res_LegalCopyright=Alexander Silenko 2012
#AutoIt3Wrapper_Res_Comment=Переключатель языка в стиле Windows 8
#AutoIt3Wrapper_Res_Field=OriginalFilename|Language 8 Switcher.exe
#AutoIt3Wrapper_Res_Field=ProductName|Language 8 Switcher
#AutoIt3Wrapper_Res_Field=ProductVersion|1.0
#AutoIt3Wrapper_Icon=D:\Картинки\4_Другое\Иконки\Token Light\TokenW-ICO\Office\Writing2.ico
#endregion
#region CHECK REGISTRY
If RegRead('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'SelectedColor') = '' Then
	RegWrite('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'SelectedColor', 'REG_SZ', '0xFFFFFF')
EndIf

If RegRead('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'BkSelectedColor') = '' Then
	RegWrite('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'BkSelectedColor', 'REG_SZ', '0x000000')
EndIf

If RegRead('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'ShowTime') = '' Then
	RegWrite('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'ShowTime', 'REG_SZ', '700')
EndIf
#endregion
#region VARIABLES
Global $_SelectedColor = RegRead('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'SelectedColor')
Global $_BkSelectedColor = RegRead('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'BkSelectedColor')
Global $iShowTime = Number(RegRead('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'ShowTime'))

Global $_Color = 0x000000, $_BkColor = 0xFFFFFF
Global $hUser32DLL = DllOpen("user32.dll")

Global $hCurrentWindow = WinGetHandle('[active]')
Global $iCurrentLayout = _WinAPI_GetCurrentWindowKeyboardLayout()
#endregion
#region OPTIONS
OnAutoItExitRegister('_CloseUser32DLL')
Opt('TrayMenuMode', 3)
TraySetClick(8)
#endregion


_CreateSwitcherGui()

$_Tray_Settings = TrayCreateItem('Настройки')
TrayCreateItem('')
$_Tray_Exit = TrayCreateItem('Выход')
TraySetState()
TraySetToolTip('Language 8 Switcher / AutoIt 3.3.8.1')

_WinAPI_EmptyWorkingSet()

ConsoleWrite(@LF & 'Started' & @LF)
While True
	If _LayoutChanged() Then
		If WinGetHandle('[active]') <> $hCurrentWindow Then
			$hCurrentWindow = WinGetHandle('[active]')
			$iCurrentLayout = _WinAPI_GetCurrentWindowKeyboardLayout()
		Else
			_LayoutChangedGui()
		EndIf
	EndIf

	$iTrayMsg = TrayGetMsg()
	Switch $iTrayMsg
		Case $_Tray_Settings
			_SettingsGui()

		Case $_Tray_Exit
			Exit

	EndSwitch
WEnd








Func _CreateSwitcherGui()
	Global $aiLayouts = _WinAPI_GetKeyboardLayoutListEx()
	Global $a_LayoutLabel[(UBound($aiLayouts))][3]

	Local Const $iLayerLabelHeight = 15, $iLabelHeight = 35, $iGuiCornersMargin = 10
	Local $iGuiWidth = 260
	Local $iGuiHeight = ($aiLayouts[0] * ($iLabelHeight + $iLayerLabelHeight)) + (2 * $iGuiCornersMargin)
	Local Const $iMainLabelWidth = 60, $iLabelWidth = $iGuiWidth - $iMainLabelWidth

	#region CREATE WINDOW
	Global $hGui = GuiCreate('Lang8switch', $iGuiWidth, $iGuiHeight, @DesktopWidth - $iGuiWidth - 1, Default, $WS_POPUP, $WS_EX_TOOLWINDOW)
	GUISetBkColor($_BkColor)
	GUICtrlCreateLabel('', 0, 0, $iGuiWidth, 2)					; TOP LINE
	GUICtrlSetBkColor(-1, 0x000000)
	GUICtrlCreateLabel('', 0, 0, 2, $iGuiHeight)				; LEFT LINE
	GUICtrlSetBkColor(-1, 0x000000)
	GUICtrlCreateLabel('', $iGuiWidth - 2, 0, 2, $iGuiHeight)	; RIGHT LINE
	GUICtrlSetBkColor(-1, 0x000000)
	GUICtrlCreateLabel('', 0, $iGuiHeight - 2, $iGuiWidth, 2)	; BOTTOM LINE
	GUICtrlSetBkColor(-1, 0x000000)
	#endregion
	#region CREATE CONTROLS
	Local $iY = $iGuiCornersMargin

	For $n = 1 To (UBound($aiLayouts) - 1)
		$a_LayoutLabel[$n][0] = GUICtrlCreateLabel('', 2, $iY, $iGuiWidth - 4, $iLayerLabelHeight)
		$a_LayoutLabel[$n][1] = GUICtrlCreateLabel(_ReturnLabelName($n, 1), 2, $iY + 15, $iMainLabelWidth, $iLabelHeight, $SS_RIGHT)
		GUICtrlSetFont(-1, 12, 800, 0, 'Arial')
		$a_LayoutLabel[$n][2] = GUICtrlCreateLabel(_ReturnLabelName($n, 2), $iMainLabelWidth + 2, $iY + 15, $iGuiWidth - $iMainLabelWidth - 4, $iLabelHeight, $SS_CENTER)
		GUICtrlSetFont(-1, 12, 400, 0, 'Arial')

		$iY += ($iLabelHeight + $iLayerLabelHeight)
	Next
	#endregion
	WinSetOnTop($hGui, '', 1)
EndFunc


Func _LayoutChangedGui()
	_SetLayoutIndicator()

	GUISetState(@SW_SHOWNOACTIVATE)
	Local $hTimer = TimerInit()

	While Round(TimerDiff($hTimer)) < $iShowTime
		If _LayoutChanged() Then
			_SetLayoutIndicator()
			$hTimer = TimerInit()
		EndIf

		Sleep(20)
	WEnd

	$iCurrentLayout = _WinAPI_GetCurrentWindowKeyboardLayout()
	GUISetState(@SW_HIDE)
EndFunc


Func _WinAPI_GetKeyboardLayoutListEx()
	Local $iList = _WinAPI_GetKeyboardLayoutList()

	For $i = 1 To $iList[0]
		$iList[$i] = Hex(BitAND($iList[$i], 0xFFFF))
	Next

	_ArraySort($iList, 0, 1)

	Return $iList
EndFunc


Func _SelectLabel($iNum)
	GUICtrlSetBkColor($a_LayoutLabel[$iNum][0], $_BkSelectedColor)

	GUICtrlSetColor($a_LayoutLabel[$iNum][1], $_SelectedColor)
	GUICtrlSetBkColor($a_LayoutLabel[$iNum][1], $_BkSelectedColor)

	GUICtrlSetColor($a_LayoutLabel[$iNum][2], $_SelectedColor)
	GUICtrlSetBkColor($a_LayoutLabel[$iNum][2], $_BkSelectedColor)
EndFunc


Func _UnselectLabel($iNum)
	GUICtrlSetBkColor($a_LayoutLabel[$iNum][0], $_BkColor)

	GUICtrlSetColor($a_LayoutLabel[$iNum][1], $_Color)
	GUICtrlSetBkColor($a_LayoutLabel[$iNum][1], $_BkColor)

	GUICtrlSetColor($a_LayoutLabel[$iNum][2], $_Color)
	GUICtrlSetBkColor($a_LayoutLabel[$iNum][2], $_BkColor)
EndFunc


Func _ReturnLabelName($iNum, $iLabelNum)
	For $n = 1 To $aiLayouts[0]
		If $n = $iNum Then
			If $iLabelNum = 1 Then
				Return _WinAPI_GetLocaleInfo(Number('0x' & $aiLayouts[$n]), $LOCALE_SABBREVLANGNAME)
			Else
				Return _WinAPI_GetLocaleInfo(Number('0x' & $aiLayouts[$n]), $LOCALE_SLANGUAGE)
			EndIf
		EndIf
	Next
EndFunc


Func _CloseUser32DLL()
	DllClose($hUser32DLL)
	ConsoleWrite('User32.dll CLOSED' & @LF & @LF)
EndFunc


Func _WinAPI_GetCurrentWindowKeyboardLayout()
	Return Hex(BitAND(_WinAPI_GetKeyboardLayout(WinGetHandle('[active]')), 0xFFFF))
EndFunc


Func _SetLayoutIndicator()
	For $n = 1 To $aiLayouts[0]
		If $aiLayouts[$n] = $iCurrentLayout Then
			_SelectLabel($n)
		Else
			_UnselectLabel($n)
		EndIf
	Next
EndFunc


Func _LayoutChanged()
	$iNew = _WinAPI_GetCurrentWindowKeyboardLayout()

	If $iNew <> $iCurrentLayout And $iNew <> 0 Then
		$iCurrentLayout = $iNew
		ConsoleWrite(@HOUR & ':' & @MIN & ':' & @SEC & ' New layout ' & $iNew & ' for window "' & WinGetTitle('[active]') & '"' & @LF)
		Return 1
	EndIf

	Return 0
EndFunc


Func _SettingsGui()
	#region COLOUR SCHEME CONSTANTS
	Local $_BlackScheme[2] =	['0x000000', '0xFFFFFF']	; BkColor, Color
	Local $_BlueScheme[2] =		['0x4617B4', '0xFFFFFF']
	Local $_GreenScheme[2] =	['0x1DB417', '0x000000']
	Local $_RedScheme[2] =		['0xB41B17', '0xFFFFFF']
	Local $_CyanScheme[2] =		['0x17B0B4', '0x000000']
	Local $_VioletScheme[2] =	['0x9D17B4', '0xFFFFFF']
	#endregion
	#region GUI CREATE
	Local $iGuiHeight = 150
	Local $hSettingsGui = GUICreate('Настройки Language 8 Switcher', 342, $iGuiHeight, -1, -1, $WS_SYSMENU)
	GUISetFont(12, 400, 0, 'Arial')

	Global $_SelectedSchemeCtrl = GUICtrlCreateLabel('Текущая тема оформления', 5, 5, 325, 20, $SS_CENTER)
	GUICtrlSetColor(-1, $_SelectedColor)
	GUICtrlSetBkColor(-1, $_BkSelectedColor)

	GUISetFont(11, 400, 0, 'Arial')

	GUICtrlCreateLabel('Выберите тему:', 5, 35)

	GUISetFont(12, 400, 0, 'Arial')

	$_BlackSchemeCtrl = GUICtrlCreateLabel('RUS', 5, 55, 50, 20, $SS_CENTER)
	GUICtrlSetBkColor(-1, $_BlackScheme[0])
	GUICtrlSetColor(-1, $_BlackScheme[1])

	$_BlueSchemeCtrl = GUICtrlCreateLabel('RUS', 60, 55, 50, 20, $SS_CENTER)
	GUICtrlSetBkColor(-1, $_BlueScheme[0])
	GUICtrlSetColor(-1, $_BlueScheme[1])

	$_GreenSchemeCtrl = GUICtrlCreateLabel('RUS', 115, 55, 50, 20, $SS_CENTER)
	GUICtrlSetBkColor(-1, $_GreenScheme[0])
	GUICtrlSetColor(-1, $_GreenScheme[1])

	$_RedSchemeCtrl = GUICtrlCreateLabel('RUS', 170, 55, 50, 20, $SS_CENTER)
	GUICtrlSetBkColor(-1, $_RedScheme[0])
	GUICtrlSetColor(-1, $_RedScheme[1])

	$_CyanSchemeCtrl = GUICtrlCreateLabel('RUS', 225, 55, 50, 20, $SS_CENTER)
	GUICtrlSetBkColor(-1, $_CyanScheme[0])
	GUICtrlSetColor(-1, $_CyanScheme[1])

	$_VioletSchemeCtrl = GUICtrlCreateLabel('RUS', 280, 55, 50, 20, $SS_CENTER)
	GUICtrlSetBkColor(-1, $_VioletScheme[0])
	GUICtrlSetColor(-1, $_VioletScheme[1])

	GUISetFont(11, 400, 0, 'Arial')

	$_CloseBtn = GUICtrlCreateButton('OK', 230, $iGuiHeight - 65, 100, 30)

	GUISetState()
	#endregion
	#region GUI MSGLOOP
	Do
		$iGuiMsg = GUIGetMsg()
		Switch $iGuiMsg
			Case $_BlackSchemeCtrl
				_SetSchemeColor($_BlackScheme)

			Case $_BlueSchemeCtrl
				_SetSchemeColor($_BlueScheme)

			Case $_GreenSchemeCtrl
				_SetSchemeColor($_GreenScheme)

			Case $_RedSchemeCtrl
				_SetSchemeColor($_RedScheme)

			Case $_CyanSchemeCtrl
				_SetSchemeColor($_CyanScheme)

			Case $_VioletSchemeCtrl
				_SetSchemeColor($_VioletScheme)

		EndSwitch
	Until $iGuiMsg = -3 Or $iGuiMsg = $_CloseBtn
	#endregion
	GUIDelete($hSettingsGui)
EndFunc


Func _SetSchemeColor($aiColors)
	GUICtrlSetBkColor($_SelectedSchemeCtrl, $aiColors[0])
	GUICtrlSetColor($_SelectedSchemeCtrl, $aiColors[1])

	$_BkSelectedColor = $aiColors[0]
	RegWrite('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'BkSelectedColor', 'REG_SZ', $aiColors[0])

	$_SelectedColor = $aiColors[1]
	RegWrite('HKEY_CURRENT_USER\Software\Alexander Silenko\Language 8 Switcher', 'SelectedColor', 'REG_SZ', $aiColors[1])
EndFunc
 
Автор
A

akmal18

Новичок
Сообщения
11
Репутация
0
sngr, kasper_sky
Спасибо! :IL_AutoIt_1:
 
Верх