-
Trapping Enter key in VB 2005
Hi all,
I am trying to trap the Enter key in my form. My form contains a textbox and many buttons. The enter key will click whatever button was last selected by default.
I tried to use:
Code:
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Enter Then
btnEqual.PerformClick()
MsgBox("You've pressed the Enter key")
End If
End Sub
but it does not work. When i tried MyBase.KeyUp instead, it does catch it, and process the 2 statements, but only AFTER it has already processed the default keydown (button perform click) on whatever was selected.
I have done quite a bit of searching, and still can't figure it out. Any help would be appreciated!
-
Re: Trapping Enter key in VB 2005
Have you set the form's KeyPreview property to True?
-
Re: Trapping Enter key in VB 2005
yes i have. The keydown works for everything else, the numpad, etc. It just doesn't work for enter. I believe it is because the enter key is special, and windows treats it differently.
-
Re: Trapping Enter key in VB 2005
Just check the form property there is Accept button property set you command button name there.
-
Re: Trapping Enter key in VB 2005
hello, i have already tried that as well. I have set the AcceptButton in the form Properties to that button i wish for it to press, but still it does not work.
Hitting enter will click any button that was last clicked (highlighted).
Thanks for all your replies, any more suggestions?
-
Re: Trapping Enter key in VB 2005
What you need to do is to declare a class-level variable to hold the last clicked button. For example:
Code:
Private lastClickedButton As Button = Nothing
Then unset the form's acceptbutton property.
Now in each button click event handler, you set this variable to the clicked button.
Code:
lastClickedButton = DirectCast(sender, Button)
When you want to click the last button, you just call
Code:
If Not lastClickedButton Is Nothing Then
lastClickedButton.PerformClick()
End If
-
Re: Trapping Enter key in VB 2005
hi stanav,
I think you may have misunderstood me. From what i see, i think your solution is the opposite of my goal.
I want the Enter Key, to performclick on 1 button, and 1 button Only. (btnEqual).
But instead, the Enter Key will performclick on the last button clicked, which may be any other buttons that i have on that form. I do NOT want the Entery key to do this.
I hope that makes sense. If I have misunderstood you, please let me know. thanks again!
-
Re: Trapping Enter key in VB 2005
Quote:
Handles MyBase.KeyDown
change that to
Quote:
Handles Me.KeyDown
D
-
Re: Trapping Enter key in VB 2005
Quote:
Originally Posted by dminder
darn, it didn't work. The same thing happens.
any more ideas? I can't believe this isn't well documented. There must have been other people beside me with this problem right?
-
Re: Trapping Enter key in VB 2005
I have never had this issue. So long as the form I was working with had the keypreview option set - I have always been able to trap the enter key pretty much just as the code was entered there. Unfortunately, I am not sure what else to tell you. Hopefully somebody will have an answer/solution and will post it in case anybody does run into it.
Good Luck...
D
-
Re: Trapping Enter key in VB 2005
if the cursor is set inside the textbox, then the enter key will get trapped and function correctly.
However if the cursor is on a button, hitting enter will select this button.
I have started a new project. Made 2 buttons. then enter these codes
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("button 1")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox("button 2")
End Sub
Private Sub Form1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
Button2.PerformClick()
MsgBox("You've pressed the Enter key")
End If
End Sub
End Class
I have set KeyPreview to true, and even AcceptButton to Button2.
When running the program, i click on button1, it shows message. i click ok. Then hit "ENTER KEY" and it still clicks on button1.
would you mind trying it out real quick, and just copy and paste this code?
I would greatly appreciate it.
-
Re: Trapping Enter key in VB 2005
Quote:
Originally Posted by GODzillaSDM
hi stanav,
I think you may have misunderstood me. From what i see, i think your solution is the opposite of my goal.
I want the Enter Key, to performclick on 1 button, and 1 button Only. (btnEqual).
But instead, the Enter Key will performclick on the last button clicked, which may be any other buttons that i have on that form. I do NOT want the Entery key to do this.
I hope that makes sense. If I have misunderstood you, please let me know. thanks again!
From yor original post
Quote:
The enter key will click whatever button was last selected by default.
Is this your intention?
-
Re: Trapping Enter key in VB 2005
no, that is how it is acting now. But i do NOT want that.
I want the enter key to click 1 button. "btnEqual" every single time.
regardless of what was last clicked.
thanks.
-
Re: Trapping Enter key in VB 2005
I just did a bit more research and it looks like they really don't want you to do this. When a Button has focus and you press the Enter key, and specifically the Enter key, it looks like the there isn't even a Windows message passed to the WndProc method of either the form or the button that relates to the key being depressed. It must be being handled further up the chain. If this is possible you are going to have to get your hands fairly dirty and inherit either the Form class, the Button class or both, then get into their inner workings to find out where exactly that key is being handled. I don't pretend to know I'm afraid.
-
Re: Trapping Enter key in VB 2005
I see what you're talkking about now... Off the top of my head, I think you can workaround this problem by shifting the focus to the button you want... This is only a workaround and hopefully some others can find a better solution.
For example, in the button click event handler of Button1 in your example, you just do this:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("button 1")
DirectCast(Me.AcceptButton, Button).Focus()
End Sub
-
Re: Trapping Enter key in VB 2005
That's not going to work stanav, because then you couldn't click another button with the mouse either. I thought of trapping the Enter key somewhere and shifting focus but even using the WndProc method of the form and the Button I couldn't see any notification of the key being pressed until after the Click event has already been processed. I even tried returning False from the Button's IsInputKey for Enter but that didn't help. That Enter key is being trapped somewhere much farther up the chain that managed code doesn't have immediate access to.
-
Re: Trapping Enter key in VB 2005
Hmmm.. I had to trap the focus to a single button for the enter key myself
It didn't keep me from click with the mouse button on other controls.
Code:
Private Sub BTN_btnEqual_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_btnEqual.Leave
BTN_btnEqual.Focus()
End Sub
just to make sure, in the keydown event, did you use e.suppresskey after your statements to make sure it doesn't get handled again?
-
Re: Trapping Enter key in VB 2005
Quote:
Originally Posted by Froyd
Hmmm.. I had to trap the focus to a single button for the enter key myself
It didn't keep me from click with the mouse button on other controls.
Code:
Private Sub BTN_btnEqual_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_btnEqual.Leave
BTN_btnEqual.Focus()
End Sub
just to make sure, in the keydown event, did you use e.suppresskey after your statements to make sure it doesn't get handled again?
Then how can any other control, like a TextBox, ever receive focus? If you don't have any other controls that require focus then that's a hack that will work but it's not a general solution.
-
Re: Trapping Enter key in VB 2005
err. that's right, I don't have anything else except for a menustrip and a game panel. Think about e.suppresskey then
-
Re: Trapping Enter key in VB 2005
Quote:
Originally Posted by Froyd
err. that's right, I don't have anything else except for a menustrip and a game panel. Think about e.suppresskey then
e.SuppressKeyPress is only relevant in the KeyDown event handler, but we've established that pressing the Enter key when a Button has focus doesn't raise a KeyPress event.
-
Re: Trapping Enter key in VB 2005
i am a bit confused by the last few comments.
but your discussion has given me an idea to a possible workaround. I can try to make the btnEqual have focus as much as possible. This means, when a user clicks another button (which is acceptable), upon mouseclick up, i can have focus back onto btnEqual right?
So, whenever the user hits enter, it won't trap the enter key, but it will press the btnEqual which is what i want anyways.
You guys think that will work? Thanks for the discussion, and keeping this thread alive. if you guys have a working idea, please let me know.
-
Re: Trapping Enter key in VB 2005
actually, i just noticed and understood. that is what you are trying to do right stanav? i will give it a try.
thanks
*** edit ***
ok so it works. i can give the focus to btnEqual, everytime a user clicks on any other button.
but, if the user uses the arrow keys to highlight another button, it will stay on another button. Is there any way, to prevent the user from using the arrow keys to select another button?
Thanks!
-
Re: Trapping Enter key in VB 2005
Quote:
Originally Posted by GODzillaSDM
i am a bit confused by the last few comments.
but your discussion has given me an idea to a possible workaround. I can try to make the btnEqual have focus as much as possible. This means, when a user clicks another button (which is acceptable), upon mouseclick up, i can have focus back onto btnEqual right?
So, whenever the user hits enter, it won't trap the enter key, but it will press the btnEqual which is what i want anyways.
You guys think that will work? Thanks for the discussion, and keeping this thread alive. if you guys have a working idea, please let me know.
It's a bit of a hack but thinking on it further you can make it work. Rather than handling the Leave event of the Button you want to remain focused you should handle the Enter event of all the other Buttons. That way you can still have other controls, like TextBoxes, receive focus. You just need one method to handle the Enter event of all the other Buttons. In it you should call the Select method of the Button you want to keep focus.
-
Re: Trapping Enter key in VB 2005
Quote:
Originally Posted by Froyd
Hmmm.. I had to trap the focus to a single button for the enter key myself
It didn't keep me from click with the mouse button on other controls.
Code:
Private Sub BTN_btnEqual_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTN_btnEqual.Leave
BTN_btnEqual.Focus()
End Sub
just to make sure, in the keydown event, did you use e.suppresskey after your statements to make sure it doesn't get handled again?
hey froyd, is that question for me or stanav?
-
Re: Trapping Enter key in VB 2005
conclusion: So i pretty much have the enter key resolved. By using the work around method
i am able to get the focus to stay on that button. So hitting enter will click on it. And when the focus is in the textbox, then it will still work, because the trap handling will work in there.
So, i still have a small problem. And that is, if the user uses the arrow key, to navigate throughout the other available buttons, the focus will not stay on the btnEqual.
Any ideas on how to disable the arrow keys? I'll try to trap them and see if it works. Thanks again guys!
(if someone out there knows the official way to trap the enter key, i would still love to hear it.)
-
Re: Trapping Enter key in VB 2005
yah, looks like the arrow keys don't get trap in the form either (only in textbox)
-
Re: Trapping Enter key in VB 2005
In the keypress event of the object put
vb Code:
If nonNumberEntered Then e.Handled = (True)
and for the keydown event .
vb Code:
nonNumberEntered = (False)
If e.KeyCode <> Windows.Forms.Keys.Back Then nonNumberEntered=True
This example handles a backspace. In your case i think you want
vb Code:
If e.KeyCode <> Windows.Forms.Keys.right Then nonNumberEntered=True
If e.KeyCode <> Windows.Forms.Keys.left Then nonNumberEntered=True
If e.KeyCode <> Windows.Forms.Keys.up Then nonNumberEntered=True
If e.KeyCode <> Windows.Forms.Keys.down Then nonNumberEntered=True
-
Re: Trapping Enter key in VB 2005
oh and the enter key would be
vb Code:
If e.KeyCode <> Windows.Forms.Keys.enter Then
nonNumberEntered=True
something.focus()
end if
-
Re: Trapping Enter key in VB 2005
And by the way, set the keypreview property for the form to true, so the controls on the form register key events with the form.
-
Re: Trapping Enter key in VB 2005
Had you taken my advice from post #23 this latest problem in post #25 wouldn't have arisen. As I said, if you handle the Enter event of the other Buttons rather than the Leave event of the Button you want to retain focus then there's no issue because if the user tries to use the arrow keys to get to another Button focus will still shift back to the one you want. Also, as the documentation says, call the Button's Select method, not Focus.
-
Re: Trapping Enter key in VB 2005
Quote:
Originally Posted by jmcilhinney
Had you taken my advice from post #23 this latest problem in post #25 wouldn't have arisen. As I said, if you handle the Enter event of the other Buttons rather than the Leave event of the Button you want to retain focus then there's no issue because if the user tries to use the arrow keys to get to another Button focus will still shift back to the one you want. Also, as the documentation says, call the Button's Select method, not Focus.
oh i c what you are saying. Set the event handling on when button is leaving focus. It will always come back.
I'll have to try to understand the button's select method tho, instead of focus.
And thank you for your post wrecklesswun. I haven't given that a try yet, but i will do so. I'll probably have questions, because i am not sure if i understand completely.
thanks again guys! the help was much appreciated!
-
Re: Trapping Enter key in VB 2005
The idea is not to prevent the one button from losing focus. It's to prevent the other buttons receiving it. Therefore you should logically be handling the Enter events of the other buttons, which is the event that is raised when they receive focus.
This is from the MSDN documentation for the Control.Focus method:
Quote:
Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms.
-
Re: Trapping Enter key in VB 2005
Quote:
Originally Posted by jmcilhinney
Had you taken my advice from post #23 this latest problem in post #25 wouldn't have arisen. As I said, if you handle the Enter event of the other Buttons rather than the Leave event of the Button you want to retain focus then there's no issue because if the user tries to use the arrow keys to get to another Button focus will still shift back to the one you want. Also, as the documentation says, call the Button's Select method, not Focus.
actually, i didn't try to handle the Leave event of 'btnEqual'. I was handling the button.click of each of the OTHER buttons.
But as you said, handling the gotfocus is better, since it will handle the click and arrow focusing.
I am still using btnEqual.focus() tho, and it works. I don't see any Select methods.
And i will still get around to wreckless' advice soon. (ur post hasn't been wasted)
and thanks again for all your help everyone!
-
Re: Trapping Enter key in VB 2005
I didn't say handle GotFocus. I said handle Enter. You are not supposed to use the GotFocus event except in the very specific circumstances detailed in the documentation. As for the Select method, I don't understand how you can not see it. Just type ".Select" instead of ".Focus".
-
Re: Trapping Enter key in VB 2005
jmcilhinney,
forgive my ignorance. It must have been very frustrating with all this misunderstanding. But i finally understand you, and it works great!
but one thing i don't understand his, how come Handles Enter works for arrow keys too? Arrow keys changes focus, but doesn't click or press enter. Why would it call on that event handler?
thanks
-
Re: Trapping Enter key in VB 2005
I'm not sure I understand your question completely, but the Enter event is raised whenever the control receives focus, no matter how that occurs. If you handle the Enter event of all your other Buttons and Select the one Button then any time any other Button receives focus, whether by clicking or using the Tab or arrow keys, then the one Button will be reselected. That doesn't stop the other Buttons being clicked but it does prevent focus lingering on them so they can never have focus when you press the Enter key.
-
Re: Trapping Enter key in VB 2005
ok, handle Enter event actually handles "when button receive focus" gotcha. kinda weird description to me tho. but good to know. thanks again.
-
Re: Trapping Enter key in VB 2005
"Enter" and "Leave" are used because there are also "GotFocus" and "LostFocus" events already, which work a little differently and exist for very specific circumstances. The names basically refer to the system caret, which defines focus. They are raised when the system caret enters and leaves the control.