Turn OFF Enter Key on CommandButton [RESOLVED]
I have users who like to use 'enter' rather 'tab' to register data and move to the next field. Have this working for all controls except the CommandButton. How might I turn off the 'enter' key from executing on a CommandButton???????? It would appear that simply intercepting the enter key and then sendkeys "{TAB}" does not work. Any suggestions??????
Re: Turn OFF Enter Key on CommandButton
Quote:
Originally Posted by jbonner
I have users who like to use 'enter' rather 'tab' to register data and move to the next field. Have this working for all controls except the CommandButton. How might I turn off the 'enter' key from executing on a CommandButton???????? It would appear that simply intercepting the enter key and then sendkeys "{TAB}" does not work. Any suggestions??????
I believe setting the DEFAULT property to FALSE will stop ENTER from taking the command button.
If not, you need to catch the ASCII value 13 in KEYPRESS and change it to 0 - that should suppress it.
Re: Turn OFF Enter Key on CommandButton
Thanks for the reply.
vbDefault=false on all CommandButton
keyascii = 0 on 'enter' intercept
This does not work.
Re: Turn OFF Enter Key on CommandButton
you could try:
VB Code:
Private Sub command1_keypress(Keyascii as integer)
if keyascii = 30 then ' i think its 30
then keyascii = 0
end sub
Re: Turn OFF Enter Key on CommandButton
Quote:
Originally Posted by |2eM!x
you could try:
VB Code:
Private Sub command1_keypress(Keyascii as integer)
if keyascii = 30 then ' i think its 30
then keyascii = 0
end sub
None of these will work. The _Click event fires regardless of what you do. Place a breakpoint
at the start of each of those procedures and you will see that they wont fire from an enter
key being presses, even with Form1.KeyPreview set to True.
Now if the command button does not have the focus and the Default is False
then thats another story.
If you want you can just have the command button never be able to recieve
the focus by setting the TabStop property ot False.
You may have to end up sub-classing the command button and handling the
enter keypress in order to cancel it out if the button in question has the focus.
Re: Turn OFF Enter Key on CommandButton
RobDog88:
Neither 'enter' or 'tab' get to to the key events.
I had just verified that when I got your reply.
I not much good at subclassing.
Could you point me to an example.
TIA
Re: Turn OFF Enter Key on CommandButton
this isnt that simple as i though it would lol
rob said subclass...and even I though subclassing would do it. ok i subclassed cmd button watched for wm_keydown and wparam to be vk_return and nothing. This worked on all other controls exept cmdbuttons :/ why...i dont know...
anyways im thinking of another way to do this...is interesting "challenge" :p
Re: Turn OFF Enter Key on CommandButton
Yes, this one is a little harder then it sounds. Basically you want to disable the click event if invoked
by a enter keypress, but enabled for a mouse click?
I set up a small test project subclassing the cmd button and I tried canceling
a few messages but I havent got the right one yet. I may have more time on
this tomorrow.
Re: Turn OFF Enter Key on CommandButton
prepare to slap foreheads..
VB Code:
Private Sub Command1_Click()
'do nothing
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'code here
End Sub
Re: Turn OFF Enter Key on CommandButton
Re: Turn OFF Enter Key on CommandButton
Quote:
Originally Posted by dis1411
prepare to slap foreheads..
VB Code:
Private Sub Command1_Click()
'do nothing
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'code here
End Sub
No slap since the poster wants the command button to function as usual unless its a keyboard enter press.
I know it sounds triavial but there may be consequenses it there are events
taken out of the click event. Order of other events fireing based on the click,
but I say this should work unless we hear otherwise.
Ok, how about a half of a slap? *Sl... :lol:
Re: Turn OFF Enter Key on CommandButton
Re: Turn OFF Enter Key on CommandButton
Here's a slight variation on dis1411's suggestion. A bit long-winded perhaps, but it works:
VB Code:
Dim MouseClicked As Boolean
Private Sub Command1_Click()
If Not MouseClicked Then ' Assume the user pressed enter
MsgBox "Enter"
Exit Sub
Else
'
' Do your button-click stuff here
'
MsgBox "Click"
MouseClicked = False
End If
End Sub
Private Sub Command1_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 Command1_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
Re: Turn OFF Enter Key on CommandButton
even better. the other half slap
Re: Turn OFF Enter Key on CommandButton
yeah, the part i was debating over weather or not to do :p
i figured if i got the ball rolling someone would feel like finishing it
Re: Turn OFF Enter Key on CommandButton
Yet again, thanks for all the help.
I will accept the forehead slap, Duhhhhhhhhhhhhhhhhhhh.
Jim B
Re: Turn OFF Enter Key on CommandButton [RESOLVED]
Nice workaround guys :thumb: I will not forget this if I ever need something like this in one of my projects. :thumb:
Ok, I'll take my full *Slap* now :lol:
Re: Turn OFF Enter Key on CommandButton [RESOLVED]