Sound when pressing a key Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
riohn
Posts: 4
Joined: 19 Jun 2021, 06:43

Sound when pressing a key

19 Jun 2021, 06:51

Hello,
i downloaded two wav files that say : Activated / Deactivated
and i want to use them in a script that does the following :
when pressed a key once: it says activated
when i press the same key again: it says deactivated
then it reset the loop in a way that the 3rd press will have "activated" 4th, deactivated etc
each time i press only either one of those sounds once
can someone help me please?
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Sound when pressing a key

19 Jun 2021, 08:28

Code: Select all

dir = %A_ScriptDir%
activated = %dir%\activated.wav
deactivated = %dir%\deactivated.wav
F3::SoundPlay, % (on := !on) ? activated : deactivated
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Sound when pressing a key

19 Jun 2021, 08:36

@mikeyww I wanted to "hear" it. :)

Code: Select all

#SingleInstance, Force
#Warn
On := 0

F3:: ComObjCreate("SAPI.SpVoice").Speak( (On:=!On) ? "activated" : "deactivated")
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Sound when pressing a key

19 Jun 2021, 08:43

Code: Select all

ComObjCreate("SAPI.SpVoice").Speak("That is a terrific script, exclamation point.")
Sally2Q
Posts: 43
Joined: 16 Jun 2021, 10:18

Re: Sound when pressing a key

19 Jun 2021, 12:22

mikeyww wrote:
19 Jun 2021, 08:43

Code: Select all

ComObjCreate("SAPI.SpVoice").Speak("That is a terrific script, exclamation point.")
wtf I didn't know you could do that!! I've been agonizing to go with a paid tts program or a free one that might be ransomware lol
Sally2Q
Posts: 43
Joined: 16 Jun 2021, 10:18

Re: Sound when pressing a key

19 Jun 2021, 12:58

mikeyww wrote:
19 Jun 2021, 08:43

Code: Select all

ComObjCreate("SAPI.SpVoice").Speak("That is a terrific script, exclamation point.")
If I might ask a question, this is cool but it seems to hang the script. It pauses the current thread. But worse not even another hotkey can fire till it's done reading. What to do about this?
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Sound when pressing a key

19 Jun 2021, 13:08

Code: Select all

SpVoice := ComObjCreate("SAPI.SpVoice")
SVSFlagsAsync := SPF_ASYNC := 1 << 0
SpVoice.Speak("That is a terrific script, exclamation point.", SVSFlagsAsync)
Sally2Q
Posts: 43
Joined: 16 Jun 2021, 10:18

Re: Sound when pressing a key

19 Jun 2021, 13:18

swagfag wrote:
19 Jun 2021, 13:08

Code: Select all

SpVoice := ComObjCreate("SAPI.SpVoice")
SVSFlagsAsync := SPF_ASYNC := 1 << 0
SpVoice.Speak("That is a terrific script, exclamation point.", SVSFlagsAsync)
Nice @swagfag . Thanks!
I think I have one other question about this but I guess I should start my own thread because I've crashed this one long enough lol.
User avatar
riohn
Posts: 4
Joined: 19 Jun 2021, 06:43

Re: Sound when pressing a key

19 Jun 2021, 15:15

Thanks a lot, but i already have an audio file.
all i want is ahk calling out each file at each press
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Sound when pressing a key

19 Jun 2021, 15:26

SoundPlay should be OK for you.
User avatar
riohn
Posts: 4
Joined: 19 Jun 2021, 06:43

Re: Sound when pressing a key

19 Jun 2021, 15:36

mikeyww wrote:
19 Jun 2021, 15:26
SoundPlay should be OK for you.
yes, it's working.
this is how it looks like

Code: Select all

dir = %A_ScriptDir%
activated = C:\Users\Bae\Downloads\activated.mp3
deactivated = C:\Users\Bae\Downloads\deactivated.mp3
²::SoundPlay, % (on := !on) ? activated : deactivated
if SoundPlay, % (on := !on) ? activated :
 {
 RButton::
 loop
  {
   if getkeystate("RButton", "p") 
    {
      send, {t}
      sleep, 50
      send, {v}
    }
  else
    {
      break
    }
  }
 return
 }
  else
    {
      pause
    }
one last thing is about the second part, when "activated", i would like it to put the next script on, and when it's "deactivated" to put it off
Last edited by riohn on 19 Jun 2021, 15:55, edited 1 time in total.
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Sound when pressing a key  Topic is solved

19 Jun 2021, 15:54

Example:

Code: Select all

dir = %A_ScriptDir%
activated = C:\Users\Bae\Downloads\activated.mp3
deactivated = C:\Users\Bae\Downloads\deactivated.mp3
²::SoundPlay, % (on := !on) ? activated : deactivated
#If on
RButton::
While GetKeyState("RButton", "P") && on
{
 Send t
 Sleep, 50
 Send v
}
Return
#If
User avatar
riohn
Posts: 4
Joined: 19 Jun 2021, 06:43

Re: Sound when pressing a key

19 Jun 2021, 15:59

Thank you very much. working perfectly splendid!! :dance: :superhappy:
36_kills
Posts: 1
Joined: 12 Jan 2023, 11:05

Re: Sound when pressing a key

12 Jan 2023, 11:07

the script dosen't work for me :headwall:
User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Sound when pressing a key

12 Jan 2023, 12:16

Some keyboards do not have the needed key!

Things to check:
1. Do the sound files exist?
2. Are the hotkeys being triggered?
3. What are the values of the variables & conditional statements?
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Sound when pressing a key

12 Jan 2023, 17:24

mikeyww wrote:
19 Jun 2021, 08:43

Code: Select all

ComObjCreate("SAPI.SpVoice").Speak("That is a terrific script, exclamation point.")
Offtopic: On this line... How to increase playback speed?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 127 guests