Sunday, March 27, 2011

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. I was just wondering if this was something that I was doing wrong or not?

From stackoverflow
  • 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.

  • 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
    

0 comments:

Post a Comment