-
[RESOLVED] Using Keypresses instead of Button clicks?
Hi there,
I'm currently creating a music video quiz program, where the user selects from one of four possible answers by clicking upon the corresponding button image or answer label. What I would like to do is have it so that the user can choose to press the keyboard buttons 1, 2, 3 or 4 (along the top of the keyboard preferably) so select an answer, as an alternative to clicking. Can this be done, if so, how do I do this?
When the button corresponding to the answer is clicked, the following code is initiated at present:
VB Code:
Private Sub imgButton_Click(Index As Integer)
'Disables the other buttons and answer labels
For x = 0 To 3
imgButton(x).Enabled = False
lblAnswer(x).Enabled = False
Next x
If loadCorrectNo = (Index + 1) Then
lblAnswer(Index).BackColor = RGB(85, 245, 85)
'Gives the answer a green background and adds to the score before loading a new question
YourScore = YourScore + (10 * QuestionTime)
Else
'Gives the answer a red background before loading a new question
lblAnswer(Index).BackColor = RGB(245, 45, 45)
End If
'Disables the question time counter as the question has been answered
tmrQuestionTime.Enabled = False
tmrNewQuestion.Enabled = True
End Sub
Thanks for any help you can provide,
GT
-
Re: Using Keypresses instead of Button clicks?
You can use the Keypress event of the Form. Set the KeyPreview property of the Form to true and use this code
VB Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("1"), Asc("2"), Asc("3"), Asc("4")
imgButton_Click (KeyAscii - 49)
End Select
End Sub
I am assuming that the array starts from 0.
-
Re: Using Keypresses instead of Button clicks?
set the key preview of the form to true then
on the keypress event of the form
VB Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 49
imgButton_Click 0
Case 50
imgButton_Click 1
Case 51
imgButton_Click 2
Case 52
imgButton_Click 3
End Select
End Sub
-
Re: Using Keypresses instead of Button clicks?
Quote:
Originally Posted by Shuja Ali
You can use the Keypress event of the Form. Set the KeyPreview property of the Form to true and use this code
VB Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("1"), Asc("2"), Asc("3"), Asc("4")
imgButton_Click (KeyAscii - 49)
End Select
End Sub
I am assuming that the array starts from 0.
Thanks for that, it worked perfectly. :)
-
Re: Using Keypresses instead of Button clicks?
Actually, I just noticed that, if all four keys (1, 2, 3 and 4) are pressed at once, then the user can cheat. How do I make it so that it will accept only the first key pressed and then be reset when the next question is loaded in?
-
Re: Using Keypresses instead of Button clicks?
KeyPress will handle only one key at a time. So even if user presses all the 4 keys simultaneously only one key will be processed.
However, you will need to modify your code for checking the answer.
-
Re: Using Keypresses instead of Button clicks?
Quote:
Originally Posted by Shuja Ali
KeyPress will handle only one key at a time. So even if user presses all the 4 keys simultaneously only one key will be processed.
However, you will need to modify your code for checking the answer.
The program has a label for each answer that lights up green or red based on whether or not it is correct. If I press 1, 2, 3 and 4 at the same time, all 4 labels light up instead of just the one corresponding to the first pressed key. So, I need to know how to block subsequent key presses until a new question loads.
-
Re: Using Keypresses instead of Button clicks?
Maybe it'll help if you separate theme in multiple cases:
VB Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("1"):
imgButton_Click (KeyAscii - 49)
Exit Sub
Case Asc("2"):
imgButton_Click (KeyAscii - 49)
Exit Sub
'...
'...
End Select
End Sub
-
Re: Using Keypresses instead of Button clicks?
Quote:
Originally Posted by gavio
Maybe it'll help if you separate theme in multiple cases:
VB Code:
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("1"):
imgButton_Click (KeyAscii - 49)
Exit Sub
Case Asc("2"):
imgButton_Click (KeyAscii - 49)
Exit Sub
'...
'...
End Select
End Sub
Thanks for the suggestion, I just gave this a try but the program behaved in exactly the same way, allowing all four answers to be selected at once.
-
Re: Using Keypresses instead of Button clicks?
-
Re: Using Keypresses instead of Button clicks?
Dont try it in the IDE, compile it and try it.
-
Re: Using Keypresses instead of Button clicks?
Quote:
Originally Posted by shragel
Dont try it in the IDE, compile it and try it.
Just did that and it performs in exactly the same way.
As for creating a flag, how do I do that?
-
Re: Using Keypresses instead of Button clicks?
I do something similar, but I use it for my menu system. You might be able to adapt it for your own use.
VB Code:
Private Sub Label2_Click(Index As Integer)
' this is for mouse entry
Select Case Index
Case 1: ' 1
frmJobEnter.Show
Case 2: ' 2
If Desk > 1 Then frmJobFind.Show
Case 3: ' 3
If Desk > 1 Then frmBobsForm.Show
Case 4: ' 4
If Desk > 1 Then frmA7200.Show
Case 5: ' 5
If UserName = "xx" Or UserName = "xxx" Or UserName = "xx" Then frmApral.Show
Case 6: ' 6
frmDriverEdit.Show
Case 7: ' 7
frmVerifyDockets.Show
Case 8: ' 8
If Desk > 1 Then frmMenuMaint.Show
Case 9: ' 9
Close
Dim frmForms As Form
For Each frmForms In Forms
If frmForms.hWnd <> Me.hWnd Then
Unload frmForms
End If
Next frmForms
Unload Me
End
End Select
End Sub
VB Code:
Select Case KeyCode
' this is for keyboard entry
Case 49, 97: ' 1
frmJobEnter.Show
Case 50, 98: ' 2
frmJobFind.Show
Case 51, 99: ' 3
If Desk > 1 Then frmBobsForm.Show
Case 52, 100: ' 4
If Desk > 1 Then frmA7200.Show
Case 53, 101: ' 5
If UserName = "xx" Or UserName = "xxx" Or UserName = "xx" Then frmApral.Show
Case 54, 102: ' 6
frmDriverEdit.Show
Case 55, 103: ' 7
frmVerifyDockets.Show
Case 56, 104: ' 8
If Desk > 1 Then frmMenuMaint.Show
Case 57, 105: ' 9
UnloadForm
Unload Me
End Select
End Sub
-
Re: Using Keypresses instead of Button clicks?
Quote:
Originally Posted by Supremus
I do something similar, but I use it for my menu system. You might be able to adapt it for your own use.
VB Code:
Select Case KeyCode
' this is for keyboard entry
Case 49, 97: ' 1
frmJobEnter.Show
Case 50, 98: ' 2
frmJobFind.Show
Case 51, 99: ' 3
If Desk > 1 Then frmBobsForm.Show
Case 52, 100: ' 4
If Desk > 1 Then frmA7200.Show
Case 53, 101: ' 5
If UserName = "xx" Or UserName = "xxx" Or UserName = "xx" Then frmApral.Show
Case 54, 102: ' 6
frmDriverEdit.Show
Case 55, 103: ' 7
frmVerifyDockets.Show
Case 56, 104: ' 8
If Desk > 1 Then frmMenuMaint.Show
Case 57, 105: ' 9
UnloadForm
Unload Me
End Select
End Sub
I don't quite understand, where do I place this code? I'm not exactly sure how to adapt this to fit my needs either.
-
Re: Using Keypresses instead of Button clicks?
Declare a Global Variable
Set it in your form load:
In you Procedure:
VB Code:
'Test your flag first
if not bFlag then
Exit Sub
end if
'Set your flag so that further key presses won't get through
bFlag = False
'run your case statement
Select Case KeyAscii
Case 49
imgButton_Click 0
Case 50
imgButton_Click 1
Case 51
imgButton_Click 2
Case 52
imgButton_Click 3
End Select
'reset your flag
bFlag=True
I hope this helps, doing this off the cuff with a monster headache....
-
Re: Using Keypresses instead of Button clicks?
Quote:
Originally Posted by dminder
Declare a Global Variable
Set it in your form load:
In you Procedure:
VB Code:
'Test your flag first
if not bFlag then
Exit Sub
end if
'Set your flag so that further key presses won't get through
bFlag = False
'run your case statement
Select Case KeyAscii
Case 49
imgButton_Click 0
Case 50
imgButton_Click 1
Case 51
imgButton_Click 2
Case 52
imgButton_Click 3
End Select
'reset your flag
bFlag=True
I hope this helps, doing this off the cuff with a monster headache....
I'll give that a go in the morning, thanks alot.
-
Re: Using Keypresses instead of Button clicks?
Quote:
Originally Posted by gt123
I'll give that a go in the morning, thanks alot.
I finally got around to trying this, and it had no effect. The program still behaves in exactly the same way. All 4 possible answers can be selected at once by pressing keys 1 to 4 simulataneously.
-
Re: Using Keypresses instead of Button clicks?
try something like this:
VB Code:
Private bIgnore As Boolean
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 49 To 53
If Not bIgnore Then
cmdArray(KeyAscii - 49).Value = True
bIgnore = True
End If
End Select
End Sub
' Then once you've loaded the new question allow presses again:
Private Sub LoadQuestion()
' Load Question
bIgnore = False
End Sub
-
Re: Using Keypresses instead of Button clicks?
Don't do the checking of answers in the Click event of imgButton. Just do the selection and have another button and in that buttons click event do the validation of answers.
-
Re: Using Keypresses instead of Button clicks?
Quote:
Originally Posted by bushmobile
try something like this:
VB Code:
Private bIgnore As Boolean
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 49 To 53
If Not bIgnore Then
cmdArray(KeyAscii - 49).Value = True
bIgnore = True
End If
End Select
End Sub
' Then once you've loaded the new question allow presses again:
Private Sub LoadQuestion()
' Load Question
bIgnore = False
End Sub
Where in all that to I play the code for the button click, so that the answer is checked etc?
Quote:
Originally Posted by Shuja Ali
Don't do the checking of answers in the Click event of imgButton. Just do the selection and have another button and in that buttons click event do the validation of answers.
I'm a novice with VB6, so I have no idea how i'd go about this.
-
Re: Using Keypresses instead of Button clicks?
VB Code:
cmdArray(KeyAscii - 49).Value = True
that is the button click.
but if you're not using a button then just replace it with what you were using before imgButton_Click (KeyAscii - 49)
-
Re: Using Keypresses instead of Button clicks?
That solved the problem, thanks alot for your help everyone. :)
-
Re: Using Keypresses instead of Button clicks?
Why not use radio buttons?
Put them in a frame and this way you can only select one answer.
You can use the number keys to select one button.
-
Re: Using Keypresses instead of Button clicks?
Quote:
Originally Posted by Supremus
Why not use radio buttons?
Put them in a frame and this way you can only select one answer.
Radio buttons would be a bit too small on the form for what I need. The problem is solved now anyway, so i'm happy with it. :)