Microsoft KB Archive/937129

= The KeyDown event may invoke multiple handlers for the same key press in Windows Presentation Foundation =

Article ID: 937129

Article Last Modified on 6/28/2007

-

APPLIES TO


 * Microsoft .NET Framework 3.0

-



SYMPTOMS
In Microsoft Windows Presentation Foundation (WPF), the KeyDown event may invoke multiple handlers for the same key press in rare circumstances.



CAUSE
This problem occurs when a KeyDown event is handled by both the WPF message pump and a WPF window procedure. Under most conditions, WPF handles keyboard event messages in the following ways:
 * From the message pump, WPF calls through the ComponentDispatcher class to enable the message to be processed. Top-level WPF windows register with the ComponentDispatcher class and integrate the message processing through the IKeyboardInputSink interface. If the message is handled, the message pump is instructed not to dispatch the message.
 * If the message was not handled in the previous process, the message pump will dispatch the message to the appropriate window’s message procedure.

Top-level WPF windows receive messages from either the message pump or from their message procedures. Because WPF integrates with a variety of environments, it is not guaranteed that the message pump will call through the ComponentDispatcher class. To support these scenarios, top-level WPF windows record the last window message that they receive from the message pump. Top-level WPF windows discard it if the same message is sent to the window procedure. If a message is only received through the window procedure, the message pump is assumed to be uncooperative, and the message is processed as usual.

Keyboard messages are converted into equivalent WPF keyboard events. There is a scenario when the event causes a nested message pump. However, this event is not marked as handled. After the nested message pump returns, the original message is dispatched to the window procedure. Because of the arbitrary number of messages that are processed by the nested message pump, the WPF window procedure may not recognize this as a message that it has already processed. Therefore, the WPF window procedure processes the message again. This leads to the same message and causes two KeyDown events.



RESOLUTION
To resolve this problem, mark KeyDown events as handled when you use a nested message pump.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



MORE INFORMATION
For more information about how to use WPF, visit the following Microsoft Web site:

http://windowsclient.net/Default.aspx

Keywords: kbprb kbtshoot kbexpertiseadvanced kbbug KB937129

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.