Trap Return key

  • Thread starter Thread starter Z.K.
  • Start date Start date
Z

Z.K.

I have a problem trapping the return key. I thought if I added the
forms keydown event handler that this would do what I want. It did not
even go into the keydown event handler when I pressed return. I have a
form and three buttons. I assigned one button to the cancel button and
another to the accept button property. But the right button rarely gets
activated unless it just happens to have focus. So, I thought maybe I
need to trap the Return key with the keydown handler of the form, but
this did not do a thing. I even added keydown handlers for the buttons
themselves, but that did not do anything either. So, just how do I trap
the return key for when the return key is pressed no matter what control
might have focus?

Z.K.
 
First set the keypreview property of the form to true ( standard = false )

Now you can trap all the keys pressed on form level on the event handlers
asociated to the keys

HTH

Michel
 
ZK,

For the button there is only one event and that is the click.

On the other hand, why are you using the key down, the information comes as
the key is going up.

Cor
 
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

If e.KeyData = Keys.Enter Then

e.SuppressKeyPress = True

MsgBox("hi")

End If

End Sub

In this case the key up event isn`t even raised ( unless you hold the enter
key )
 
Back
Top