;-- TIME_STAMP 2020-04-19 17:20:12 v 0.2 ; #FUNCTION# ======================================================================================= ; Name ..........: _Accelerator_Create ; Description ...: Creates a dummy control associated with a hotkey for a gui, (default) ; ...............: or assigns the passed Ctrl-ID to the hotkey. ; Parameters ....: $_hWnd The gui handle ; ...............: $_HotKey The desired hotkey ; ...............: $_CtrlID [optional] ID of an existing control, that should assigned to a hotkey ; Return values .: Success The ID the hotkey is associated with. ; ...............: Failure -1 @error=1 hotkey already exists, @extended=ID from existing hotkey ; Author ........: BugFix ; ================================================================================================== Func _Accelerator_Create($_hWnd, $_HotKey, $_ID='') Return __AcceleratorManagement($_hWnd, $_HotKey, $_ID) EndFunc ;==>_Accelerator_Create ; #FUNCTION# ======================================================================================= ; Name ..........: _Accelerator_GetID ; Description ...: Gets the ID which is associated with a passed hotkey and gui. ; Parameters ....: $_hWnd The gui handle ; ...............: $_HotKey The requested hotkey ; Return values .: Success The ID the hotkey is associated with. ; ...............: Failure -1 @error=1 accelerator array is empty ; ...............: @error=2 none accelerator for this hWnd ; ...............: @error=3 none hotkey exists for this ID ; Author ........: BugFix ; ================================================================================================== Func _Accelerator_GetID($_hWnd, $_HotKey) Return __AcceleratorManagement($_hWnd, $_HotKey, '', 'getID') EndFunc ;==>_Accelerator_GetID ; #FUNCTION# ======================================================================================= ; Name ..........: _Accelerator_GetHotkey ; Description ...: Gets the Hotkey which is associated with a passed ID and gui. ; Parameters ....: $_hWnd The gui handle ; ...............: $_ID The requested ID ; Return values .: Success The Hotkey the ID is associated with. ; ...............: Failure -1 @error=1 accelerator array is empty ; ...............: @error=2 none accelerator for this hWnd ; ...............: @error=3 none hotkey exists for this ID ; Author ........: BugFix ; ================================================================================================== Func _Accelerator_GetHotkey($_hWnd, $_ID) Return __AcceleratorManagement($_hWnd, '', $_ID, 'getHK') EndFunc ;==>_Accelerator_GetHotkey ; #FUNCTION# ======================================================================================= ; Name ..........: _Accelerator_ChangeHotkey ; Description ...: Changes the Hotkey for a passed ID. ; Parameters ....: $_hWnd The gui handle ; ...............: $_ID The requested ID ; ...............: $_HotKey The new hotkey ; Return values .: Success 1 The new Hotkey was set. ; ...............: Failure -1 @error=1 accelerator array is empty ; ...............: @error=2 none accelerator for this hWnd ; ...............: @error=3 none hotkey exists for this ID ; Author ........: BugFix ; ================================================================================================== Func _Accelerator_ChangeHotkey($_hWnd, $_ID, $_HotKey) Return __AcceleratorManagement($_hWnd, $_HotKey, $_ID, 'changeHK') EndFunc ;==>_Accelerator_ChangeHotkey ; #FUNCTION# ======================================================================================= ; Name ..........: _Accelerator_Delete ; Description ...: Deletes one Hotkey by passed ID or all Hotkeys if only passed the hWnd. ; Parameters ....: $_hWnd The gui handle ; ...............: $_ID The ID, if one hotkey should to delete ; Return values .: Success 1 The new Hotkey was set. ; ...............: Failure -1 @error=1 accelerator array is empty ; ...............: @error=2 none accelerator for this hWnd ; ...............: @error=3 none hotkey exists for this ID (only for deleting by ID) ; Author ........: BugFix ; ================================================================================================== Func _Accelerator_Delete($_hWnd, $_ID='') If $_ID = '' Then Return __AcceleratorManagement($_hWnd, '', '', 'delHWnd') Else Return __AcceleratorManagement($_hWnd, '', $_ID, 'del') EndIf EndFunc ;==>_Accelerator_Delete ;--------------------------------------------------------------------------------------------------- #Region - helper functions Func __AcceleratorManagement($_hWnd, $_HotKey='', $_ID='', $_sAction='create') Local Static $aHWnd[] = [0] ; [count, hWnd, hWnd, ..] Local Static $aAccelerators[] = [0] ; [count, aAcc, aAcc, ..] Local Static $aAccTemplate[1][2] ; [[Hotkey,ID]] Local $aGet, $aAccTmp, $dummyID, $idx, $iMatch = -1, $result Switch $_sAction Case 'getID', 'getHK' $idx = __AM_GetHWndArrayIndex($aHWnd, $_hWnd) If @error Then Return SetError(@error,0,-1) ; (1) array is empty or (2) none accelerator for this hWnd $aAccTmp = $aAccelerators[$idx] $result = __AM_GetFromAccArray($aAccTmp, $idx, ($_sAction='getID'?$_HotKey:$_ID), $_sAction) If @error Then Return SetError(@error,0,-1) ; (3) HK or ID doesn't exist Return $result Case 'create' $result = __AcceleratorManagement($_hWnd, $_HotKey, $_ID, 'getID') If $result <> -1 Then Return SetError(1,$result,-1) ; hotkey already exists for this hWnd, @extended: the associated ID $_ID = $_ID = '' ? GUICtrlCreateDummy() : $_ID $idx = __AM_GetHWndArrayIndex($aHWnd, $_hWnd) If @error Then ; $aHWnd is empty $aHWnd[0] += 1 ReDim $aHWnd[$aHWnd[0]+1] $aHWnd[$aHWnd[0]] = $_hWnd ; register the new hWnd $aAccTmp = $aAccTemplate ; create accelerator array $aAccTmp[0][0] = $_HotKey $aAccTmp[0][1] = $_ID $aAccelerators[0] += 1 ReDim $aAccelerators[$aAccelerators[0]+1] $aAccelerators[$aAccelerators[0]] = $aAccTmp ; write to management array Else $aAccTmp = $aAccelerators[$idx] ReDim $aAccTmp[UBound($aAccTmp)+1][2] $aAccTmp[UBound($aAccTmp)-1][0] = $_HotKey $aAccTmp[UBound($aAccTmp)-1][1] = $_ID $aAccelerators[$idx] = $aAccTmp ; write back to management array EndIf GUISetAccelerators($aAccTmp, $_hWnd) ; assign the accelerators Return $_ID Case 'changeHK' ; new hotkey for the passed ID $idx = __AM_GetHWndArrayIndex($aHWnd, $_hWnd) If @error Then Return SetError(@error,0,-1) $result = __AcceleratorManagement($_hWnd, $_HotKey, $_ID, 'getHK') If @error Then Return SetError(@error,0,-1) ; none hotkey exists for this ID on this hWnd $aAccTmp = $aAccelerators[$idx] For $i = 0 To UBound($aAccTmp) -1 If $aAccTmp[$i][1] = $_ID Then $iMatch = $i ; a match is guaranteed based on the previous query ExitLoop EndIf Next $aAccTmp[$iMatch][0] = $_HotKey ; replace the old hotkey with the new one $aAccelerators[$idx] = $aAccTmp ; write back to management array GUISetAccelerators($aAccTmp, $_hWnd) Return 1 Case 'del' ; delets by passed ID, if last entry for an window will deleted - the hole array will deleted too $idx = __AM_GetHWndArrayIndex($aHWnd, $_hWnd) If @error Then Return SetError(@error,0,-1) $result = __AcceleratorManagement($_hWnd, $_HotKey, $_ID, 'getHK') If @error Then Return SetError(@error,0,-1) ; none hotkey exists for this ID on this hWnd $aAccTmp = $aAccelerators[$idx] If UBound($aAccTmp) = 1 Then ; the last entry - so delete the array and deregister the hWnd If $aAccelerators[0] = 1 Then ; its only one hWnd with one accArray, that has one accelerator - delete all ReDim $aAccelerators[1] $aAccelerators[0] = 0 ReDim $aHWnd[1] $aHWnd[0] = 0 Return 1 Else ; more than one hWnd is registered, delete the related hWnd and the accelerator array $aAccelerators[0] -= 1 $aAccelerators[$idx] = Null $aHWnd[0] -= 1 $aHWnd[$idx] = Null EndIf GUISetAccelerators('', $_hWnd) ; unset accelerators from hWnd Else ; There are more than one accelerators in accArray, delete only the one, related to the ID For $i = 0 To UBound($aAccTmp) -1 If $aAccTmp[$i][1] = $_ID Then $iMatch = $i ExitLoop EndIf Next $aAccTmp[$iMatch][0] = $aAccTmp[UBound($aAccTmp)-1][0] ; move last entry to position to delete $aAccTmp[$iMatch][1] = $aAccTmp[UBound($aAccTmp)-1][1] ReDim $aAccTmp[UBound($aAccTmp)-1][2] ; delete last entry $aAccelerators[$idx] = $aAccTmp ; write back to management array GUISetAccelerators($aAccTmp, $_hWnd) EndIf Return 1 Case 'delHWnd' ; deletes the array with accelerators for the passed hWnd an deregisters the hWnd $idx = __AM_GetHWndArrayIndex($aHWnd, $_hWnd) If @error Then Return SetError(@error,0,-1) If $aAccelerators[0] = 1 Then ; its the last entry - delete all ReDim $aAccelerators[1] $aAccelerators[0] = 0 ReDim $aHWnd[1] $aHWnd[0] = 0 Return 1 Else $aAccelerators[0] -= 1 $aAccelerators[$idx] = Null $aHWnd[0] -= 1 $aHWnd[$idx] = Null EndIf Return GUISetAccelerators('', $_hWnd) ; unset accelerators from hWnd EndSwitch EndFunc ;==>__AcceleratorManagement Func __AM_GetHWndArrayIndex(ByRef $_aHWnd, $_hWnd) ; returns the index If $_aHWnd[0] = 0 Then Return SetError(1,0,-1) ; array is empty For $i = 1 To $_aHWnd[0] If $_aHWnd[$i] = $_hWnd Then Return $i Next Return SetError(2,0,-1) ; none accelerator for this hWnd EndFunc ;==>__AM_GetHWndArrayIndex Func __AM_GetFromAccArray(ByRef $_aAccTmp, $_index, $_vAsk, $_sAction='getID') ; $_sAction: 'getID', 'getHK' Local $iAsk = $_sAction = 'getID' ? 0 : 1 Local $iRet = Abs($iAsk -1) For $i = 0 To UBound($_aAccTmp) -1 If $_aAccTmp[$i][$iAsk] = $_vAsk Then Return $_aAccTmp[$i][$iRet] Next Return SetError(3,0,-1) ; hotkey or ID doesn't exist EndFunc ;==>__AM_GetFromAccArray #EndRegion