• 0

Numbers, Command Buttons and VB6


Question

I have 10 buttons on a form, all called 'cmd[whatever]'. These are captioned from 1 to 10.

I would like to make it so that when the numbers (on the top of the keyboard or the numpad) are pressed, it will initiate the button and its action (instead of clicking on it).

Thanks.

EDIT: 1 to 9, and 0 in place of 10. :)

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

I wrote...

Private Sub Form1_keypress(KeyAsciii as Integer)

If KeyAscii = 98 Then

msgbox blah blah

end sub

...to test it but it still refuses to work, although I do not get any errors. What am I doing wrong here? (98 = b)

Link to comment
Share on other sites

  • 0

It would only be a keypress on the form if you have no buttons, i think.

Private Function Key(asc as integer)
   Select Case asc
        case 98
            msgbox "blah"
    end select
end function


'then put 
'
'    "Key keyascii"
'
'in every buttons keypress event.

Link to comment
Share on other sites

  • 0

keypress event happens for watever conrols is in focus. so you have to take care of that and as elliot said you can write a central method and call that from every control's keypress event.

Link to comment
Share on other sites

  • 0

But would I then have to make 10 central events, a unique one for each button?

As the program stands.......each button must be assigned a number.

Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

Private Sub cmdAirlockC_Click()
sndPlaySound "C:\Documents and Settings\Maxim\Desktop\thing\Sounds\airlockclose.wav", SND_ASYNC Or SND_FILENAME
End Sub

Private Sub cmdAlarm_Click()
sndPlaySound "C:\Documents and Settings\Maxim\Desktop\thing\Sounds\ng3sonaalarm.wav", SND_ASYNC Or SND_FILENAME
End Sub

Private Sub cmdBeeps_Click()
sndPlaySound "C:\Documents and Settings\Maxim\Desktop\thing\Sounds\quickbeeps.wav", SND_ASYNC Or SND_FILENAME
End Sub

Private Sub cmdDeep_Click()
sndPlaySound "C:\Documents and Settings\Maxim\Desktop\thing\Sounds\deepflobbawobba.wav", SND_ASYNC Or SND_FILENAME
End Sub

Private Sub cmdDown_Click()
sndPlaySound "C:\Documents and Settings\Maxim\Desktop\thing\Sounds\systemsdown.wav", SND_ASYNC Or SND_FILENAME
End Sub

Private Sub cmdGeneral_Click()
sndPlaySound "C:\Documents and Settings\Maxim\Desktop\thing\Sounds\rsegeneralalarm.wav", SND_ASYNC Or SND_FILENAME
End Sub

Private Sub cmdGun_Click()
sndPlaySound "C:\Documents and Settings\Maxim\Desktop\thing\Sounds\guncockedpumpedshot.wav", SND_ASYNC Or SND_FILENAME
End Sub
Private Sub cmdReturn_Click()
sndPlaySound "C:\Documents and Settings\Maxim\Desktop\thing\Sounds\soundreturn.wav", SND_ASYNC Or SND_FILENAME
End Sub
Private Sub cmdMotion_Click()
sndPlaySound "C:\Documents and Settings\Maxim\Desktop\thing\Sounds\motionbase.wav", SND_ASYNC Or SND_FILENAME
End Sub

Private Sub cmdScan_Click()
sndPlaySound "C:\Documents and Settings\Maxim\Desktop\thing\Sounds\scan.wav", SND_ASYNC Or SND_FILENAME
End Sub

Link to comment
Share on other sites

  • 0

Thanks for the help guys, but I am still a bit confused.

I have the sound working, and it plays when 'B' is pressed - however the command button has to be selected before it plays....how can i make it so that it is adjusted for the whole form? (iShakeel said something about the form, but I want to know how to do this)

Link to comment
Share on other sites

  • 0

Private Sub cmdBeeps_KeyPress(KeyAscii As Integer)
KeySound KeyAscii
End Sub

Private Sub cmdDeep_KeyPress(KeyAscii As Integer)
KeySound KeyAscii
End Sub

Private Sub cmdDown_KeyPress(KeyAscii As Integer)
KeySound KeyAscii
End Sub

Private Sub KeySound(code As Integer)
    Select Case code
        Case Asc("b")
            MsgBox "play the b sound"
        Case Asc("d")
            MsgBox "play the d sound"
        Case Asc("p")
            MsgBox "play the p sound"
    End Select
End Sub

You see every buttons keypress gets the ascii code and sends it to the central sub, this then plays the correct sound for that key. It works no matter which button has focus.

Link to comment
Share on other sites

  • 0
You see every buttons keypress gets the ascii code and sends it to the central sub, this then plays the correct sound for that key. It works no matter which button has focus.

584775338[/snapback]

Thanks! Got it working perfectly :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.