none
KeyPress is handled before KeyDown? RRS feed

  • Question

  • I thought that the KeyDown event was handled before the KeyPress event, but it looked like I was wrong.

    Can someone please confirm that KeyPress is handled before KeyDown?

    By the way, why the Delete key doesn't get  trapped in the KeyPress event (it does get trap in KeyUp/Down events though).

     

    Thanks.

    Friday, January 19, 2007 4:48 PM

Answers

  • Hi

    Under the covers KeyDown is analogous to the WM_KEYDOWN message, KeyUp is the WM_KEYUP message.  Key Press is a .net event that's fired after the key comes up.  The easiest way to figure this out is to over-ride WndProc in your form with a few Debug.WriteLine / Print statements to see what is happening.

    Something along these lines:

    protected override void WndProc(ref Message m)

    {

    switch (m.Msg)

    case WM_KEYUP:

    Debug.WriteLine("Key Up Message");

    break;

    case WM_KEYDOWN:

    etc.

    base.WndProc(ref m);

     

    To get the values for all the different messages I use this link:

    http://www.woodmann.com/fravia/sources/WINUSER.H

    As you don't get WINUSER.H unless you install C++.  WndProc gets hit first, then .net decides what event to fire depending on the message.  What .net does for you is convert the keyboard scan code that comes with the message into an actual key.

    HTH

     

     

    Friday, January 19, 2007 5:41 PM
  • Thanks Alan.
    Saturday, January 20, 2007 3:13 PM

All replies

  • Hi

    Under the covers KeyDown is analogous to the WM_KEYDOWN message, KeyUp is the WM_KEYUP message.  Key Press is a .net event that's fired after the key comes up.  The easiest way to figure this out is to over-ride WndProc in your form with a few Debug.WriteLine / Print statements to see what is happening.

    Something along these lines:

    protected override void WndProc(ref Message m)

    {

    switch (m.Msg)

    case WM_KEYUP:

    Debug.WriteLine("Key Up Message");

    break;

    case WM_KEYDOWN:

    etc.

    base.WndProc(ref m);

     

    To get the values for all the different messages I use this link:

    http://www.woodmann.com/fravia/sources/WINUSER.H

    As you don't get WINUSER.H unless you install C++.  WndProc gets hit first, then .net decides what event to fire depending on the message.  What .net does for you is convert the keyboard scan code that comes with the message into an actual key.

    HTH

     

     

    Friday, January 19, 2007 5:41 PM
  • Thanks Alan.
    Saturday, January 20, 2007 3:13 PM