[C#,.NET] Building a better key handler - Revisited

  • For those of you who grabbed the code in the previous post, you need an update.


    HERE'S WHAT HAPPENED:


    So I started to test the code and I found a few nasty bugs. Hotkeys mysteriously not firing, double commands after closing a dialog box.

    So I spent a few hours going over my design and I came to a realization:


    We need to use the KeyUp event as well as the KeyDown event. We need a way to signal the key handler code to ignore the next keydown / keyup events.

    But only when we are closing a dialog box whose close keystroke is the same as a Hot-key command.


    To start, just add this bool to the top of your form


    Csharp
    1. private bool ProcessingCmd = false;


    Now, here's the new Keydown Event code


    And now... the KeyUp event handler.



    Here's the KeyPress handler, just for completeness...


    Csharp: scintilla KeyPress event Handler
    1. private void TextEditor_KeyPress(object sender,KeyPressEventArgs e)
    2. {
    3. if (char.IsControl(e.KeyChar) == true)
    4. e.Handled = true;
    5. else if (blockCharPress == true)
    6. {
    7. e.Handled = true;
    8. blockCharPress = false;
    9. }
    10. }