When using the Form1.KeyDown, keystrokes don't seem to register.

For example, the Click works fine:

Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        MsgBox("Hello")
    End Sub

That will display the message box when the form is clicked, but if you change it to:

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        MsgBox("Hello")
    End Sub

I can't seem to get the message box to appear.

Recommended Answers

All 2 Replies

Hmn. The code looks right. I would try to change it from handling mybase though, to handling the form in question. I'm not sure if that will make a big difference or not, since I don't use .NET, but it would seem to me that the sub should handle keypress event of the form's name.

if you want it to work you will have to put the "keypreview" property of Form1 to true. This will make keyboard events be registered on the form.

In VB6 this same property made the form's keyboard events take priority from the other controls' keyboard events. It seems to have changed

saludos

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.