BetaLeaf

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

3 posts in this topic

#1 ·  Posted (edited)

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.

My Scripts:

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

AutoISO  - Automatic ISO Image creation using ImgBurn.

SoundBoard - Play any song or sound you want at the press of a hotkey

Share this post


Link to post
Share on other sites



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

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

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

My Scripts:

False Positive Reporter - Mass email all anti virus vendors with an attachment of your program for easy whitelisting.

PortableApps.com App Creation Wizard  - A simple GUI-based Wizard for creating PortableApps.

AutoISO  - Automatic ISO Image creation using ImgBurn.

SoundBoard - Play any song or sound you want at the press of a hotkey

Share this post


Link to post
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

  • Similar Content

    • Catalyst78
      By Catalyst78
      I want to be able to press one of the many extra buttons on my mouse to execute an autoit script function. To do this I have bound weird key combinations to to each button that would otherwise never be pressed, like shift+alt+ctrl+/ (see linked image and the following code.)
      Unfortunately this has a few annoying side effects, such as interfering with whatever I'm doing or one of the modifier keys occasionally getting 'stuck' down.
      I have been compling each version of this script directly into my startup folder(win7) and have been using it for years. But surely there is a better way of doing this, thanks for help in advance.

      http://prnt.sc/dquyar
      ;^ Ctrl ! Alt + Shift # Win HotKeySet("!{/}", "Back_Btn") ; spamclickoff HotKeySet("+!^{.}", "Forw_Btn") ; ___/Speakers/HeadPhones HotkeySet("+!^{,}", "Midd_Btn") ;MouseLock/InputStuff HotkeySet("+!^{\}", "Gs_Back_Btn") ;ScreenBrightness 100/60/20 HotkeySet("+!^{=}", "Gs_Forw_Btn") ;Vlc/CloseVlc Recent/Touhou HotkeySet("+!^{-}", "Gs_Midd_Btn") ;ScreenSwaper ToMain/ToSecond HotkeySet("+!^{]}", "Gs_M3") ;VlcTogglePlay HotkeySet("+!^{[}", "Gs_MW_Left") ;F11/!Enter  
    • rootx
      By rootx
      I would like that if you press the ENTER key is to simulate the pressure of a button in the GUI.
      I have no idea how to do it. THX
      $BTENTER = GUICtrlCreateButton("ENTER", 260, 160, 20, 25) EX. HotKeySet("{ENTER}",$BTENTER )  
    • TheOnlyOne
      By TheOnlyOne
      So currently I have some code waiting for a status to change(there is different indicators to check if this status has changed) 
      Func oneRow($count) $globaltimer = _Timer_SetTimer($gui, 60000*10, "reset") $errTimer = TimerInit() While (1)         If PixelGetColor(1117, 326) = 0xC6C6C6 Then ExitLoop (1)         Sleep(500)         MouseClick("right")         Sleep(600)         If PixelGetColor(1117, 326) = 0xC6C6C6 Then ExitLoop (1)         If TimerDiff($errTimer) > 5000 Then             If PixelGetColor(1100, 310) = 0x707070 Or PixelGetColor(901, 305) = 0xE0E0E0 Or PixelGetColor(1043, 383) = 0xC6C6C6 Then                 Sleep(100)                 Send("{esc}")                 Sleep(1000)             EndIf         EndIf     WEnd .... EndFunc   ;==>oneRow The two timers is for error checking. This works just fine the problem comes when I go into the while 1 loop, my hotkeys at the top of the program seems to stop working, for an example 
      HotKeySet("{f1}", "stop") Func stop() Exit EndFunc ;==>stop Does not work at all after the while loop. The globaltimer is never called even though i tried setting the delay all the way down to 3 seconds. So I thought that maybe one of the function I use is blocking so the timer would never be called/the hotkey wouldn't work? if i make an delay just before the while loop the hotkey works just fine. Oh and the errTimer is supossed to not reset after 5 seconds but should keep making the check it does if five seconds have passed
    • Simpel
      By Simpel
      Hi,
       
      I have written a programm offering me a lot of hotkeys to work with windows. All in all there are 40 hotkeys defined now.
      Now I made a portable version of it. But in this there are a lot of hotkeys useless. So I had to change some things. But that's not my point.
      Because of all these many hotkeys I sometimes know I made a hotkey but what were the keys I have to press.
      So I want to make a little help GUI or html or whatever. Now I'm interested to automate my own list of hotkeys.
      How can I collect all my hotkeys and necessary keys for this in a list?
       
      Regard, Conrad
    • dynamitemedia
      By dynamitemedia
      i have the following snippet...   now its working but i have it inside a function and want to be able to use   $aThumb[$i]  outside the function in the rest of the script, i tried return and keep getting this error  "Invalid keyword at the start of this line.:"  
      Global $iRows = UBound($a, $UBOUND_ROWS) Global $iCols = UBound($a, $UBOUND_COLUMNS) $oID = $oID + 1 $oURL = $oString.selectSingleNode("./url") $oName = $oString.selectSingleNode("./name") $oCategory = $oString.selectSingleNode("./category") $oThumb = $oString.selectSingleNode("./image") $oLanguage = $oString.selectSingleNode("./language") $aThumb = [$iRows] _ArrayAdd($aThumb, $oThumb.text) For $i = 1 To UBound($aThumb) - 1 ConsoleWrite($oID & @TAB & $aThumb[$i] & @CRLF) Next Next ConsoleWrite( "rows: " & $iRows & @CRLF) Thanks for your help