play sound when certain keys are hit

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Snickers
Macro Veteran
Posts: 151
Joined: Thu Dec 09, 2004 3:01 pm
Location: Somewhere in TX

play sound when certain keys are hit

Post by Snickers » Fri May 25, 2012 8:13 pm

During a portion of my script, I would like to listen for certain keys to be hit. If those keys are hit, I want a wav file to be played. the keys to be hit can come in any order so it looks like I can't use waitkeydown.

How can I have macro scheduler listen for any key?

for example:

listenforkey

if> A pressed
playwav
endif

if> S pressed
playwav
endif

if> D pressed
playwav
endif

etc.

I always appreciate your help.

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri May 25, 2012 9:00 pm

Code: Select all

//Plays the tone when an "a" or "A" is pressed
OnEvent>Key_Down,A,0,Tone

Let>ToneFlag=0
Label>Loop
  Wait>0.01
  //The use of the flag prevents the tone from playing repetitively
  //when the key is held down
  If>ToneFlag>0
    Add>ToneFlag,1
    If>ToneFlag>3
      Let>ToneFlag=0
    EndIF
  EndIf
Goto>Loop

SRT>Tone
  If>ToneFlag=0
    //Substitute a wav file if you'd rather
    LibFunc>kernel32,Beep,Bres,600,50
  EndIF
  Let>ToneFlag=1
END>Tone

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Sun May 27, 2012 10:32 pm

In this example I have OnEvent set to play a wav file.

Virtual key codes (VK) are located here http://www.mjtnet.com/vkcodes.htm

I hope this helps.

Code: Select all

OnEvent>KEY_DOWN,VK65,0,Key_A
OnEvent>KEY_DOWN,VK66,0,Key_B
OnEvent>KEY_DOWN,VK67,0,Key_C
OnEvent>KEY_DOWN,VK68,0,Key_D
OnEvent>KEY_DOWN,VK69,0,Key_E
OnEvent>KEY_DOWN,VK70,0,Key_F

Label>Loop
Wait>.1
Goto>Loop

SRT>Key_A
 PlayWav>C:\Audio\Wav\A.wav
END>Key_A

SRT>Key_B
 PlayWav>C:\Audio\Wav\B.wav
END>Key_B

SRT>Key_C
 PlayWav>C:\Audio\Wav\C.wav
END>Key_C

SRT>Key_D
 PlayWav>C:\Audio\Wav\D.wav
END>Key_D

SRT>Key_E
 PlayWav>C:\Audio\Wav\E.wav
END>Key_E

SRT>Key_F
 PlayWav>C:\Audio\Wav\F.wav
END>Key_F


Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts