|
-
Aug 18th, 2006, 08:26 AM
#1
Thread Starter
Addicted Member
[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
Last edited by Hack; Aug 21st, 2006 at 10:40 AM.
Reason: Last edited by gt123 : 08-19-2006 at 08:31 AM. Reason: No longer resolved.
-
Aug 18th, 2006, 08:44 AM
#2
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.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Aug 18th, 2006, 08:48 AM
#3
Hyperactive Member
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
if you have a problem face  it
but what if your face  is your problem
how can you face  it 
-
Aug 18th, 2006, 10:11 AM
#4
Thread Starter
Addicted Member
Re: Using Keypresses instead of Button clicks?
 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.
-
Aug 19th, 2006, 07:32 AM
#5
Thread Starter
Addicted Member
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?
-
Aug 19th, 2006, 07:43 AM
#6
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.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Aug 19th, 2006, 07:46 AM
#7
Thread Starter
Addicted Member
Re: Using Keypresses instead of Button clicks?
 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.
-
Aug 19th, 2006, 08:48 AM
#8
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
-
Aug 19th, 2006, 09:26 AM
#9
Thread Starter
Addicted Member
Re: Using Keypresses instead of Button clicks?
 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.
-
Aug 19th, 2006, 09:12 PM
#10
Hyperactive Member
Re: Using Keypresses instead of Button clicks?
if you have a problem face  it
but what if your face  is your problem
how can you face  it 
-
Aug 19th, 2006, 11:27 PM
#11
Frenzied Member
Re: Using Keypresses instead of Button clicks?
Dont try it in the IDE, compile it and try it.
-
Aug 20th, 2006, 04:14 AM
#12
Thread Starter
Addicted Member
Re: Using Keypresses instead of Button clicks?
 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?
-
Aug 20th, 2006, 05:16 AM
#13
Hyperactive Member
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
My reputation is in tatters. Don't bother trying to repair it.
-
Aug 21st, 2006, 10:36 AM
#14
Thread Starter
Addicted Member
Re: Using Keypresses instead of Button clicks?
 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.
-
Aug 21st, 2006, 02:55 PM
#15
Fanatic Member
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....
-
Aug 21st, 2006, 03:44 PM
#16
Thread Starter
Addicted Member
Re: Using Keypresses instead of Button clicks?
 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.
-
Aug 23rd, 2006, 06:17 AM
#17
Thread Starter
Addicted Member
Re: Using Keypresses instead of Button clicks?
 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.
-
Aug 23rd, 2006, 06:51 AM
#18
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
-
Aug 23rd, 2006, 06:52 AM
#19
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.
Use [code] source code here[/code] tags when you post source code.
My Articles
-
Aug 23rd, 2006, 06:57 AM
#20
Thread Starter
Addicted Member
Re: Using Keypresses instead of Button clicks?
 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?
 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.
-
Aug 23rd, 2006, 06:58 AM
#21
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)
-
Aug 23rd, 2006, 07:06 AM
#22
Thread Starter
Addicted Member
Re: Using Keypresses instead of Button clicks?
That solved the problem, thanks alot for your help everyone.
-
Aug 23rd, 2006, 07:08 AM
#23
Hyperactive Member
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.
My reputation is in tatters. Don't bother trying to repair it.
-
Aug 23rd, 2006, 07:09 AM
#24
Thread Starter
Addicted Member
Re: Using Keypresses instead of Button clicks?
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|