.net – How to create compound keyboard shortcuts in a Windows Forms application

keyboard shortcutsnetwinforms

I want to create a component that allows us to have compound keyboard shortcuts associated with an arbitrary command, like the Visual Studio IDE and Microsoft Office do.

That is, keyboard shortcuts consisting of a sequence of multiple keystrokes, such as Ctrl + W + C. In Visual Studio this opens the class-view. When the first set of the keys is pressed (Ctrl + W) the message "(Ctrl + W) was pressed. Waiting for the second key of the chord…" appears in the status bar.

Best Answer

In answer to the question of keyboard chords specifically, I do not believe there is a ready-made option available to you at this point.

However, it should be simple enough to model. I would create a single class, perhaps KeyboardChordProvider. It will need to know about keyboard events at the form level. As stated elsewhere, the Form.KeyPreview property must be true. It may be enough for this provider to subscribe to the Form.KeyPress event. You could do all of this in the provider's constructor if you passed in the form.

You would need to register the potential keystrokes with the provider.

Internally this instance will track the current state. Whenever a keystroke is observed that represents the first key of the chord, you would update the provider's state and raise an event so that a subscriber could set text: (CTRL+W) was pressed. Waiting for second key of chord...

If the next keystroke matches a potential secondary option, then you have a match and could raise a ChordPressed event containing the details of the strokes entered. Alternatively, you might just call a particular callback that was given to the provider at the time the chord was registered (to avoid having a switch statement or some other dispatch in the ChordPressed event handler).

If, at any time, a keystroke does not match a potential next option, then you would reset the state of the provider.

Internal to the provider you might model the possible keystrokes using a tree structure. The current state of the provider is just a particular tree node. At the beginning, the root node would be active. If a child matches a keystroke then it becomes the current node in anticipation of the next stroke. If the child was a leaf node, then an entire chord has matched and you would raise ChordPressed event (passing the chain of strokes that got you to that point) or invoke the callback stored in the leaf. Whenever no keystroke matches a child, reset back to making the root node active.

I think this design would achieve what you want.

Related Topic