65.9K
CodeProject is changing. Read more.
Home

Key Press Decoder

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.20/5 (8 votes)

Oct 12, 2009

CPOL

3 min read

viewsIcon

28143

downloadIcon

797

Tool to convert a key press into a string corresponding to the key pressed. Will also convert multiple simultaneous keys pressed to a string.

Introduction

When working with key press events in .NET, often it is desired to translate those key press events into a string representing which keys have been pressed. There is no native way in .NET to do this. This DLL will accomplish it. It will also determine if the currently depressed keys comprise a valid hot-key sequence.

Using the Code

This program comprises a single module, and therefore doesn't need to be instantiated. The code is internally documented, and is pretty easy to follow.

Parameters

There are a few settings that can optionally be set to make sure that the string returned is one that is you want.

  • KeySeparator: This is the string that will go between the characters pressed. For example, if you press they keys Ctrl and E, you might want them to appear as Ctrl-E or as Ctrl+E (the default). Setting this parameter modifies what appears between the keys pressed. This can be a string of any length.
  • KeyWrapper: This is a string that is used to wrap the keys pressed. The first two characters of the string are used. For example: if the string entered is *(), then our above example would appear as *Ctrl)+*E). If we were to set this to (), then we would get (Ctrl)+(E).
  • ModifyKeySort: The keys are registered in the order they are pressed, but we commonly will want to display E Ctrl as Ctrl+E. In order to do this, the keys must be ordered. If you want to modify the order of the keys, set this parameter. For example, the Win key is ordered below the letter keys, so a key press of Win E would display as E+Win. If you want to move the order of the Win key up 100 places, simply use these commands: ModifyKeySort(Keys.LWin) = -100 and ModifyKeySort(Keys.RWin) = -100. Doing so will allow the aforementioned keystrokes to be rendered as Win+E.

Return Values

There are some properties and functions that return useful information.

  • ToString: This will simply return a string representing all the keys currently pressed.
  • HotKeyString: This will return a string of all the modifier keys that are currently pressed and the first non-modifier key currently pressed. This is for compatibility with a hot-key routine. I would recommend a library such as Managed Windows API. For a description of the hot-key functions they implement, see this. There is a sample implementation of this DLL and a Managed Windows API DLL in the Media Center Magnifier project.
  • Ctrl, Alt, Shift, Win: These return a Boolean showing if these keys are down.
  • PressedKey: Returns the first non-modifier key that was pressed (after the keys are sorted).
  • PressedKeys: Returns an integer array of all the keys currently pressed.

Implementation

In order for this code to work properly, there really are only two things that need to be done: a call to the DLL should be made in both the KeyDown and KeyUp events.

Private Sub frmExample_KeyDown(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    KeyPressDecoder.KeyDown(e)
End Sub
 
Private Sub frmExample_KeyUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    KeyPressDecoder.KeyUp(e)
End Sub

It is good to note that both KeyDown and KeyUp will return a String. KeyDown will return the key that was just pressed. KeyUp will return all the keys that remain pressed (the same as the ToString function).

Other Stuff to Note

There are some other functions to clean up the keys-down cache. Probably, most importantly, these have been written with integration with the Managed Windows API routines in mind. As such, they work very well to allow users to customize their hot-keys. A full-blown implementation is available with my other project on CodePlex called: Media Center Magnifier.

History

  • 2009-10-12: Initial release.