Search code examples
c#.netwinformskeyevent

A Form's key events go missing


I have a bug in my application that I don't know how to how to attack. Basically, key events "go missing" after some time. I have added event handlers to KeyDown, KeyPressed and KeyUp that write to the console (both for the Form and one of its TextBoxes). Here's the correct order of things:

control_PreviewKeyDown
ShellForm_KeyDown
control_KeyDown
ShellForm_KeyPress
control_KeyPress
ShellForm_KeyUp
control_KeyUp

In the failed state (which I can't reproduce consistently), I get these events when pressing a key (TextBox has focus):

ShellForm_KeyUp
control_KeyUp

Form.KeyPreview is true.

It's the same for all TextBoxes in the Form; they are not disabled and not set to readonly, but it's no longer possible to write text.

Problem is, I don't really know where to begin.


Solution

  • Begin with Application.AddMessageFilter(...) just to intercept the flow of events. You can find some ideas here.