Jump to content

[Solved]How to create a Function with flags in a Function for use with Hotkeyset


Recommended Posts

Hi guys. I am trying to make a soundboard app. 

Here is the code I have so far

#include "Misc.au3"
#RequireAdmin;needed to work in some games.
#include "array.au3"
Opt("WinTitleMatchMode", -2)
If @OSArch = "x64" Then
    Global $VLC_Path = "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"
    Global $VLC_WorkingDir = "C:\Program Files (x86)\VideoLAN\VLC\"
Else
    Global $VLC_Path = "C:\Program Files\VideoLAN\VLC\vlc.exe"
    Global $VLC_WorkingDir = "C:\Program Files\VideoLAN\VLC\"
EndIf
Global $sectionData = IniReadSectionNames(@ScriptDir & "\SoundBoard.ini")
If @error Then
    IniWriteSection(@ScriptDir & "\SoundBoard.ini", "Sound1", 'File="' & @UserProfileDir & '\Music\SampleTrack.mp3"' & @CRLF & 'StartTime="12"' & @CRLF & 'EndTime="34"' & @CRLF & 'PlaybackDevice="Microsoft Soundmapper"' & @CRLF & 'Hotkey="+{numpad9}"')
    MsgBox(16, "SoundBoard", "SoundBoard.ini is missing. It has been created for you.")
    ShellExecute(@ScriptDir & "\SoundBoard.ini", "", "", "edit")
    InputBox("SoundBoard", "Notes:" & @CRLF & "StartTime and EndTime are in seconds. Available Hotkeys can be found at the following url:", "https://www.autoitscript.com/autoit3/docs/functions/Send.htm")
    Exit
EndIf
For $i = 1 To $sectionData[0]
    Local $iArray = IniReadSection(@ScriptDir & "\SoundBoard.ini", $sectionData[$i])
    For $j = 1 To UBound($iArray) - 1
        Local $result = Assign("SoundBoard" & $i & "_" & $j, IniRead(@ScriptDir & "\SoundBoard.ini", $sectionData[$i], $iArray[$j][0], $iArray[$j][1]), 2)
        If $result = 1 Then
            Consolewrite("Variable Assigned: SoundBoard" & $i & "_" & $j & @CRLF & "Data=" & $iArray[$j][1]&@CRLF)
        Else
            Consolewrite("Variable was not assigned: SoundBoard" & $i & "_" & $j & @CRLF & "Data=" & $iArray[$j][1]&@CRLF)
        EndIf
        
    Next
Next
For $i = 1 To $sectionData[0]
    Local $Hotkey = Eval("SoundBoard"&$i&"_5")
    ConsoleWrite("Processing Hotkey "&$Hotkey&@CRLF)

;NEED HELP HERE
    
;   HotKeySet($Hotkey,"")
Next
While 1
    Sleep(500);idle to prevent unnecessary work. 10 is the minimal we can set this value to.
WEnd
Func LoadVLC($iPlayFile, $iPlayFileStartTime, $iPlayFileEndTime, $iPlayAudioDevice = "Microsoft Soundmapper")
    ShellExecuteWait($VLC_Path, '--qt-start-minimized --play-and-exit --start-time="' & $iPlayFileStartTime & '" --stop-time="' & $iPlayFileEndTime & '" --aout=waveout --waveout-audio-device="' & $iPlayAudioDevice & '" "' & $iPlayFile & '"', $VLC_WorkingDir, "", @SW_HIDE)
    Beep(500, 200)
EndFunc   ;==>LoadVLC

For example, I have a song called "MoonlightSonata.mp3" and I want to activate it with hotkey !{numpad9}. However, hotkeyset does not allow sending of flags so I cannot use LoadVLC as it is now. I need to create a function that stores the flags using the data from the earlier Inireads. Ik how to use the data but not how to create the function to store that data. I looked at IsFunc() second example but I do not understand. Sorry if I am not being clear on what I am trying to do. My brain is fried right now after trying various things for an hour. Any help would be appreciated. 

The idea is to be able to have a dedicated hotkey for each sound I want to play so I will need to have my script create a new function by itself using the data from the INI.

hotkeyset("$hotkey1","MySoundBoard1")
hotkeyset("$hotkey2","MySoundBoard2")
func MySoundBoard1()
    LoadVLC("MoonlightSonata.mp3") ;the other flags are optional and will not be set for simplicity.
endfunc
func MySoundBoard1()
    LoadVLC("TheBananaSong.mp3")
endfunc

Honestly I don't care how it is done as long as I have a dedicated hotkey for each entry in my ini.  An example of such an entry looks like

[Sound1]
File="C:\Users\BetaL\Music\SampleTrack1.mp3"
StartTime="12"
EndTime="34"
PlaybackDevice="Microsoft Soundmapper"
Hotkey="!{numpad8}"

[Sound2]
File="C:\Users\BetaL\Music\SampleTrack2.mp3"
StartTime="24"
EndTime="43"
PlaybackDevice="Microsoft Soundmapper"
Hotkey="!{numpad9}"

Am I making any sense? Please let me know.

 

Edit: Huge thanks to @Melba23 for the learning experience, his time, and help.

Edited by BetaLeaf
To give thanks.

 

 

Link to comment
Share on other sites

  • Moderators

BetaLeaf,

Using your ini format I have recast the script and I believe it now does what you require:

;#RequireAdmin;needed to work in some games. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< This does not make me very happy <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#include "Misc.au3"
#include "Array.au3"

Opt("WinTitleMatchMode", -2)

If @OSArch = "x64" Then
    Global $VLC_Path = "C:\Program Files (x86)\VideoLAN\VLC\vlc.exe"
    Global $VLC_WorkingDir = "C:\Program Files (x86)\VideoLAN\VLC\"
Else
    Global $VLC_Path = "C:\Program Files\VideoLAN\VLC\vlc.exe"
    Global $VLC_WorkingDir = "C:\Program Files\VideoLAN\VLC\"
EndIf

$sIniName = @ScriptDir & "\SoundBoard.ini"

; Read ini section names
Global $aSectionList = IniReadSectionNames($sIniname)

; Create data array to hold ini data for each HotKey
Global $aHotKeyData[UBound($aSectionList)][5]
;_ArrayDisplay($aHotKeyData, "", Default, 8)

If @error Then
    IniWriteSection($sIniname, "Sound1", 'File="' & @UserProfileDir & '\Music\SampleTrack.mp3"' & @CRLF & 'StartTime="12"' & @CRLF & 'EndTime="34"' & @CRLF & 'PlaybackDevice="Microsoft Soundmapper"' & @CRLF & 'Hotkey="+{numpad9}"')
    MsgBox(16, "SoundBoard", "SoundBoard.ini is missing. It has been created for you.")
    ShellExecute($sIniname, "", "", "edit")
    InputBox("SoundBoard", "Notes:" & @CRLF & "StartTime and EndTime are in seconds. Available Hotkeys can be found at the following url:", "https://www.autoitscript.com/autoit3/docs/functions/Send.htm")
    Exit
EndIf

; For each section
For $i = 1 To $aSectionList[0]

    ; Read ini section
    $aSection = IniReadSection($sIniname, $aSectionList[$i])

    ; Fill HotKey data array                                                                ; example content
    $aHotKeyData[$i][0] = IniRead($sIniName, $aSectionList[$i], "HotKey", "Error")          ; !{numpad8}
    $aHotKeyData[$i][1] = IniRead($sIniName, $aSectionList[$i], "File", "Error")            ; C:\Users\BetaL\Music\SampleTrack1.mp3
    $aHotKeyData[$i][2] = IniRead($sIniName, $aSectionList[$i], "StartTime", "Error")       ; 12
    $aHotKeyData[$i][3] = IniRead($sIniName, $aSectionList[$i], "EndTime", "Error")         ; 34
    $aHotKeyData[$i][4] = IniRead($sIniName, $aSectionList[$i], "PlayBackDevice", "Error")  ; Microsoft Soundmapper

    ; Set HotKey to common function
    HotKeySet($aHotKeyData[$i][0], "_HotKeyFunc")

Next

;_ArrayDisplay($aHotKeyData, "", Default, 8)

While 1
    Sleep(10);idle to prevent unnecessary work. 10 is the minimal we can set this value to.
WEnd

Func _HotKeyFunc()

    ;Get HotKey pressed
    $sHotKeyPressed = @HotKeyPressed

    ;ConsoleWrite($sHotKeyPressed & @CRLF)

    ; Find HotKey pressed in the data array
    $iIndex = _ArraySearch($aHotKeyData, $sHotKeyPressed)
    ; Check found
    If $iIndex <> -1 Then
        ; Create parameter using the data in the array
        $sParam = '--qt-start-minimized --play-and-exit --start-time="' & $aHotKeyData[$iIndex][2] & '" --stop-time="' & $aHotKeyData[$iIndex][3] & '" --aout=waveout --waveout-audio-device="' & _
        $aHotKeyData[$iIndex][4] & '" "' & $aHotKeyData[$iIndex][1] & '"'
        ; Simulate passing commandline to VLC
        ConsoleWrite("ShellExecuteWait:" & @CRLF & $VLC_Path & @CRLF & $sParam & @CRLF & $VLC_WorkingDir & @CRLF & @CRLF)
        Beep(500, 200)
    Else
        ConsoleWrite("Not a valid HotKey" & @CRLF)
    EndIf

EndFunc

I have commented out a number of _ArrayDisplay/ConsoleWrite lines - just uncomment them to see what happens at the various stages.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Amazing @Melba23, I wasn't aware of @HotKeyPressed. I greatly appreciate your help.

 

About the #requireadmin. I'm not going to try and defend myself. Instead I will just simply tell you why that was even there. I have friends on Dota 2 that like to hear music while they play the game and in order to activate the hotkey in the game, admin is required. If I don't then the hotkey doesn't recognize it was pressed. How I send audio from my soundboard is a simple matter. I just connect a headphone cable that is male on both ends, one into my mic jack, the other into my headphone jack. That is where the playback device flag comes in.

@Melba23 Once again I thank you from the bottom of my heart. You have provided me with a fantastic learning experience and I greatly appreciate your kindness, time, and expertise. You didn't have to help at all but you did. I am grateful.

Jeff Savage ~ BetaLeaf

Edited by BetaLeaf

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...