|
-
Nov 25th, 2005, 07:54 AM
#1
Thread Starter
Member
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:
Private Sub cmdStart_Click()
Timer1.Interval = 1000
txttimer.Text = "10"
cmdClicker.Visible = True
cmdStart.Visible = False
End Sub
Private Sub Form_Load()
txtcounter.Text = 0
cmdClicker.Visible = False
cmdNext.Visible = False
End Sub
Private Sub Timer1_Timer()
If txttimer.Text = 0 Then
Timer1.Enabled = False
cmdClicker.Visible = False
cmdNext.Visible = True
Else
txttimer.Text = txttimer.Text - 1
End If
End Sub
Private Sub cmdclicker_click()
txtcounter.Text = txtcounter.Text + 1
End Sub
Heres what should work using the [RESOLVED] code from pnish:
VB Code:
Dim MouseClicked As Boolean
Private Sub cmdclicker_Click()
If Not MouseClicked Then ' Assume the user pressed enter
MsgBox ("cheat")
Exit Sub
Else
'
' Do your button-click stuff here
'
txtcounter.Text = txtcounter.Text + 1
MouseClicked = False
End If
End Sub
Private Sub Cmdclicker_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'
' Flag that the mouse button has been pressed
' (May want to check for left or right button etc)
'
MouseClicked = True
End Sub
Private Sub cmdclicker_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'
' Flag that the mouse button has been released
' (May want to check for left or right button etc)
'
If MouseClicked = True Then
MouseClicked = False
End If
End Sub
Private Sub cmdStart_Click()
Timer1.Interval = 1000
txttimer.Text = "10"
cmdClicker.Visible = True
cmdStart.Visible = False
End Sub
Private Sub Form_Load()
txtcounter.Text = 0
cmdClicker.Visible = False
cmdNext.Visible = False
End Sub
Private Sub Timer1_Timer()
If txttimer.Text = 0 Then
Timer1.Enabled = False
cmdClicker.Visible = False
cmdNext.Visible = True
Else
txttimer.Text = txttimer.Text - 1
End If
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!
-
Nov 25th, 2005, 08:03 AM
#2
Fanatic Member
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
-
Nov 25th, 2005, 08:30 AM
#3
Thread Starter
Member
Re: Disable Enter on command button
hi there!
wow, thanks for the quick reply.
is this what you meant?
VB Code:
Private Sub cmdclicker_Click()
If KeyDown = True Then
MsgBox ("cheat")
Else
txtcounter.Text = txtcounter.Text + 1
End If
End Sub
Private Sub cmdClicker_KeyDown(KeyCode As Integer, Shift As Integer)
KeyDown = True
End Sub
Private Sub cmdStart_Click()
Timer1.Interval = 1000
txttimer.Text = "10"
cmdClicker.Visible = True
cmdStart.Visible = False
End Sub
Private Sub Form_Load()
txtcounter.Text = 0
cmdClicker.Visible = False
cmdNext.Visible = False
End Sub
Private Sub Timer1_Timer()
If txttimer.Text = 0 Then
Timer1.Enabled = False
cmdClicker.Visible = False
cmdNext.Visible = True
Else
txttimer.Text = txttimer.Text - 1
End If
End Sub
becuase it still allowing enter if thats what you mean, i dont get any messagebox after pressing enter either.
thanks again!
Matt
-
Nov 25th, 2005, 08:34 AM
#4
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:
Private Sub Command1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeySpace Then
KeyCode = 0
End If
End Sub
-
Nov 25th, 2005, 08:40 AM
#5
Fanatic Member
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
-
Nov 25th, 2005, 08:41 AM
#6
Thread Starter
Member
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
-
Nov 25th, 2005, 08:42 AM
#7
Fanatic Member
Re: Disable Enter on command button
 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
-
Nov 25th, 2005, 08:45 AM
#8
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?
-
Nov 25th, 2005, 08:52 AM
#9
Thread Starter
Member
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
-
Nov 25th, 2005, 08:52 AM
#10
Re: Disable Enter on command button
Look at this..
VB Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Command1_Click()
If GetAsyncKeyState(13) Or GetAsyncKeyState(vbKeySpace) Then
Exit Sub '(Don't compute this click)
End If
MsgBox "Clicked!" 'Compute this click!
End Sub
-
Nov 25th, 2005, 08:53 AM
#11
Fanatic Member
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
-
Nov 25th, 2005, 08:55 AM
#12
Fanatic Member
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:
If GetAsyncKeyState(13) Or GetAsyncKeyState(vbKeySpace) Then
Exit Sub '(Don't compute this click)
End If
txtCounter = txtCounter + 1
You can also remove the KeyDown event code, and the KeyDown boolean
r0ach™
Don't forget to rate the post
-
Nov 25th, 2005, 09:03 AM
#13
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:
Private Sub Command1_Click()
If GetAsyncKeyState(13) Or GetAsyncKeyState(vbKeySpace) Then
Exit Sub
'(Don't compute this click)
End If
Debug.Print "pressed"
End Sub
(the debug window is not devastated)
-
Nov 25th, 2005, 09:32 AM
#14
Thread Starter
Member
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
-
Dec 13th, 2005, 03:58 AM
#15
New Member
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.
-
Dec 13th, 2005, 07:21 AM
#16
Re: Disable Enter on command button
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|