Click here to Skip to main content
Click here to Skip to main content

Key Press Decoder

, 23 Dec 2009
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.

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.

License

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

Share

About the Author

cjbarth
Software Developer Wynright
United States United States
No Biography provided

Comments and Discussions

 
Generali voted 1 PinmemberMr.PoorEnglish28-Dec-09 4:11 
GeneralRe: i voted 1 Pinmembercjbarth28-Dec-09 4:48 
GeneralRe: i voted 1 PinmemberMr.PoorEnglish28-Dec-09 7:53 
GeneralMy vote of 1 Pinmemberssteo200912-Oct-09 15:02 
GeneralRe: My vote of 1 PinmemberJon_Boy14-Oct-09 4:10 
GeneralRe: My vote of 1 Pinmembercjbarth15-Oct-09 3:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.140827.1 | Last Updated 23 Dec 2009
Article Copyright 2009 by cjbarth
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid