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

  • Hi All,

    One very nice feature in an application is the ability to respond to keyboard chords. An example of the use of this technique is Visual Studio.


    To toggle regions , you would press <Ctrl+M>,'M'.


    In this post, I'm going to share a (fairly) simple technique to do exactly this. My approach lends itself to being able to be loaded from / saved to an XML file.


    So let's create a list of hotkeys. Let's first start with the definition of a hotkey. For simplicity's sake, I only allowed the second part of the key chord to be a character without a control modifier.

    We'll start with an object that encapsulates a KeyCode, Modifier pair.

    As you can see, the KeyCode and Modifier properties are read only, being set when the object is created.


    Now, let's create a HotKey object.


    Something worth mentioning is the HasCond property. I use it when close program is called,

    because I allow the program to be closed with the <Esc> key.


    If you compile this, you'll get a compiler error. That's because you need to create an enumeration that describes all of the commands that you wish to associate to a hotkey.


    This is what my enum looks like. I'm currently building an editor that uses the ScintillaNET control. Of course, you'll have to add your own definitions.


    Csharp: MJEditorCmds enum
    1. public enum MJEditorCmds {
    2. ecEmpty = 0,ecGotoBookmark0 = 1,ecGotoBookmark1 = 2,ecGotoBookmark2 = 3,ecGotoBookmark3 = 4,ecGotoBookmark4 = 5,
    3. ecGotoBookmark5 = 6,ecGotoBookmark6 = 7,ecGotoBookmark7 = 8, ecGotoBookmark8 = 9,ecGotoBookmark9 = 10,
    4. ecToggleBookmark0 = 11,ecToggleBookmark1 = 12,ecToggleBookmark2 = 13,ecToggleBookmark3 = 14,ecToggleBookmark4 = 15,
    5. ecToggleBookmark5 = 16,ecToggleBookmark6 = 17,ecToggleBookmark7 = 18,ecToggleBookmark8 = 19,ecToggleBookmark9 = 20,
    6. ecToggleBraceMatching = 21,ecToggleParenMatching = 22,ecFileNew = 23,ecFileOpen = 24,ecFileSave = 25,ecFileSaveAs = 26,
    7. ecExitProgram = 27,ecRedo = 28,ecUndo = 29,ecCut = 30,ecCopy = 31,ecPaste = 32,ecFindFirst = 33,ecReplaceFirst = 34,
    8. ecFindReplaceNext = 35,ecInvokeSettings = 36
    9. }


    Now, let's create a hotkey list. Here's my original code. Take a look at the first two definition. The first is a private declaration of our hotkey list.

    The second is a public indexer property. Quite useful if you need to access the object as an array. In this case, it's readonly, but you could easily

    add an on changed event and create a property writer. I'll demonstrate that technique in a post about the color combo box.



    Okay. Now, we have a list of hotkeys. To use them, we need to override both the keydown and keypress event of our form.

    We also need to define a subroutine that will process the Editor command and execute the function, and a bool that will be used to suppress the

    extra keystroke produced when the chord (second) key is pressed.


    Here's the code to do that. I have made significant changes to the way I handle the key events.

    You can find them in this post.


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


    Here are the menu events from my form main menu.


    The post was edited 1 time, last by Michaela Joy: The Definition of IsSecondaryHotkey has been changed. ().