What's the purpose of 'PreviewKeyDown ' event?

What's the purpose of new 'PreviewKeyDown' event The doc doesn't say much.

Is the only difference that it occurs before the 'KeyDown' event If everyone start using this instead, won't it simply replace 'KeyDown'

When should I use one or the other




Answer this question

What's the purpose of 'PreviewKeyDown ' event?

  • Aaron McKee

    I found this excellent answer:

    http://blogs.msdn.com/jfoscoding/archive/2006/01/26/518181.aspx

    "If you've struggled before with having to override a class in order to set IsInputKey=true so you can get arrow keys for your control, you can now also use the PreviewKeyDown event."

    That's apperently what's is for.



  • Frode Sorhoy

    Thanks for the answer, but can't I do exactly the same in 'KeyDown' So I get two equal events fired after one another, right

    Or, just a thought, Do 'PreviewKeyDown' get fired before the control internally processes the message and 'KeyDown' after the control's internal processing Is that the difference



  • blackpanter

    The events aren't equal. The Preview event is fired before KeyDown and the Preview event gives you only a preview and in the KeyDown event you can specify if the Key is handled or not.


  • LoinHrat

    Now I'm really confused. First you say (about PreviewKeyDown):

    PJ. van de Sande wrote:
    .....you can set the Handled flag.

    then about KeyDown:

    PJ. van de Sande wrote:
    ... in the KeyDown event you can specify if the Key is handled or not.


    That looks very much alike to me!



  • rico81

    This event is fired before the KeyDown event is fired. It only gives your an preview and you can't set the Handled flag.

    Use this event for logging logic, shortcut logic because this will be allways fired and can't be stopped by setting a Handled flag.


  • Jason Poll

    Sorry, mistyped it.

    PreviewKeyDown
    • Fired before KeyDown event.
    • Doesn't provide you the functionality to mark it as handled.
    KeyDown
    • Fired after PreviewKeyDown event.
    • Prodive functionality to mark key as handled.


  • What's the purpose of 'PreviewKeyDown ' event?