Click here to Skip to main content
12,697,340 members (25,666 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


20 bookmarked

Key Press Decoder

, 23 Dec 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
Tool to convert a key press into a string corresponding to the key pressed. Will also convert multiple simultaneous keys pressed to a string.


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.


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.


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
End Sub
Private Sub frmExample_KeyUp(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
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.


  • 2009-10-12: Initial release.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Wynright
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generali voted 1 Pin
Mr.PoorEnglish28-Dec-09 5:11
memberMr.PoorEnglish28-Dec-09 5:11 
GeneralRe: i voted 1 Pin
cjbarth28-Dec-09 5:48
membercjbarth28-Dec-09 5:48 
GeneralRe: i voted 1 Pin
Mr.PoorEnglish28-Dec-09 8:53
memberMr.PoorEnglish28-Dec-09 8:53 
GeneralMy vote of 1 Pin
ssteo200912-Oct-09 16:02
memberssteo200912-Oct-09 16:02 
GeneralRe: My vote of 1 Pin
Jon_Boy14-Oct-09 5:10
memberJon_Boy14-Oct-09 5:10 
GeneralRe: My vote of 1 Pin
cjbarth15-Oct-09 4:47
membercjbarth15-Oct-09 4:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 23 Dec 2009
Article Copyright 2009 by cjbarth
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid