Page 1 of 1

A simple program that registers hotkeys from a file

Posted: Tue Apr 01, 2008 8:05 am
by Mistrel
You can use this tool to register global hotkeys to launch folders and applications. It's based on the LiteStep themevars.rc hotkey syntax.

To reload the hotkeys from the file use the hotkey "Ctrl-Alt-|".

hotkeyboard.pb

Code: Select all

HotkeyFile.s="hotkeys.conf"

Structure Hotkeys
	HotkeyID.l
	HotkeyPath.s
EndStructure

Global NewList Hotkeys.Hotkeys()

Procedure ReadHotkeys()
	Shared HotkeyFile.s
	
	FileID=ReadFile(#PB_Any,HotkeyFile.s)
	If FileID
		While Eof(FileID)=0
			LineNumber+1
			Line.s=Trim(ReadString(FileID))
			
			;// Check for common errors
			; Check for invalid hotkey label
			If Not Left(Line.s,7)="*Hotkey" And Not Left(Line.s,1)=";"
				Firstpart.s=Left(Line.s,FindString(Line.s," ",1))
				MessageRequester("Error","Invalid hotkey label "+Chr(34)+Firstpart.s+Chr(34)+" at line "+Str(LineNumber))
			EndIf
			
			; Check for an irregular number of double-quotes
			If CountString(Line.s,Chr(34))<>2
				MessageRequester("Error","Illegal string count at line "+Str(LineNumber))
			EndIf
			
			; Remove the hotkey label
			Line.s=Trim(ReplaceString(Line.s,"*Hotkey",""))
			
			; Find the path part and remove it from the hotkey line
			PathPart.s=Right(Line.s,Len(Line.s)-FindString(Line.s,Chr(34),1)+1)
			Line.s=Trim(ReplaceString(Line.s,PathPart.s,""))
			; Remove double-quotes from path
			PathPart.s=ReplaceString(PathPart.s,Chr(34),"")
			
			; Find and trim the hotkey character
			HotkeyChar.s=UCase(Right(Line.s,1))
			Line.s=Left(Line.s,Len(Line.s)-1)
			
			; Verify hotkey character and identify corresponding virtual key
			If Not Len(HotkeyChar.s)=1
				MessageRequester("Error","Illegal hotkey character "+Chr(34)+HotkeyChar.s+Chr(34)+"at line "+Str(LineNumber))
			EndIf
			Alphabet.s="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
			For i=1 To Len(Alphabet.s)
				If Mid(Alphabet.s,i,1)=HotkeyChar.s
					IsChar=1
				EndIf
			Next i
			If Not IsChar
				MessageRequester("Error","Illegal hotkey character "+Chr(34)+HotkeyChar.s+Chr(34)+"at line "+Str(LineNumber))
			EndIf
			Select HotkeyChar.s
				Case "A": HotkeyVK=#VK_A
				Case "B": HotkeyVK=#VK_B
				Case "C": HotkeyVK=#VK_C
				Case "D": HotkeyVK=#VK_D
				Case "E": HotkeyVK=#VK_E
				Case "F": HotkeyVK=#VK_F
				Case "G": HotkeyVK=#VK_G
				Case "H": HotkeyVK=#VK_H
				Case "I": HotkeyVK=#VK_I
				Case "J": HotkeyVK=#VK_J
				Case "K": HotkeyVK=#VK_K
				Case "L": HotkeyVK=#VK_L
				Case "M": HotkeyVK=#VK_M
				Case "N": HotkeyVK=#VK_N
				Case "O": HotkeyVK=#VK_O
				Case "P": HotkeyVK=#VK_P
				Case "Q": HotkeyVK=#VK_Q
				Case "R": HotkeyVK=#VK_R
				Case "S": HotkeyVK=#VK_S
				Case "T": HotkeyVK=#VK_T
				Case "U": HotkeyVK=#VK_U
				Case "V": HotkeyVK=#VK_V
				Case "W": HotkeyVK=#VK_W
				Case "X": HotkeyVK=#VK_X
				Case "Y": HotkeyVK=#VK_Y
				Case "Z": HotkeyVK=#VK_Z
			EndSelect
			
			; Split modifiers
			Count=CountString(Line.s,"+")
			Dim HotkeyModifiers.s(Count)
			For i=0 To Count
				;HotkeyModifiers(i)=StringField(Line.s,i+1,"+")
				Select Trim(StringField(Line.s,i+1,"+"))
					Case "ALT"
						Modifiers=Modifiers|#MOD_ALT
					Case "CTRL"
						Modifiers=Modifiers|#MOD_CONTROL
					Case "SHIFT"
						Modifiers=Modifiers|#MOD_SHIFT
					Default
						MessageRequester("Info","Unknown hotkey modifier "+Chr(34)+StringField(Line.s,i+1,"+")+Chr(34)+" at line "+Str(LineNumber))
				EndSelect
			Next i
			
			; Obtain unique hotkey ID
			HotkeyID+1
			
			; Register hotkey
			RegisterHotKey_(0,HotkeyID,Modifiers,HotkeyVK)
			
			AddElement(Hotkeys())
			Hotkeys()\HotkeyID=HotkeyID
			Hotkeys()\HotkeyPath=PathPart.s
			
			; Clear modifiers
			Modifiers=0
		Wend
		CloseFile(FileID)
	Else
		MessageRequester("Information","Couldn't open the file!")
	EndIf
	
	; Reset hotkeys hotkey
	RegisterHotKey_(0,HotkeyID+1,#MOD_CONTROL|#MOD_ALT,$DC) ; CTRL+ALT "\|" for US
EndProcedure

Procedure UnregisterHotkeys()
	ForEach Hotkeys()
		UnregisterHotKey_(0,Hotkeys()\HotkeyID)
		Hotkeys()\HotkeyPath.s="" ; empty list strings
	Next
	; Unregister reset hotkey
	UnregisterHotKey_(0,Hotkeys()\HotkeyID+1)
	ClearList(Hotkeys())
EndProcedure

ReadHotkeys()

Repeat: Delay(20)
	GetMessage_(@Msg.MSG,0,0,0)
	If Msg\message=#WM_HOTKEY
		ForEach Hotkeys()
			If Hotkeys()\HotkeyID=Msg\wParam
				FileSize=FileSize(Hotkeys()\HotkeyPath)
				; If the path is a file then run it
				If FileSize>0
					RunProgram(Hotkeys()\HotkeyPath,"","")
				; If the path is a directory then open it
				ElseIf FileSize=-2
					RunProgram("explorer.exe",Hotkeys()\HotkeyPath,"")
				EndIf
			EndIf
		Next
		; If the hotkey is the reset hotkey (CTRL+ALT |)
		If PeekW(@Msg\lParam+(SizeOf(LONG)/2))=$DC
			UnregisterHotkeys()
			ReadHotkeys()
		EndIf
	EndIf
ForEver
Here is an example Hotkeys.conf to use with the tool.

Hotkeys.conf

Code: Select all

*Hotkey CTRL+ALT F "D:\Program Files\Mozilla Firefox\firefox.exe"
*Hotkey CTRL+SHIFT F "D:\Program Files\Mozilla Firefox"
*Hotkey CTRL+SHIFT M "My_Computer.lnk"