Tom’s Blog

October 7, 2009

Creating a Simple Hotkey Component using RegisterHotKey

Filed under: .NET,C#,P/Invoke,Visual Basic — Tom Shelton @ 12:47 pm

A while ago, I needed to create a simple tray application that had to be able be brought into focus using a global hotkey.  I did a bit of research and came across the RegisterHotKey API function.  On my first attempt at using it, I created a simple class that I used to subclass any window handle that was passed into the constructor.  It  worked well – until you wanted to have more then one hotkey and it seemed to me it might be more user friendly as a component then as a class.  So, I rewrote the code as a windows forms component – and in the process I even created a custom type editor for the HotKey.Modifiers property.

Anyway, I’m posting two projects – the first is the HotKeyLib project that contains the Hotkey component.  The second is a project (both C# and VB.NET) that make use of the component.  You will need to compile the HotKeyLib project and then add it to your IDE’s toolbox to compile the second project.  The reason I did it this way was to show that the custom icon of the hotkey component will indeed show up in the IDE toolbox.

Anyway, hope this code proves helpful.

6 Comments »

  1. This is great, just what I was looking for, but I doesn’t seem to respond to media keys. Any suggestions?

    thanks
    -Darryl

    Comment by Darryl — April 10, 2010 @ 6:41 am

  2. Sorry Darryl, I haven’t ever tried to deal with the media keys. I’m not sure if RegisterHotKey does either :) If I get sometime, I will try and investigate that though.

    Comment by Tom Shelton — April 23, 2010 @ 10:28 am

  3. Hi Tom,

    I know how to check joystick button’s pressed status without directx, but winmm.dll. I wonder whether it is possible to hook a joystick button (a joystick virtual key registration?).

    I just want to get some code to run when one of joystick button is pressed WHILE application is inactive. Is it possible to register a joystick button as virtual key?

    Thanks!

    Comment by Onur — June 2, 2010 @ 2:02 am

  4. Onur – not sure if I know the answer to that or not. It maybe possible – since the RegisterHotKey function does take a virtual key code…. But, I don’t have a setup where I can really test that. I have no joystick :) Feel free to take the source code and experiment with that. And, if you feel inclined post back and let me know how that worked.

    Comment by Tom Shelton — June 5, 2010 @ 7:24 am

  5. Hi Tom, when i try to open the project with VB 2005 Express i get an error:

    “Unable to read the project file ‘VisualBasicExample.vbproj’.

    C:\Users\\Desktop\HotkeyUsage\VisualBasicExample\VisualBasicExample.vbproj(118,11): The imported project “C:\Microsoft.VisualBasic.targets” was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.”

    ?

    Comment by Onur — June 30, 2010 @ 5:32 am

  6. Hi Tom, when i try to open HotKeyUsage project with VB 2005 Express, i get an error:

    “Unable to read the project file ‘VisualBasicExample.vbproj’.

    C:\Users\\Desktop\HotkeyUsage\VisualBasicExample\VisualBasicExample.vbproj(118,11): The imported project “C:\Microsoft.VisualBasic.targets” was not found. Confirm that the path in the declaration is correct, and that the file exists on disk.”

    The reference Microsoft.VisualBasic.targets is required? It has a problem in solution.

    Comment by Onur — June 30, 2010 @ 5:33 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress