Results 1 to 24 of 24

Thread: [RESOLVED] Using Keypresses instead of Button clicks?

  1. #1

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    Resolved [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:
    1. Private Sub imgButton_Click(Index As Integer)
    2.     'Disables the other buttons and answer labels
    3.         For x = 0 To 3
    4.             imgButton(x).Enabled = False
    5.             lblAnswer(x).Enabled = False
    6.         Next x
    7.            
    8.         If loadCorrectNo = (Index + 1) Then
    9.             lblAnswer(Index).BackColor = RGB(85, 245, 85)
    10.     'Gives the answer a green background and adds to the score before loading a new question
    11.             YourScore = YourScore + (10 * QuestionTime)
    12.         Else
    13.     'Gives the answer a red background before loading a new question
    14.             lblAnswer(Index).BackColor = RGB(245, 45, 45)
    15.         End If
    16.        
    17.     'Disables the question time counter as the question has been answered
    18.         tmrQuestionTime.Enabled = False
    19.         tmrNewQuestion.Enabled = True
    20. 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.

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2.     Select Case KeyAscii
    3.         Case Asc("1"), Asc("2"), Asc("3"), Asc("4")
    4.             imgButton_Click (KeyAscii - 49)
    5.     End Select
    6. 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

  3. #3
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Philippines
    Posts
    468

    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:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2. Select Case KeyAscii
    3.     Case 49
    4.         imgButton_Click 0
    5.     Case 50
    6.          imgButton_Click 1
    7.     Case 51
    8.          imgButton_Click 2
    9.     Case 52
    10.          imgButton_Click 3
    11. End Select
    12. End Sub
    if you have a problem face it
    but what if your face is your problem
    how can you face it

  4. #4

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    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:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2.     Select Case KeyAscii
    3.         Case Asc("1"), Asc("2"), Asc("3"), Asc("4")
    4.             imgButton_Click (KeyAscii - 49)
    5.     End Select
    6. End Sub
    I am assuming that the array starts from 0.
    Thanks for that, it worked perfectly.

  5. #5

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    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?

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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

  7. #7

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    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.

  8. #8
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Using Keypresses instead of Button clicks?

    Maybe it'll help if you separate theme in multiple cases:

    VB Code:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2.     Select Case KeyAscii
    3.         Case Asc("1"):
    4.             imgButton_Click (KeyAscii - 49)
    5.                 Exit Sub
    6.         Case Asc("2"):
    7.             imgButton_Click (KeyAscii - 49)
    8.                 Exit Sub
    9.         '...
    10.         '...
    11.     End Select
    12. End Sub

  9. #9

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    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:
    1. Private Sub Form_KeyPress(KeyAscii As Integer)
    2.     Select Case KeyAscii
    3.         Case Asc("1"):
    4.             imgButton_Click (KeyAscii - 49)
    5.                 Exit Sub
    6.         Case Asc("2"):
    7.             imgButton_Click (KeyAscii - 49)
    8.                 Exit Sub
    9.         '...
    10.         '...
    11.     End Select
    12. 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.

  10. #10
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    Philippines
    Posts
    468

    Re: Using Keypresses instead of Button clicks?

    create a flag
    if you have a problem face it
    but what if your face is your problem
    how can you face it

  11. #11
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Re: Using Keypresses instead of Button clicks?

    Dont try it in the IDE, compile it and try it.

  12. #12

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    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?

  13. #13
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    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:
    1. Private Sub Label2_Click(Index As Integer)
    2. ' this is for mouse entry
    3.  
    4.   Select Case Index
    5.   Case 1:  ' 1
    6.     frmJobEnter.Show
    7.   Case 2:  ' 2
    8.     If Desk > 1 Then frmJobFind.Show
    9.   Case 3:  ' 3
    10.     If Desk > 1 Then frmBobsForm.Show
    11.   Case 4:  ' 4
    12.     If Desk > 1 Then frmA7200.Show
    13.   Case 5:  ' 5
    14.     If UserName = "xx" Or UserName = "xxx" Or UserName = "xx" Then frmApral.Show
    15.   Case 6:  ' 6
    16.     frmDriverEdit.Show
    17.   Case 7:  ' 7
    18.     frmVerifyDockets.Show
    19.   Case 8:  ' 8
    20.     If Desk > 1 Then frmMenuMaint.Show
    21.   Case 9:  ' 9
    22.  
    23.     Close
    24.     Dim frmForms As Form
    25.     For Each frmForms In Forms
    26.       If frmForms.hWnd <> Me.hWnd Then
    27.         Unload frmForms
    28.       End If
    29.     Next frmForms
    30.     Unload Me
    31.     End
    32.  
    33.   End Select
    34. End Sub

    VB Code:
    1. Select Case KeyCode
    2. ' this is for keyboard entry
    3.   Case 49, 97:  ' 1
    4.     frmJobEnter.Show
    5.  
    6.   Case 50, 98:  ' 2
    7.     frmJobFind.Show
    8.   Case 51, 99:  ' 3
    9.     If Desk > 1 Then frmBobsForm.Show
    10.  
    11.   Case 52, 100:  ' 4
    12.     If Desk > 1 Then frmA7200.Show
    13.   Case 53, 101:  ' 5
    14.     If UserName = "xx" Or UserName = "xxx" Or UserName = "xx" Then frmApral.Show
    15.  
    16.   Case 54, 102:  ' 6
    17.     frmDriverEdit.Show
    18.  
    19.   Case 55, 103:  ' 7
    20.     frmVerifyDockets.Show
    21.   Case 56, 104:  ' 8
    22.     If Desk > 1 Then frmMenuMaint.Show
    23.   Case 57, 105:  ' 9
    24.     UnloadForm
    25.     Unload Me
    26.   End Select
    27. End Sub
    My reputation is in tatters. Don't bother trying to repair it.

  14. #14

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    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:
    1. Select Case KeyCode
    2. ' this is for keyboard entry
    3.   Case 49, 97:  ' 1
    4.     frmJobEnter.Show
    5.  
    6.   Case 50, 98:  ' 2
    7.     frmJobFind.Show
    8.   Case 51, 99:  ' 3
    9.     If Desk > 1 Then frmBobsForm.Show
    10.  
    11.   Case 52, 100:  ' 4
    12.     If Desk > 1 Then frmA7200.Show
    13.   Case 53, 101:  ' 5
    14.     If UserName = "xx" Or UserName = "xxx" Or UserName = "xx" Then frmApral.Show
    15.  
    16.   Case 54, 102:  ' 6
    17.     frmDriverEdit.Show
    18.  
    19.   Case 55, 103:  ' 7
    20.     frmVerifyDockets.Show
    21.   Case 56, 104:  ' 8
    22.     If Desk > 1 Then frmMenuMaint.Show
    23.   Case 57, 105:  ' 9
    24.     UnloadForm
    25.     Unload Me
    26.   End Select
    27. 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.

  15. #15
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: Using Keypresses instead of Button clicks?

    Declare a Global Variable
    VB Code:
    1. Dim bFlag as Boolean

    Set it in your form load:
    VB Code:
    1. bFlag = True

    In you Procedure:
    VB Code:
    1. 'Test your flag first
    2. if not bFlag then
    3.    Exit Sub
    4. end if
    5.  
    6. 'Set your flag so that further key presses won't get through
    7. bFlag = False
    8. 'run your case statement
    9. Select Case KeyAscii
    10.     Case 49
    11.         imgButton_Click 0
    12.     Case 50
    13.          imgButton_Click 1
    14.     Case 51
    15.          imgButton_Click 2
    16.     Case 52
    17.          imgButton_Click 3
    18. End Select
    19.  
    20. 'reset your flag
    21. bFlag=True

    I hope this helps, doing this off the cuff with a monster headache....

  16. #16

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    Re: Using Keypresses instead of Button clicks?

    Quote Originally Posted by dminder
    Declare a Global Variable
    VB Code:
    1. Dim bFlag as Boolean

    Set it in your form load:
    VB Code:
    1. bFlag = True

    In you Procedure:
    VB Code:
    1. 'Test your flag first
    2. if not bFlag then
    3.    Exit Sub
    4. end if
    5.  
    6. 'Set your flag so that further key presses won't get through
    7. bFlag = False
    8. 'run your case statement
    9. Select Case KeyAscii
    10.     Case 49
    11.         imgButton_Click 0
    12.     Case 50
    13.          imgButton_Click 1
    14.     Case 51
    15.          imgButton_Click 2
    16.     Case 52
    17.          imgButton_Click 3
    18. End Select
    19.  
    20. 'reset your flag
    21. 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.

  17. #17

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    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.

  18. #18
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Using Keypresses instead of Button clicks?

    try something like this:
    VB Code:
    1. Private bIgnore As Boolean
    2.  
    3. Private Sub Form_KeyPress(KeyAscii As Integer)
    4.     Select Case KeyAscii
    5.         Case 49 To 53
    6.             If Not bIgnore Then
    7.                 cmdArray(KeyAscii - 49).Value = True
    8.                 bIgnore = True
    9.             End If
    10.     End Select
    11. End Sub
    12.  
    13. ' Then once you've loaded the new question allow presses again:
    14. Private Sub LoadQuestion()
    15.     ' Load Question
    16.     bIgnore = False
    17. End Sub

  19. #19
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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

  20. #20

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    Re: Using Keypresses instead of Button clicks?

    Quote Originally Posted by bushmobile
    try something like this:
    VB Code:
    1. Private bIgnore As Boolean
    2.  
    3. Private Sub Form_KeyPress(KeyAscii As Integer)
    4.     Select Case KeyAscii
    5.         Case 49 To 53
    6.             If Not bIgnore Then
    7.                 cmdArray(KeyAscii - 49).Value = True
    8.                 bIgnore = True
    9.             End If
    10.     End Select
    11. End Sub
    12.  
    13. ' Then once you've loaded the new question allow presses again:
    14. Private Sub LoadQuestion()
    15.     ' Load Question
    16.     bIgnore = False
    17. 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.

  21. #21
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Using Keypresses instead of Button clicks?

    VB Code:
    1. 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)

  22. #22

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    Re: Using Keypresses instead of Button clicks?

    That solved the problem, thanks alot for your help everyone.

  23. #23
    Hyperactive Member
    Join Date
    Feb 2005
    Location
    Wollongong. NSW. Australia
    Posts
    470

    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.

  24. #24

    Thread Starter
    Addicted Member gt123's Avatar
    Join Date
    May 2006
    Location
    United Kingdom
    Posts
    145

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width