Jump to content
Sign in to follow this  
webb34

Passing params though functions called by HotKeySet

Recommended Posts

webb34

Hey I'm new to AutoIt and loving it thus far. I have been building a script to swap between audio devices, which is based off of one I found online. Unfortunately I ran into an issue where I want to pass a param depending on which hotkey calls a function.

here is what I have so far so you can see what I'm trying to to.

Dim $ConfigWindowTitle = "Sound"
HotKeySet("+^1", Swap(1)) ; Makes Shift+Ctrl+1 a hotkey that calls the swap function passing 1 as a variable
         ; This is for the headset
HotKeySet("+^2", Swap(2)) ; Makes Shift+Ctrl+2 a hotkey that calls the swap function passing 2 as a variable
         ; This is for the speaker system
HotKeySet("+^3", Swap(3)) ; Makes Shift+Ctrl+3 a hotkey that calls the swap function passing 3 as a variable
         ; This is for the Headphones
While 1 ; continuous lop to keep script active
Sleep(100) ; Sleeps for 100 miliseconds
WEnd ; Loop ends
Func Swap($whichDevice)
MsgBox(0,"Which device is selected", $whichDevice);
Run("control mmsys.cpl") ; Starts the sound control applet and hides it
WinWaitActive($ConfigWindowTitle) ; Wait for it to be active before sending keystrokes.
Send("{TAB}{TAB}{TAB}{TAB}") ; Puts the focus on the playback device list
For $i = 1 to $whichDevice Step 1
Send("{DOWN}")
Next
Send("!s") ; Sends Alt+S to set the selected device as the default
WinClose($ConfigWindowTitle) ; Closes the window
EndFunc

(I commented it for my sake :/ )

Any help is appreciated. Thanks in advance!

Edited by webb34

Share this post


Link to post
Share on other sites
czardas

If you use global variables then you do not need to pass them as parameters to the function since they are readily available.An alternative is to trigger a series of functions using a global flag. It works like this: A hotkey sets the flag to TRUE. A conditional statement checks the flag within your main code. If the flag is TRUE then you call a function with local parameters. Set the global flag back to FALSE after you have finished.

Edited by czardas

Share this post


Link to post
Share on other sites
webb34

I see what you are saying but, I am referring to specifically this line

HotKeySet("+^1", "Swap(1)") ;

or the other "HotKeySet " lines

I want the hotkey "Shift+Ctrl+1" to call the Swap function, passing it the number 2. I want to have one function that selects 1 of the 3 different playback devices I want, depending on which hotkey is used, and I would rather not have to go through a function that is serving as a middleman to set a global variable.

Share this post


Link to post
Share on other sites
JohnOne

HotKeySet("+^1", "_test")
HotKeySet("+!1", "_test")




While 1
    Sleep(100)
WEnd




Func _test()
    If @HotKeyPressed = "+^1" Then
        MsgBox(0, "YAY!", "Shift, ctrl 1")
    Else
        MsgBox(0, "BOOO!", @HotKeyPressed)
    EndIf
EndFunc   ;==>_test

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
czardas

You can't directly set a hotkey to run a function with defined parameters. There are alternatives such as those I mentioned earlier. I thought it might be possible to incorporate some tricks using default parameters, but my attempts didn't work.

HotKeySet("^1", "_DefParam") ; This will never work.

Global $vDefault = False ; Declare the parameter.

_DefParam() ; This works! (kind of)

While 1
    Sleep(10)
WEnd

Func _DefParam($vDefault = "This works! (kind of)")
    MsgBox(0, "Default", "param = " & $vDefault)
EndFunc

The code is only intended to illustrate the behaviour of AutoIt. It is not a solution to a problem.

Edited by czardas

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
Sign in to follow this  

×