Play tone while key is held down?
I want to play a tone while the key is held down and immediately stop when key is up. I have messed with the Beep API and it plays it through the PC internal speaker. I need a tone to play through the soundcard speakers.
The only way I know through the soundcard is a wav file but I need to adjust the length of how long is it played. It could be 1/2 a second, could be 1 full second for the tone to play.
Any suggestions?
Thanks!
Re: Play tone while key is held down?
Re: Play tone while key is held down?
Thanks. I tried it and didn't work like I needed. I'm wanting to produce tones for morse code. But I can't play a wav file cause you are creating how long each tone is. All I can do is use Beep api but thats not through the soundcard speaker.
Anyone have any advice?
Thanks!
Re: Play tone while key is held down?
Re: Play tone while key is held down?
Why not try playing a dot when you press down the left mouse button and a dash when you press the right mouse button?
Re: Play tone while key is held down?
Here try this, it works for me. But you have to have a device isntalled the plays back .wav files. If not just convert your Beep.wav file to an MP3 and it'll work just fine.
Put this into a module or declare them private and put them in your form.
Code:
Public Declare Function mciSendString Lib "winmm.dll" Alias _
"mciSendStringA" (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As Any, ByVal uReturnLength As Long, ByVal _
hwndCallback As Long) As Long
Public Declare Function mciGetErrorString Lib "winmm.dll" Alias _
"mciGetErrorStringA" (ByVal dwError As Long, ByVal _
lpstrBuffer As String, ByVal uLength As Long) As Long 'Get the error message of the mcidevice if any
Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) _
As Long
Public Function Short_Name(Long_Path As String) As String
'Returns short pathname of the passed lo
' ng pathname
Dim Short_Path As String
Dim PathLength As Long
Short_Path = Space(250)
PathLength = GetShortPathName(Long_Path, Short_Path, Len(Short_Path))
If PathLength Then
Short_Name = Left$(Short_Path, PathLength)
End If
End Function
You use the Short_Name() function if the path to file is a long one.
Now put this into your form and replace the Form1 with your forms name.
Code:
Option Explicit
Dim boolPlaying As Boolean
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Dim ret As Long
Dim buf As String
Dim pos As Integer
If boolPlaying = False Then
If KeyCode = vbKeyUp Then
mciSendString "stop Sound", 0&, 0, 0
mciSendString "close Sound", 0&, 0, 0
ret = mciSendString("open PathToBeepFile type waveaudio alias Sound", 0&, 0, 0)
If ret <> 0 Then
buf = Space$(1024)
mciGetErrorString ret, buf, Len(buf)
pos = InStr(buf, Chr$(0))
buf = Left$(buf, pos - 1)
MsgBox buf
Exit Sub
End If
mciSendString "play Sound", 0&, 0, 0
boolPlaying = True
End If
End If
End Sub
Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
If boolPlaying = True Then
If KeyCode = vbKeyUp Then
mciSendString "stop Sound", 0&, 0, 0
mciSendString "close Sound", 0&, 0, 0
boolPlaying = False
End If
End If
End Sub
Hope it works, also if you do convert your wave file into and MP3 file remove the "type waveaudio" from "ret = mciSendString("open PathToBeepFile type waveaudio alias Sound", 0&, 0, 0)".
-Jaquio