
'--------------------------Окно HotKey-----------------------------------------

Sub Obj.Hotkey()
	HotKeyHWND=OpenWindow("Горячие клавиши",100,100,320,115,WS_POPUP)
	CenterWindow(HotKeyHWND):WindowColor(HotKeyHWND,&h7D828E)
	GroupGadget(GroupHotkey,15,5,290,100,"Горячие клавиши")
	StringGadget(StringHotkeyPATH,30,30,230,22,"Выбрать путь до программы",ES_AUTOHSCROLL Or ES_READONLY,WS_EX_CLIENTEDGE)
	StringGadget(StringHotkeyControl,65,66,170,22,"",ES_READONLY,WS_EX_CLIENTEDGE)
	ListViewGadget(ListViewHotKey,10,120,300,199,LVS_EX_FULLROWSELECT Or LVS_EX_GRIDLINES,LVS_REPORT Or WS_BORDER ,WS_EX_CLIENTEDGE)
	AddListViewColumn(ListViewHotKey, "Программа",0,1,100)
	AddListViewColumn(ListViewHotKey, "Клавиша(ы)",1,2,100)
	AddListViewColumn(ListViewHotKey, "Путь",2,3,100)
	AddListViewColumn(ListViewHotKey, "mod",3,4,100)
	AddListViewColumn(ListViewHotKey, "virt",4,5,100)
	AddListViewColumn(ListViewHotKey, "№",5,6,100)
	ButtonGadget(ButtonHotKeyPath,265,31,20,20,"...")
	ButtonGadget(ButtonHotKeyClear,240,70,17,17,"X")
	ButtonImageGadget(ButtonHotkeyS,25,75,20,20,Cast(Integer,LoadImageFromResource(MAKEINTRESOURCE(101))))
	ButtonGadget(ButtonHotkeyOK,270,75,25,20,"Ok")
	TextGadget(TextHotkeyExit,305,-1,15,15,"X",SS_CENTER)
	GadgetToolTip(ButtonHotKeyPath,"Выбрать путь к вашей программе")
	GadgetToolTip(ButtonHotKeyClear,"Очистить строку")
	GadgetToolTip(ButtonHotkeyS,"Открыть список с клавишами")
	GadgetToolTip(ButtonHotkeyOK,"Потвердить добавление клавиши")
	GadgetToolTip(TextHotkeyExit,"Закрыть окно")
	SetGadgetColor(GroupHotkey,&h7D828E,0,1)
	SetGadgetColor(StringHotkeyPATH,&h7D828E,&hFF80FF,3)
	SetGadgetColor(StringHotkeyControl,&h7D828E,&hFCAA9E,3)
	SetGadgetColor(StringHotkeyControl,&h7D828E,&hFCAA9E,3)
	SetGadgetColor(TextHotkeyExit,&h7D828E,255,3)
	SetGadgetColor(ListViewHotKey,&h7D828E,&hFCAA9E,3)
End Sub

'Окно отлавливающее сообщения нажатия клавиш в HotKeyControl

Sub Obj.EventHotkey(Byval param As Any Ptr = 0)
	Static b as UByte=8
	Do
		If GetAsyncKeyState(b)<0 Then
			If a.Hot_Key.Key="" Then
				Select Case b
					Case VK_SHIFT
						a.Hot_Key.Shift="SHIFT + "
						a.Hot_Key.tempmodkey Or= 4
					Case VK_CONTROL
						a.Hot_Key.Ctrl="CTRL + "
						a.Hot_Key.tempmodkey Or= 2
					Case VK_MENU
						a.Hot_Key.Alt="ALT + "
						a.Hot_Key.tempmodkey Or= 1
					Case VK_TAB
						a.Hot_Key.key="TAB"
					Case 19
						a.Hot_Key.Key="BREAK"
					Case 13 ' Enter ничего не делаем
					Case 33 To 36
						a.Hot_Key.Key=a.Hot_Key.DimKey0(b)
					Case 45 To 46
						a.Hot_Key.Key=a.Hot_Key.DimKey1(b)
					Case 96 To 111
						a.Hot_Key.Key=a.Hot_Key.DimKey2(b)
					Case 186 To 192
						a.Hot_Key.Key=a.Hot_Key.DimKey3(b)
					Case 219 To 222
						a.Hot_Key.Key=a.Hot_Key.DimKey4(b)
					Case Else
						a.Hot_Key.Key=CHR(b)
				End Select
				SetGadgetText(StringHotkeyControl,a.Hot_Key.Shift & a.Hot_Key.Ctrl _
				& a.Hot_Key.Alt & a.Hot_Key.Key)
				If a.Hot_Key.Key<>"" Then
					a.Hot_Key.tempvirtkey=b
				EndIf
			EndIf
		EndIf
		b+=1
		If b=19 Then
			b=30
		ElseIf b=136 Then
			b=186
		ElseIf b=193 Then
			b=219
		ElseIf b=223 Then
			b=8
		EndIf
		SleepW9(10)
	Loop Until a.Hot_Key.Hide=1
End Sub

' Очистка флагов и HotKey контрола

Sub Obj.Clear
	Hot_Key.Shift=""
	Hot_Key.Ctrl=""
	Hot_Key.Alt=""
	Hot_Key.Key=""
	Hot_Key.tempmodkey=0
	Hot_Key.tempvirtkey=0
	Tdir_=""
	SetGadgetText(StringHotkeyControl,"")
End Sub


' Загрузка  и сохранение из файла( или в файл) клавиш в ListView

Sub Obj.LoadSaveHotkey(flag As bool)
	If flag = 0 Then ' чтение
		If OpenFBini("U2.ini")<>-1 Then
			Hot_Key.CountHotKey=ReadIntegerValueFBini("HotKey","CountHotKey")
			If Hot_Key.CountHotKey<>0 Then
				For n As Integer=1 To Hot_Key.CountHotKey
					AddListViewItem(ListViewHotKey,ReadStringValueFBini("GR_" & n,"program"),0,n-1,0)
					AddListViewItem(ListViewHotKey,ReadStringValueFBini("GR_" & n,"keyS"),0,n-1,1)
					AddListViewItem(ListViewHotKey,ReadStringValueFBini("GR_" & n,"path"),0,n-1,2)
					AddListViewItem(ListViewHotKey,ReadStringValueFBini("GR_" & n,"ModKey"),0,n-1,3)
					AddListViewItem(ListViewHotKey,ReadStringValueFBini("GR_" & n,"VirtKey"),0,n-1,4)
					AddListViewItem(ListViewHotKey,ReadStringValueFBini("GR_" & n,"NumKey"),0,n-1,5)
					RegisterHotKey(HotKeyHWND,n,Val(ReadStringValueFBini("GR_" & n,"ModKey")),Val(ReadStringValueFBini("GR_" & n,"VirtKey")))
				Next
			EndIf
			CloseFBini()
		Else
			LoadSaveHotkey(1)
		EndIf
		RegisterHotKey(HotKeyHWND,1000,0,VK_SNAPSHOT)
		RegisterHotKey(HotKeyHWND,1001,MOD_ALT,VK_SNAPSHOT)
		RegisterHotKey(HotKeyHWND,1002,MOD_CONTROL,VK_SNAPSHOT)
		RegisterHotKey(HotKeyHWND,1003,MOD_SHIFT,VK_SNAPSHOT)
		RegisterHotKey(HotKeyHWND,1004,MOD_ALT Or MOD_CONTROL,VK_SNAPSHOT)
		RegisterHotKey(HotKeyHWND,1005,MOD_CONTROL Or MOD_SHIFT,VK_SNAPSHOT)
	Else ' запись
		If CreateFBini("U2.ini")<> -1 Then
			If Hot_Key.CountHotKey=0 Then
				WriteGroupFBini("HotKey")
				WriteValueFBini("HotKey","CountHotKey",Cast(Integer,0))
			Else
				For n As Integer=1 To Hot_Key.CountHotKey
					WriteGroupFBini("GR_" & n)
					WriteValueFBini("GR_" & n,"program",GetTextItemListView(ListViewHotKey,n-1,0))
					WriteValueFBini("GR_" & n,"keyS",GetTextItemListView(ListViewHotKey,n-1,1))
					WriteValueFBini("GR_" & n,"path",GetTextItemListView(ListViewHotKey,n-1,2))
					WriteValueFBini("GR_" & n,"ModKey",GetTextItemListView(ListViewHotKey,n-1,3))'модификация клавиши
					WriteValueFBini("GR_" & n,"VirtKey",GetTextItemListView(ListViewHotKey,n-1,4))'сама клавиша
					WriteValueFBini("GR_" & n,"NumKey",Str(n))'номер клавиши
					WriteValueFBini("HotKey","CountHotKey",Cast(Integer,Hot_Key.CountHotKey))
				Next
				WriteGroupFBini("HotKey")
				WriteValueFBini("HotKey","CountHotKey",Cast(Integer,Hot_Key.CountHotKey))
			EndIf
			CloseFBini()
		EndIf
	EndIf
End Sub

' Процедура установки новых горячих клавиш

Sub Obj.NewHotKey()
	If GetGadgetText(StringHotkeyPATH)="Выбрать путь до программы" Or GetGadgetText(StringHotkeyPATH)="" Then
		MessBox("Сообщение","Введите путь")
		Exit sub
	EndIf
	If GetGadgetText(StringHotkeyControl)="" Then
		MessBox("Сообщение","Введите горячую клавишу")
		Exit sub
	EndIf
	Tdir_=GetFilePart(GetGadgetText(StringHotkeyPATH))
	If Tdir_="" Or Tdir_=" " Then Tdir_=GetGadgetText(StringHotkeyPATH)
	AddListViewItem(ListViewHotKey,Tdir_,0,Hot_Key.CountHotKey,0)
	AddListViewItem(ListViewHotKey,GetGadgetText(StringHotkeyControl),0,Hot_Key.CountHotKey,1)
	AddListViewItem(ListViewHotKey,GetGadgetText(StringHotkeyPATH),0,Hot_Key.CountHotKey,2)
	AddListViewItem(ListViewHotKey,Str(Hot_Key.tempmodkey),0,Hot_Key.CountHotKey,3)
	AddListViewItem(ListViewHotKey,Str(Hot_Key.tempvirtkey),0,Hot_Key.CountHotKey,4)
	AddListViewItem(ListViewHotKey,Str(Hot_Key.CountHotKey+1),0,Hot_Key.CountHotKey,5)
	RegisterHotKey(HotKeyHWND,Hot_Key.CountHotKey+1,Hot_Key.tempmodkey,Hot_Key.tempvirtkey)
	Hot_Key.CountHotKey+=1
	LoadSaveHotkey(1)
	Clear()
End Sub

' Запуск по горячим клавишам

Sub Obj.HotKeyRun(param As Integer)
	For n As Integer=0 To Hot_Key.CountHotKey-1
		If Val(GetTextItemListView(ListViewHotKey,n,5)) = param Then
			Tdir_=GetTextItemListView(ListViewHotKey,n,2)
			If InStr(Tdir_,".")<>0 Then
				RunProgram(Tdir_,,GetPathPart(Tdir_))
			Else
				RunProgram(Tdir_)
			EndIf
		EndIf
	Next
End Sub

'Полная перерегистрация
'Приходится удалять всю регистрацию, очищать все пункты, и заново загружаться
'Если удалять по одному пункту, то слетает регистрация всех последующих клавиш
'Может это конечно из-за глючности моей системы, но лучше наверняка делать

Sub Obj.DeleteHotKey()
	DeleteItemListView(ListViewHotKey,GetItemListView())
	UnRegHotKey()
	Hot_Key.CountHotKey-=1
	LoadSaveHotkey(1)
	DeleteListViewItemsAll(ListViewHotKey)
	LoadSaveHotkey(0)
End Sub

' Отмена регистрации всех клавиш

Sub Obj.UnRegHotKey()
	If Hot_Key.CountHotKey<>0 Then
		For n As Integer=1 To Hot_Key.CountHotKey
			UnregisterHotKey(HotKeyHWND,n)
		Next
		For n As Integer=1000 To 1005
			UnregisterHotKey(HotKeyHWND,n)
		Next
	EndIf
End Sub

