Page 1 of 1

Hotkey chosen by user and saved in ini file

Posted: 13 Dec 2013, 09:14
by Joe Glines
I would like to be able to ask users what hotkey they want to associate to a given task, save it to an ini file, and then use what they saved in future scripts. Ideally I'd like to make it as simple as possible for people that are not typical AutoHotKey users.

I think creating a gui with radio buttons listing frequently used modifier keys (Control, Shift, Windows,Alt) and asking for a letter and/or number. Does anyone have anything like this? I wrote the following which works okay however I believe (even with a lot of direction) some non-AutoHotKey users might find it confusing.

Code: Select all

#SingleInstance, Force
#NoEnv

Gui,Add,Hotkey,vTheHotkey
Gui,Add,Button,,OK
Gui,Show
Return

ButtonOK:
  Gui,Submit ;upon submision write to to an ini file
  IniWrite, %TheHotKey%,Hotkey.ini,MyHotkey,Hotkey1 ;Write it to an ini file for future use
  ;*******************************************************.
  MsgBox It was just written to an ini file`n`n Now read it in and assign it to a label
    ;*******************************************************.
  IniRead, ChosenHotkey1,Hotkey.ini,MyHotKey,Hotkey1
Hotkey, %ChosenHotkey1%, MyLabel
Return

MyLabel:
MsgBox % "Wow this worked.`n`nYou pressed: " ChosenHotkey1
return
  

Re: Hotkey chosen by user and saved in ini file

Posted: 13 Dec 2013, 10:58
by Chef
Found this one time on GitHub and saved it, don't have the link anymore, so credit to whoever wrote it...

Code: Select all

hotkeyGUI( p_Owner="",p_Hotkey="",p_Limit="",p_OptionalAttrib=False,p_Title="" ) {
	static s_Gui := 0
	        ,s_StartGUI := 53
	        ,s_PopupXY_Function := "PopupXY"
	If s_Gui
	{
		Errorlevel := "FAIL"
		OutputDebug,
			(ltrim join`s
			End Func: %A_ThisFunc% -
			A %A_ThisFunc% window already exists.  Errorlevel=FAIL
			)
		Return
	}
	SplitPath A_ScriptName,,,,l_ScriptName
	l_ErrorLevel := 0
	l_StandardKeysList=
		(ltrim join|
		A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
		0|1|2|3|4|5|6|7|8|9|0
		``|-|=|[|]|`\|;
		'|,|.|/
		Space
		Tab
		Enter
		Escape
		Backspace
		Delete
		ScrollLock
		CapsLock
		NumLock
		PrintScreen
		CtrlBreak
		Pause
		Break
		Insert
		Home
		End
		PgUp
		PgDn
		Up
		Down
		Left
		Right
		)
	l_FunctionKeysList=
		(ltrim join|
		F1|F2|F3|F4|F5|F6|F7|F8|F9|F10
		F11|F12|F13|F14|F15|F16|F17|F18|F19|F20
		F21|F22|F23|F24
		)
	l_NumpadKeysList=
		(ltrim join|
		NumLock
		NumpadDiv
		NumpadMult
		NumpadAdd
		NumpadSub
		NumpadEnter
		NumpadDel
		NumpadIns
		NumpadClear
		NumpadUp
		NumpadDown
		NumpadLeft
		NumpadRight
		NumpadHome
		NumpadEnd
		NumpadPgUp
		NumpadPgDn
		Numpad0
		Numpad1
		Numpad2
		Numpad3
		Numpad4
		Numpad5
		Numpad6
		Numpad7
		Numpad8
		Numpad9
		NumpadDot
		)
	l_MouseKeysList=
		(ltrim join|
		LButton
		RButton
		MButton
		WheelDown
		WheelUp
		XButton1
		XButton2
		)
	l_MultimediaKeysList=
		(ltrim join|
		Browser_Back
		Browser_Forward
		Browser_Refresh
		Browser_Stop
		Browser_Search
		Browser_Favorites
		Browser_Home
		Volume_Mute
		Volume_Down
		Volume_Up
		Media_Next
		Media_Prev
		Media_Stop
		Media_Play_Pause
		Launch_Mail
		Launch_Media
		Launch_App1
		Launch_App2
		)
	l_SpecialKeysList := "Help|Sleep"
	p_Owner=%p_Owner%
	If p_Owner is not Integer
		p_Owner := 0
	Else If p_Owner not Between 1 and 99
		p_Owner := 0
	If p_Owner
	{
		Gui %p_Owner%:+LastFoundExist
		IfWinNotExist
		{
			OutputDebug,
				(ltrim join`s
				Function: %A_ThisFunc% -
				Owner window does not exist.  p_Owner=%p_Owner%
				)
			p_Owner := 0
		}
	}
	l_Hotkey=%p_Hotkey%
	p_Limit=%p_Limit%
	If p_Limit is not Integer
		p_Limit := 0
	Else If p_Limit not Between 0 and 2047
			p_Limit := 0
	p_Title=%p_Title%
	If p_Title is Space
		p_Title := l_ScriptName . " - Select Hotkey"
	Else
	{
		If SubStr(p_Title,1,2)="++"
		{
			StringTrimLeft p_Title,p_Title,2
			p_Title := l_ScriptName . A_Space . p_Title
		}
	}
	s_Gui := s_StartGui
	Loop
	{
		Gui %s_Gui%:+LastFoundExist
		IfWinNotExist
			Break
		If (s_Gui=99)
		{
			MsgBox
			        ,262160
			        ,%A_ThisFunc% Error,
				(ltrim join`s
				Unable to create a %A_ThisFunc% window.  GUI windows
				%s_StartGUI% to 99 are already in use.  %A_Space%
				)
			s_Gui := 0
			ErrorLevel := "FAIL"
			Return
		}
		s_Gui++
	}
	If p_Owner
	{
		Gui %p_Owner%:+Disabled
		Gui %s_Gui%:+Owner%p_Owner%
	}
	Else
		Gui %s_Gui%:+Owner
	Gui %s_Gui%:Margin,6,6
	Gui %s_Gui%:-MinimizeBox +LabelHotkeyGui_
	If not p_Owner
		Gui %s_Gui%:+AlwaysOnTop
	static HG_ModifierGB
	Gui %s_Gui%:Add,GroupBox, xm y10 w170 h10 vHG_ModifierGB,Modifier
	static HG_CtrlModifier
	Gui %s_Gui%:Add,CheckBox,xp+10 yp+20 Section vHG_CtrlModifier gHotkeyGUI_UpdateHotkey,Ctrl
	static HG_ShiftModifier
	Gui %s_Gui%:Add,CheckBox,xs vHG_ShiftModifier gHotkeyGUI_UpdateHotkey,Shift
	static HG_WinModifier
	Gui %s_Gui%:Add,CheckBox,xs vHG_WinModifier gHotkeyGUI_UpdateHotkey,Win
	static HG_AltModifier
	Gui %s_Gui%:Add,CheckBox,xs vHG_AltModifier gHotkeyGUI_UpdateHotkey,Alt
	static HG_OptionalAttributesGB
	Gui %s_Gui%:Add,GroupBox,xs+160 y10 w170 h10 vHG_OptionalAttributesGB,Optional Attributes
	static HG_NativeOption
	Gui %s_Gui%:Add,CheckBox,xp+10 yp+20 Disabled Section vHG_NativeOption gHotkeyGUI_UpdateHotkey,~ (Native)
	static HG_WildcardOption
	Gui %s_Gui%:Add,CheckBox,xs Disabled vHG_WildcardOption gHotkeyGUI_UpdateHotkey,*  (Wildcard)
	static HG_LeftPairOption
	Gui %s_Gui%:Add,CheckBox,xs Disabled vHG_LeftPairOption gHotkeyGUI_LeftPair,< (Left pair only)
	static HG_RightPairOption
	Gui %s_Gui%:Add,CheckBox,xs Disabled vHG_RightPairOption gHotkeyGUI_RightPair,> (Right pair only)
	If p_OptionalAttrib
	{
		GuiControl %s_Gui%:Enable,HG_NativeOption
		GuiControl %s_Gui%:Enable,HG_WildcardOption
		GuiControl %s_Gui%:Enable,HG_LeftPairOption
		GuiControl %s_Gui%:Enable,HG_RightPairOption
	}
	GuiControlGet $Group1Pos,%s_Gui%:Pos,HG_OptionalAttributesGB
	GuiControlGet $Group2Pos,%s_Gui%:Pos,HG_RightPairOption
	GuiControl,%s_GUI%:Move,HG_ModifierGB,% "h" . ($Group2PosY-$Group1PosY)+$Group2PosH+10
	GuiControl,%s_GUI%:Move,HG_OptionalAttributesGB,% "h" . ($Group2PosY-$Group1PosY)+$Group2PosH+10
	YPos := ($Group2PosY-$Group1PosY)+$Group2PosH+20
	Gui %s_Gui%:Add,GroupBox,xm y%YPos% w340 h180,Keys
	static HG_StandardKeysView
	Gui %s_Gui%:Add,Radio,xp+10 yp+20 Checked Section vHG_StandardKeysView gHotkeyGUI_UpdateKeyList,Standard
	static HG_FunctionKeysView
	Gui %s_Gui%:Add,Radio,xs vHG_FunctionKeysView gHotkeyGUI_UpdateKeyList,Function keys
	static HG_NumpadKeysView
	Gui %s_Gui%:Add,Radio,xs vHG_NumpadKeysView gHotkeyGUI_UpdateKeyList,Numpad
	static HG_MouseKeysView
	Gui %s_Gui%:Add,Radio,xs vHG_MouseKeysView gHotkeyGUI_UpdateKeyList,Mouse
	static HG_MultimediaKeysView
	Gui %s_Gui%:Add,Radio,xs vHG_MultimediaKeysView gHotkeyGUI_UpdateKeyList,Multimedia
	static HG_SpecialKeysView
	Gui %s_Gui%:Add,Radio,xs vHG_SpecialKeysView gHotkeyGUI_UpdateKeyList,Special
	static HG_Key
	Gui %s_Gui%:Add,ListBox,xs+140 ys w180 h150 vHG_Key gHotkeyGUI_UpdateHotkey
	Gosub HotkeyGUI_UpdateKeyList
	YPos+=190
	Gui %s_Gui%:Add,Text,xm y%YPos% w70,Hotkey:
	Gui %s_Gui%:Add,Edit,x+0 w270 +ReadOnly
	Gui %s_Gui%:Add,Text,xm y+5 w70 r2 hp,Desc:
	Gui %s_Gui%:Add,Text,x+0 w270 hp +ReadOnly,None
	static HG_AcceptButton
	Gui %s_Gui%:Add,Button,xm y+5 Default Disabled vHG_AcceptButton gHotkeyGUI_AcceptButton,%A_Space% &Accept %A_Space%
	Gui %s_Gui%:Add,Button,x+5 wp hp gHotkeyGUI_Close,Cancel
	If l_Hotkey is not Space
	{
		Loop
		{
			l_FirstChar := SubStr(l_Hotkey,1,1)
			If l_FirstChar in ^,+,#,!,~,*,<,>
			{
				If (l_FirstChar="^")
					GuiControl %s_Gui%:,HG_CtrlModifier,1
				Else If (l_FirstChar="+")
					GuiControl %s_Gui%:,HG_ShiftModifier,1
				Else If (l_FirstChar="#")
					GuiControl %s_Gui%:,HG_WinModifier,1
				Else If (l_FirstChar="!")
					GuiControl %s_Gui%:,HG_AltModifier,1
				Else  If (l_FirstChar="~")
					GuiControl %s_Gui%:,HG_NativeOption,1
				Else If (l_FirstChar="*")
					GuiControl %s_Gui%:,HG_WildcardOption,1
				Else If (l_FirstChar="<")
					GuiControl %s_Gui%:,HG_LeftPairOption,1
				Else If (l_FirstChar=">")
					GuiControl %s_Gui%:,HG_RightPairOption,1
				StringTrimLeft l_Hotkey,l_Hotkey,1
				Continue
			}
			Break
		}
		If l_Hotkey is not Space
		{
			If InStr("|" . l_StandardKeysList . "|","|" . l_Hotkey . "|")
				GuiControl %s_Gui%:,HG_StandardKeysView,1
			Else If InStr("|" . l_FunctionKeysList . "|","|" . l_Hotkey . "|")
				GuiControl %s_Gui%:,HG_FunctionKeysView,1
			Else If InStr("|" . l_NumpadKeysList . "|","|" . l_Hotkey . "|")
				GuiControl %s_Gui%:,HG_NumpadKeysView,1
			Else If InStr("|" . l_MouseKeysList . "|","|" . l_Hotkey . "|")
				GuiControl %s_Gui%:,HG_MouseKeysView,1
			Else If InStr("|" . l_MultimediaKeysList . "|","|" . l_Hotkey . "|")
				GuiControl %s_Gui%:,HG_MultimediaKeysView,1
			Else If InStr("|" . l_SpecialKeysList . "|","|" . l_Hotkey . "|")
				GuiControl %s_Gui%:,HG_SpecialKeysView,1
			Gosub HotkeyGUI_UpdateKeyList
			GuiControl %s_Gui%:ChooseString,HG_Key,%l_Hotkey%
		}
		Gosub HotkeyGUI_UpdateHotkey
	}
	GuiControl %s_Gui%:Focus,HG_AcceptButton
	Gui %s_Gui%:+LastFound
	WinGet l_HotkeyGui_hWnd,ID
	If p_Owner and IsFunc(s_PopupXY_Function)
	{
		Gui %s_Gui%:Show,Hide,%p_Title%
		%s_PopupXY_Function%(p_Owner,"ahk_id " . l_HotkeyGui_hWnd,PosX,PosY)
		Gui %s_Gui%:Show,x%PosX% y%PosY%
	}
	Else
		Gui %s_Gui%:Show,,%p_Title%
	WinWaitClose ahk_id %l_HotkeyGui_hWnd%
	ErrorLevel := l_ErrorLevel
	Return HG_Hotkey
	
	HotkeyGUI_UpdateHotkey:
		Gui %s_Gui%:Submit,NoHide
		If HG_Key
			GuiControl %s_Gui%:Enable,Button18
		Else GuiControl %s_Gui%:Disable,Button18
		If HG_Key in Pause,Break
			If HG_CtrlModifier
				HG_Key := "CtrlBreak"
		If (HG_Key="CtrlBreak")
			If not HG_CtrlModifier
				HG_Key := "Pause"
		HG_Hotkey := ""
		HG_HKDesc := ""
		If HG_NativeOption
			HG_Hotkey.="~"
		If HG_WildcardOption
			HG_Hotkey.="*"
		If HG_LeftPairOption
			HG_Hotkey.="<"
		If HG_RightPairOption
			HG_Hotkey.=">"
		If HG_CtrlModifier
		{
			HG_Hotkey.="^"
			HG_HKDesc.="Ctrl + "
		}
		If HG_ShiftModifier
		{
			HG_Hotkey.="+"
			HG_HKDesc.="Shift + "
		}
		If HG_WinModifier
		{
			HG_Hotkey.="#"
			HG_HKDesc.="Win + "
		}
		If HG_AltModifier
		{
			HG_Hotkey.="!"
			HG_HKDesc.="Alt + "
		}
		HG_Hotkey.=HG_Key
		HG_HKDesc.=HG_Key
		GuiControl %s_Gui%:,Edit1,%HG_Hotkey%
		GuiControl %s_Gui%:,static3,%HG_HKDesc%
		Return
		
	HotkeyGUI_LeftPair:
		GuiControl %s_Gui%:,Button10,0
		Gosub HotkeyGUI_UpdateHotkey
		Return
		
	HotkeyGUI_RightPair:
		GuiControl %s_Gui%:,Button9,0
		Gosub HotkeyGUI_UpdateHotkey
		Return
		
	HotkeyGUI_UpdateKeyList:
		Gui %s_Gui%:Submit,NoHide
		If HG_StandardKeysView
			l_KeysList := l_StandardKeysList
		Else
			If HG_FunctionKeysView
				l_KeysList := l_FunctionKeysList
			Else
				If HG_NumpadKeysView
					l_KeysList := l_NumpadKeysList
				Else
					If HG_MouseKeysView
						l_KeysList := l_MouseKeysList
					Else
						If HG_MultimediaKeysView
							l_KeysList := l_MultimediaKeysList
						Else
							If HG_SpecialKeysView
								l_KeysList := l_SpecialKeysList
		GuiControl %s_Gui%:-Redraw,ListBox1
		GuiControl %s_Gui%:,ListBox1,|%l_KeysList%
		GuiControl %s_Gui%:+Redraw,ListBox1
		HG_Key := ""
		Gosub HotkeyGUI_UpdateHotkey
		Return
		
	HotkeyGUI_AcceptButton:
		If HG_Key is Space
		{
			Gui %s_Gui%:+OwnDialogs
			MsgBox,16,%p_Title%,A key must be selected.  %A_Space%
			Return
		}
		l_Limit := p_Limit
		l_LimitFailure := False
		Loop
		{
			If (l_Limit<=0)
				Break
			If (l_Limit>=1024)
			{
				If (HG_ShiftModifier and HG_WinModifier and HG_AltModifier)
				{
					l_Message := "Shift+WIN+ALT keys are not allowed."
					l_LimitFailure := True
					Break
				}
				l_Limit-=1024
				Continue
			}
			If (l_Limit>=512)
			{
				If (HG_ShiftModifier and HG_CtrlModifier and HG_WinModifier)
				{
					l_Message := "Shift+CTRL+WIN keys are not allowed."
					l_LimitFailure := True
					Break
				}
				l_Limit-=512
				Continue
			}
			If (l_Limit>=256)
			{
				If (HG_ShiftModifier and HG_CtrlModifier and HG_AltModifier)
				{
					l_Message := "Shift+CTRL+ALT keys are not allowed."
					l_LimitFailure := True
					Break
				}
				l_Limit-=256
				Continue
			}
			If (l_Limit>=128)
			{
				If (HG_ShiftModifier and HG_WinModifier)
				{
					l_Message := "Shift+WIN keys are not allowed."
					l_LimitFailure := True
					Break
				}
				l_Limit-=128
				Continue
			}
			If (l_Limit>=64)
			{
				If (HG_ShiftModifier and HG_AltModifier)
				{
					l_Message := "Shift+ALT keys are not allowed."
					l_LimitFailure := True
					Break
				}
				l_Limit-=64
				Continue
			}
			If (l_Limit>=32)
				{
				If (HG_ShiftModifier and HG_CtrlModifier)
				{
					l_Message := "Shift+CTRL keys are not allowed."
					l_LimitFailure := True
					Break
				}
				l_Limit-=32
				Continue
				}
			If (l_Limit>=16)
			{
				If (HG_WinModifier and not (HG_CtrlModifier or HG_ShiftModifier or HG_AltModifier))
				{
					l_Message := "WIN-only keys are not allowed."
					l_LimitFailure := True
					Break
				}
				l_Limit-=16
				Continue
			}
			If (l_Limit>=8)
			{
				If (HG_AltModifier
							and not (HG_CtrlModifier or HG_ShiftModifier or HG_WinModifier))
				{
					l_Message := "ALT-only keys are not allowed."
					l_LimitFailure := True
					Break
				}
				l_Limit-=8
				Continue
			}
			If (l_Limit>=4)
			{
				If (HG_CtrlModifier and not (HG_ShiftModifier or HG_WinModifier or HG_AltModifier))
				{
					l_Message := "CTRL-only keys are not allowed."
					l_LimitFailure := True
					Break
				}
				l_Limit-=4
				Continue
			}
			If (l_Limit>=2)
			{
				If (HG_ShiftModifier and not (HG_CtrlModifier or HG_WinModifier or HG_AltModifier))
				{
					l_Message := "Shift-only keys are not allowed."
					l_LimitFailure := True
					Break
				}
				l_Limit-=2
				Continue
			}
			If (l_Limit>=1)
			{
				If not (HG_CtrlModifier or  HG_ShiftModifier or  HG_WinModifier or  HG_AltModifier)
				{
					l_Message=
						(ltrim join`s
						At least one modifier must be used.  Other restrictions
						may apply.
						)
					l_LimitFailure := True
					Break
				}
				l_Limit-=1
				Continue
			}
		}
		If l_LimitFailure
		{
			Gui %s_Gui%:+OwnDialogs
			MsgBox,16,%p_Title%,%l_Message%  %A_Space%
			Return
		}
		Gosub HotkeyGUI_Exit
	Return
	
	HotkeyGUI_Escape:
	HotkeyGUI_Close:
		HG_Hotkey := p_Hotkey
		l_ErrorLevel := 1
	HotkeyGUI_Exit:
		If p_Owner
			Gui %p_Owner%:-Disabled
		Gui %s_Gui%:Destroy
		s_Gui := 0
		Return
}

Re: Hotkey chosen by user and saved in ini file

Posted: 14 Dec 2013, 07:02
by Joe Glines
Wow- As they say "be careful what you ask for". i thought this would be a lot simpler to do...

Thanks for the link!

Re: Hotkey chosen by user and saved in ini file

Posted: 14 Dec 2013, 08:06
by Joe Glines
I did some searching and found this example utilizing the function you provided.

Apparently it goes back to Jballi on this post

Pretty amazing!

Re: Hotkey chosen by user and saved in ini file

Posted: 14 Dec 2013, 16:02
by Guest10
do any of these scripts write to the Registry? :ugeek:

Re: Hotkey chosen by user and saved in ini file

Posted: 14 Dec 2013, 16:47
by Joe Glines
I was writing to ini files however it should be pretty easy to adapt them to write to the regsitry. (I've only played with 1 script doing so as, to me, I don't want to worry about messing up someone else's computer)