Results 1 to 16 of 16

Thread: Disable Enter on command button

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Disable Enter on command button

    Hi there!

    Been reading the forums for a while, very good comunity and tremendous help for a lot of things ive been doing. Although i'm having problems with a small issue at the moment.

    I found a post HERE that should help me, but having tried it its not really working well at all for the job.

    I may be doing something wrong, but i don't think so. Using pnish's code a little way down the page it thinks i'm pressing enter even when im clicking the mouse, sometimes it will get it right but a lot of the time not. I need this to be accurate as for every click of this command button im counting.

    In fact this is just for a little game ive been asked to make for demonstration. It sounded really simple. The idea is you must click the button as many times as you can in 10 seconds, but cheating is possible if you just hold down enter.

    I have the whole thing working at the moment apart from the cheat prevention:

    VB Code:
    1. Private Sub cmdStart_Click()
    2.     Timer1.Interval = 1000
    3.     txttimer.Text = "10"
    4.     cmdClicker.Visible = True
    5.     cmdStart.Visible = False
    6. End Sub
    7.  
    8. Private Sub Form_Load()
    9.   txtcounter.Text = 0
    10.   cmdClicker.Visible = False
    11.   cmdNext.Visible = False
    12. End Sub
    13. Private Sub Timer1_Timer()
    14.      If txttimer.Text = 0 Then
    15.           Timer1.Enabled = False
    16.         cmdClicker.Visible = False
    17.         cmdNext.Visible = True
    18.      Else
    19.           txttimer.Text = txttimer.Text - 1
    20.      End If
    21. End Sub
    22.  
    23. Private Sub cmdclicker_click()
    24.   txtcounter.Text = txtcounter.Text + 1
    25. End Sub

    Heres what should work using the [RESOLVED] code from pnish:

    VB Code:
    1. Dim MouseClicked As Boolean
    2.  
    3. Private Sub cmdclicker_Click()
    4.  
    5.     If Not MouseClicked Then    ' Assume the user pressed enter
    6.         MsgBox ("cheat")
    7.         Exit Sub
    8.     Else
    9.         '
    10.         '   Do your button-click stuff here
    11.         '
    12.         txtcounter.Text = txtcounter.Text + 1
    13.         MouseClicked = False
    14.     End If
    15.     End Sub
    16.  
    17. Private Sub Cmdclicker_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    18.     '
    19.     '   Flag that the mouse button has been pressed
    20.     '   (May want to check for left or right button etc)
    21.     '
    22.     MouseClicked = True
    23.  
    24. End Sub
    25.  
    26. Private Sub cmdclicker_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    27.     '
    28.     '   Flag that the mouse button has been released
    29.     '   (May want to check for left or right button etc)
    30.     '
    31.     If MouseClicked = True Then
    32.         MouseClicked = False
    33.     End If
    34.     End Sub
    35.    
    36. Private Sub cmdStart_Click()
    37.     Timer1.Interval = 1000
    38.     txttimer.Text = "10"
    39.     cmdClicker.Visible = True
    40.     cmdStart.Visible = False
    41. End Sub
    42.  
    43. Private Sub Form_Load()
    44.   txtcounter.Text = 0
    45.   cmdClicker.Visible = False
    46.   cmdNext.Visible = False
    47. End Sub
    48. Private Sub Timer1_Timer()
    49.      If txttimer.Text = 0 Then
    50.           Timer1.Enabled = False
    51.         cmdClicker.Visible = False
    52.         cmdNext.Visible = True
    53.      Else
    54.           txttimer.Text = txttimer.Text - 1
    55.      End If
    56. End Sub

    Just wondered if in any way you masterminds can shed some light on the problem. I must be missing something very trivial, its so simple yet so hard.

    Thanks for any help!!!!

    Mathew

    PS. If you want me to actually post the program, i'm very willing to do so!

  2. #2
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: Disable Enter on command button

    I think that because you are clicking at a tremendous speed, the mouseup gets fired before the click event, causing the click event to read a "Not MouseDown". Do it the other way around. In the keydown event, set a KeyDown = true. In the button, if KeyDown = true then ignore... Don't even set the KeyDown back to false. If it's a game, you can try to cheat, but then you lose

    r0ach™
    Don't forget to rate the post

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Re: Disable Enter on command button

    hi there!

    wow, thanks for the quick reply.

    is this what you meant?

    VB Code:
    1. Private Sub cmdclicker_Click()
    2.  
    3.     If KeyDown = True Then
    4.       MsgBox ("cheat")
    5.     Else
    6.       txtcounter.Text = txtcounter.Text + 1
    7.    End If
    8. End Sub
    9. Private Sub cmdClicker_KeyDown(KeyCode As Integer, Shift As Integer)
    10.   KeyDown = True
    11. End Sub
    12.    
    13. Private Sub cmdStart_Click()
    14.     Timer1.Interval = 1000
    15.     txttimer.Text = "10"
    16.     cmdClicker.Visible = True
    17.     cmdStart.Visible = False
    18. End Sub
    19.  
    20. Private Sub Form_Load()
    21.   txtcounter.Text = 0
    22.   cmdClicker.Visible = False
    23.   cmdNext.Visible = False
    24. End Sub
    25. Private Sub Timer1_Timer()
    26.      If txttimer.Text = 0 Then
    27.           Timer1.Enabled = False
    28.         cmdClicker.Visible = False
    29.         cmdNext.Visible = True
    30.      Else
    31.           txttimer.Text = txttimer.Text - 1
    32.      End If
    33. End Sub

    becuase it still allowing enter if thats what you mean, i dont get any messagebox after pressing enter either.

    thanks again!

    Matt

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Disable Enter on command button

    I'm pressing ENTER in a CommandButton and nothing happends, it gets pressed with SPACE KeyDown, not Enter (or I'm missing something?)

    If its just Space you can do this..
    VB Code:
    1. Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
    2.     If KeyCode = vbKeySpace Then
    3.         KeyCode = 0
    4.     End If
    5. End Sub

  5. #5
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: Disable Enter on command button

    Yes, that's what I meant, but the problem now is that the button becomes the default. There's a post somewhere (today) where someone wanted to avoid this, too.

    http://www.vbforums.com/showthread.php?t=372789

    r0ach™
    Don't forget to rate the post

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Re: Disable Enter on command button

    ahh yes jcis, the code you entered does stop space bar, but as you say it wont do the same for the enter button for some reason...

    strange

    Matt

  7. #7
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: Disable Enter on command button

    Quote Originally Posted by jcis
    I'm pressing ENTER in a CommandButton and nothing happends, it gets pressed with SPACE KeyDown, not Enter (or I'm missing something?)
    jcis, the keyboard should have absolutely no effect on the button. The keyboard's repeat rate allows for devestatingly high "scores" which is what MathewF is trying to avoid.

    r0ach™
    Don't forget to rate the post

  8. #8
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Disable Enter on command button

    The keyboard's repeat rate allows for devestatingly high "scores" which is what MathewF is trying to avoid.
    But how much repeat rate could be if you avoid the Space Key, as I do in my previous post?

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Re: Disable Enter on command button

    Right, i've devised a work around for the problem using the other page you gave r0ach.

    I'm using a command button off he visible area of the form that is made to be the focus after every button click of the counter button.

    Also using the Code to stop the space bar press i think its un-cheatable now....

    You may call this a bit of a cheat to stop cheats but i guess it works and thats all that matters....lol

    Again thanks for all the help you guys!!

    I wonder if a correct way of doing this will ever appear?

    Matt

  10. #10
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Disable Enter on command button

    Look at this..

    VB Code:
    1. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
    2.  
    3. Private Sub Command1_Click()
    4.  
    5.     If GetAsyncKeyState(13) Or GetAsyncKeyState(vbKeySpace) Then
    6.         Exit Sub   '(Don't compute this click)
    7.     End If
    8.     MsgBox "Clicked!" 'Compute this click!
    9. End Sub

  11. #11
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: Disable Enter on command button

    MathewF said it's a game that counts how many times you click the button in 10 seconds. Holding down Enter (or space) continuesly presses the button. We want to avoid this. The user should click with the mouse only.

    Here's a suggestion: remove the cmdClicker button, and replace it with a picturebox. In the click event, add 1 to the counter, IN THE DoubleClick Event ADD 2 to the counter

    r0ach™
    Don't forget to rate the post

  12. #12
    Fanatic Member r0ach's Avatar
    Join Date
    Dec 1999
    Location
    South Africa
    Posts
    722

    Re: Disable Enter on command button

    @jcis: YES! That's cool. (I'll have to rate that one on monday)

    Mathew: Replace the cmdClicker_Click Code with this
    VB Code:
    1. If GetAsyncKeyState(13) Or GetAsyncKeyState(vbKeySpace) Then
    2.         Exit Sub   '(Don't compute this click)
    3.     End If
    4.     txtCounter = txtCounter + 1

    You can also remove the KeyDown event code, and the KeyDown boolean

    r0ach™
    Don't forget to rate the post

  13. #13
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: Disable Enter on command button

    I think it works.
    The only thing is: If you keep Enter and Space pressed, then you release Enter, and finally you realease Space, the click will be fired, but just 1 time,
    and i think that 1 time is no problrm here.
    You can test it with the inmediate window, like this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     If GetAsyncKeyState(13) Or GetAsyncKeyState(vbKeySpace) Then
    3.         Exit Sub
    4.         '(Don't compute this click)
    5.     End If
    6.     Debug.Print "pressed"
    7. End Sub

    (the debug window is not devastated)

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Re: Disable Enter on command button

    hey!! well done guys!

    thanks this works really well...ill be sure to show you the end product soon!

    Matt

  15. #15
    New Member
    Join Date
    Dec 2005
    Posts
    1

    Re: Disable Enter on command button

    Hello everybody. I had a problem very similar. I have a command button in a form and I do not want the click_event to be fired when <ENTER> is pressed (mouse is disabled all the time). Thank to all of you, I solved it using the API 'GetAsyncKeyState', but I would like to know what I was doing wrong. At first I thought of using the keydown event of the commandbutton. When <ENTER> is pressed I just cancel it by setting keydown=0, BUT I could not managed to do this since KEYDOWN EVENT OF COMMAND BUTTON IS NOT FIRED WHEN <ENTER> IS PRESSED. Why is this? I read some documentation and did not find anything on this (There are other buttons on the form, but for all of them 'default' is set to false and keypreview for the form is false)
    Thank you in advanced.

  16. #16
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Disable Enter on command button

    Quote Originally Posted by lachungui2
    I solved it using the API 'GetAsyncKeyState', but I would like to know what I was doing wrong.
    If you solved it, then it would appear you did nothing wrong. Specifically, what are you trying to accomplish, and what code have you used so far in your attempt?

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