Manoj Chamikara wrote:
can you more explain please
The keyboard event handler can deal with one press at a time (only state keys like Alt, Ctrl or Shift are automatically handled and their states are available to your handler in its event argument) and is stateless.
You have to add the state to it. Say, you can add an instance member of your form or another control class to have a container of previously pressed keys. I would suggest simply using
System.Text.StringBuilder
. In your cases, you probably only need to handle the event
KeyPress
. The handler should take a character and add it to the container, each time trying to recognize the whole sequence ("aer", "aa", etc). As soon as you recognize something, clean up the character container and do corresponding action. For actions container, you can use
System.Collection.Generic.Dictinary<string, System.Action>
, where the string key is the "command", like "aa", and the value parameter is the delegate instance used to handle each "command". It can be more complex, abbreviated "commands", etc.
Important: you will need to clear up the character container by some other criteria, or make a cyclic buffer, otherwise non-recognized characters can occupy too much memory eventually. I suggest that you always clean the container when it has the number of characters equal to your longest command: if the command is recognized, its no longer needed, if not, all characters can be considered useless and you start over. You get create some smarter algorithm.
Please don't hesitate to ask your follow-up question if something is not clear. Your question is quite reasonable, so I'll gladly help you.
[EDIT]
For associations between data and actions, please see my article:
Dynamic Method Dispatcher[
^].
—SA