user2422348
user2422348

Reputation: 23

How to run two scripts at once autohotkey?

Is it possible to have a loop playing throughout the script, while also continuing to the rest of the script. i.e.

Loop
{
Send {Volume_Up 100}
}
SoundBeep, 1000

So in the above example the idea would be that the volume is being constantly looped upwards while a beep is playing in the background, I can't get it to work, i've tried putting the SoundBeep within the loop, but it just beeps and does not send volume up infinitely. I know this is a pretty low level question, but I can't figure it out.

Upvotes: 1

Views: 4824

Answers (1)

MCL
MCL

Reputation: 4085

In your example, you are sending {Volume_Up} 100 times in a row. After your (infinite!) loop, you let it beep one time. Here's the code to increase volume and beep each time:

Loop 20
{
    Send, {Volume_Up 5}
    SoundBeep, 1000
    Sleep, 500
}

It makes sense, to let the script sleep some time after each loop run. Otherwise, there won't be a meaningful acoustic effect. Of course, you can modify the number of loop runs and the number of consecutive {Volume_Up} keystrokes.

Update:

If you want the beep to be played continuously and without a short break while increasing the volume: There is no easy way to do that, since AHK supports no real multithreading. I'm going to show a quick and dirty approach how to achieve it anyway. It is based on launching another AHK process, whose SoundBeep won't halt the main process' execution.

Example structure:

In one directory, we create two files, namely main.ahk and beep.ahk

beep.ahk contains:

SoundBeep, %1%, %2%

main.ahk contains:

Run, %A_AhkPath% "%A_ScriptDir%\beep.ahk" 750 1000
Loop 10
{
    Send, {Volume_Up 2}
    Sleep, 100
}

When you start main.ahk, it will first call beep.ahk, spawning another process and passing it the start parameters for frequency and duration (here: 750 and 1000). beep.ahk will simply make a sound based on what you've passed it (note, that %1% and %2%contain our start parameters). main.ahk can then send our volume keystrokes simultaneously, without having to wait for the beep to finish.
This should only be used for educational purposes, since parallel execution shouldn't be achieved with multiple processes. If you want to play background sounds in a real project, you will most likely have your own sound file; in this case SoundPlay will be able to play sounds asynchronously.

Upvotes: 2

Related Questions