user21767
user21767

Reputation: 95

Down Arrows doesn't fire KeyPress Event

Does anyone know why the KeyPress Event doesn't get fired when the user presses a down arrow? I have to look for it in the KeyDown event. Is this just something that I am doing wrong?

Upvotes: 4

Views: 1956

Answers (2)

Grant
Grant

Reputation: 12049

Use KeyDown instead

Public Class Form1
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        Debug.WriteLine(e.KeyData.ToString + " KeyDown")
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Debug.WriteLine(e.KeyChar.ToString + " KeyPress")
    End Sub
End Class

Upvotes: 1

Andy
Andy

Reputation: 30428

According to the documentation of the KeyPress event (assuming you are using WinForms):

The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events.

The down arrow key isn't a character key, so this event shouldn't be raised for it.

Upvotes: 14

Related Questions