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

    Question

  • 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?

    Thursday, April 06, 2006 7:05 AM

Answers

All replies

  • 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.
    Thursday, April 06, 2006 7:36 AM
  • 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?

     

    Thursday, April 06, 2006 12:06 PM
  • 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.
    Thursday, April 06, 2006 12:35 PM
  • 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!

    Thursday, April 06, 2006 6:37 PM
  • 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.
    Friday, April 07, 2006 11:35 AM
  • 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.

    Saturday, April 08, 2006 12:35 AM