HotKeyStringToCode (?)

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Addict
Addict
Posts: 1318
Joined: Sun May 14, 2017 1:48 am

HotKeyStringToCode (?)

Post by AZJIO »

I would like to store "Hotkey = Ctrl +J" in the ini file instead of "HotkeyCode = 586"

Code: Select all

Procedure.l HotKeyStringToCode(HotKey$)
	Protected ModKey
	HotKey$ = ReplaceString(HotKey$ , " " , "")
	
	If FindString(HotKey$, "Ctrl+", 1, #PB_String_NoCase)
		ModKey | #MOD_CONTROL
	EndIf
	If FindString(HotKey$, "Alt+", 1, #PB_String_NoCase)
		ModKey | #MOD_ALT
	EndIf
	If FindString(HotKey$, "Shift+", 1, #PB_String_NoCase)
		ModKey | #MOD_SHIFT
	EndIf
	Debug Right(HotKey$, 1)
	ModKey | (Asc(Right(HotKey$, 1)) | $FF)
    ProcedureReturn ModKey
EndProcedure


Define HotKey$ = "Ctrl + Alt + F"
ini_HotkeyCode = HotKeyStringToCode(HotKey$)
Debug ini_HotkeyCode
Define HotKey$ = "Ctrl +J"
ini_HotkeyCode = HotKeyStringToCode(HotKey$)
Debug ini_HotkeyCode

; ===============================
If ini_HotkeyCode
	Debug ini_HotkeyCode
	VirtKey = ini_HotkeyCode & $FF ; LoWord
	ModKey = GetModKey(ini_HotkeyCode >> 8)
	Debug GetKey(ini_HotkeyCode)
	UnregisterHotKey_(hWnd_0, #HK_ID) ; reassignment works without unregistration
	If Not RegisterHotKey_(hWnd_0, #HK_ID, ModKey, VirtKey)
		MessageRequester("Error", "Failed to register hotkey")
	EndIf
EndIf
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: HotKeyStringToCode (?)

Post by kenmo »

Hi, if you're willing to include a whole file, try ParseShortcut(Text.s) and ComposeShortcut(Shortcut.i) in my MenuManager-Shortcuts.pbi
https://github.com/kenmo-pb/MenuManager ... rtcuts.pbi

Direct link to the .pbi file
https://raw.githubusercontent.com/kenmo ... rtcuts.pbi
Post Reply