soundBoard/soundBoard.ahk
2025-05-04 11:23:13 +07:00

142 lines
3.2 KiB
AutoHotkey

iniFile := "save.ini"
; --- Load settings ---
IniRead, controllerModifier, %iniFile%, Settings, ControllerModifier, ^
IniRead, soundModifier, %iniFile%, Settings, SoundModifier, !
IniRead, SaveKey, %iniFile%, GeneralHotkeys, SaveKey, ^Numpad0
IniRead, ExitKey, %iniFile%, GeneralHotkeys, ExitKey, ^#Numpad0
IniRead, ReloadKey, %iniFile%, GeneralHotkeys, ReloadKey, ^Numpad1 ; <- Tambah ini!
; --- Initialize globals ---
global Keys := []
global Paths := []
global HotkeyMap := {}
LoadHotkeys()
; --- Bind SaveKey, ExitKey, and ReloadKey ---
Hotkey, %SaveKey%, SaveData
Hotkey, %ExitKey%, ExitAppHandler
Hotkey, %ReloadKey%, ReloadData ; <- Bind reload!
return
; --- Load all hotkeys from INI ---
LoadHotkeys()
{
global Keys, Paths, HotkeyMap, iniFile, controllerModifier, soundModifier
; Clear previous keys
Keys := []
Paths := []
HotkeyMap := {}
idx := 1
Loop
{
section := "h" idx
IniRead, key, %iniFile%, %section%, key, ERROR
IniRead, path, %iniFile%, %section%, path, ERROR
if (key = "ERROR")
break
Keys.Push(key)
Paths.Push(path)
browseHotkey := controllerModifier . key
playHotkey := soundModifier . key
HotkeyMap[browseHotkey] := ["Browse", idx]
HotkeyMap[playHotkey] := ["Play", idx]
Hotkey, %browseHotkey%, HotkeyDispatcher
Hotkey, %playHotkey%, HotkeyDispatcher
idx++
}
}
; --- Dispatcher for all hotkeys ---
HotkeyDispatcher:
thisHotkey := A_ThisHotkey
actionInfo := HotkeyMap[thisHotkey]
if (!actionInfo)
return
action := actionInfo[1]
idx := actionInfo[2]
if (action = "Browse")
BrowseFile(idx)
else if (action = "Play")
PlaySound(idx)
return
; --- Save current keys and paths ---
SaveData:
IniWrite, %controllerModifier%, %iniFile%, Settings, ControllerModifier
IniWrite, %soundModifier%, %iniFile%, Settings, SoundModifier
IniWrite, %SaveKey%, %iniFile%, GeneralHotkeys, SaveKey
IniWrite, %ExitKey%, %iniFile%, GeneralHotkeys, ExitKey
IniWrite, %ReloadKey%, %iniFile%, GeneralHotkeys, ReloadKey
Loop % Keys.Length()
{
section := "h" A_Index
IniWrite, % Keys[A_Index], %iniFile%, %section%, key
IniWrite, % Paths[A_Index], %iniFile%, %section%, path
}
MsgBox, Saved!
return
; --- Reload keys and hotkeys from INI ---
ReloadData:
; First unbind all old hotkeys
for hkName, _ in HotkeyMap
Hotkey, %hkName%, Off
; Reload modifiers and keys again
IniRead, controllerModifier, %iniFile%, Settings, ControllerModifier, ^
IniRead, soundModifier, %iniFile%, Settings, SoundModifier, !
LoadHotkeys()
MsgBox, Reloaded hotkeys from INI!
return
; --- Exit application ---
ExitAppHandler:
MsgBox, Exiting...
ExitApp
return
; --- Browse a file and assign it ---
BrowseFile(idx)
{
global Keys, Paths
FileSelectFile, SelectedFile, 1, , Select a sound, Audio Files (*.wav; *.mp3)
if (!SelectedFile)
return
Paths[idx] := SelectedFile
}
; --- Play a sound file ---
PlaySound(idx)
{
global Paths
FilePath := Paths[idx]
if (FilePath = "")
{
MsgBox, No file selected.
}
else if !FileExist(FilePath)
{
MsgBox, File does not exist.
}
else
{
SoundPlay, %FilePath%
}
}