Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Down Arrows doesn't fire KeyPress Event

Tags:

.net

winforms

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?

like image 820
user21767 Avatar asked Dec 07 '22 08:12

user21767


2 Answers

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.

like image 130
Andy Avatar answered Dec 09 '22 20:12

Andy


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
like image 31
Grant Avatar answered Dec 09 '22 22:12

Grant