Click here to Skip to main content
15,884,425 members
Articles / Programming Languages / Visual Basic

A HotkeyManager Class

Rate me:
Please Sign up or sign in to vote.
4.82/5 (13 votes)
5 Aug 2008CPOL1 min read 43.2K   1.2K   45   7
The HotkeyManager class makes it easy to set global hotkeys for applications.

Introduction

The HotkeyManager class makes it easy to set global hotkeys for applications. It is a wrapper class that raises a HotkeyPressed event whenever a registered hotkey by the HotkeyManager class is pressed. The class keeps a collection of the registered hotkeys (in a Hotkey data type), and it is available to the developer through the Hotkeys property of the HotkeyMananger object. The HotkeyManager can be used to register, unregister, or replace (registered by the HotkeyManager) hotkeys.

Hotkeys/Hotkey.png

Background

To use (create) a HotkeyManager object, you should pass a valid window (Form) within the project as an argument to its constructor method. The HotkeyManager uses two (RegisterHotKey and UnregisterHotKey) API methods to register, unregister, and replace hotkeys. All the methods of HotkeyManager take an argument of type Hotkey.

On the other hand, the Hotkey structure has very useful methods too, such as ToString which returns the string representation of the hotkey (example: Ctrl+H) and many properties (as System.Windows.Forms.Keys).

Using the code

Here is an example of how to use the HotkeyManager class:

VB
Public Class Form1

    'Declaration of HotkeyManager object.
    Dim WithEvents hkM As HotkeyManager

    Private Sub Form1_Load(ByVal sender As Object, _
                ByVal e As System.EventArgs) Handles Me.Load
        'Creat a new instance of HotkeyManager
        'objec and pass this form as its argument.
        Me.hkM = New HotkeyManager(Me)
    End Sub

    Private Sub RegisterButton_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles RegisterButton.Click
        Try
            'Create a Hotkey object with its Id = 100 and value = Alt+G.
            Dim hk As New Hotkey(100, Keys.Alt Or Keys.G)
            'Register the Alt+G.
            Me.hkM.RegisterHotKey(hk, True)
        Catch ex As Exception
            'An exception is throw, show the exception message.
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.FormClosingEventArgs) _
            Handles Me.FormClosing
        If Me.hkM IsNot Nothing Then
            'Dispose the HotkeyManager objec when the application is exiting.
            'This method will unregister all hotkeys for this HotkeyManager.
            Me.hkM.Dispose()
        End If
    End Sub

    Private Sub ReplaceButton_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles ReplaceButton.Click
        Try
            'Create a Hotkey object with its Id = 100 and value = Shift+B
            Dim hk As New Hotkey(100, Keys.Shift Or Keys.B)
            'Replace previously registered hotkey 
            'with the Id = 100 (Alt+G) with Shift+B.
            Me.hkM.Replace(hk, True)
        Catch ex As Exception
            'An exception is throw, show the exception message.
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub UnregisterButton_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles UnregisterButton.Click
        Try
            'Unregister previously registered hotkey with the Id = 100.
            Me.hkM.UnregisterHotKey(100, True)
        Catch ex As Exception
            'An exception is throw, show the exception message.
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub hk_HotkeyPressed(ByVal sender As Object, _
            ByVal e As HotkeyEventArgs) Handles hkM.HotkeyPressed
        'A hotkey is pressed, show the hotkey in a lable.
        Me.HotkeyLabel.Text = e.Hotkey.ToString
    End Sub
End Class

History

  • Originally posted on 08/05/08.

License

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


Written By
Software Developer (Senior) ZipEdTech
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey23-Feb-12 21:54
professionalManoj Kumar Choubey23-Feb-12 21:54 
GeneralGreat job Pin
Boutemine Oualid30-Aug-09 10:44
Boutemine Oualid30-Aug-09 10:44 
Nice work

with best regards
vb4arab.com

Questionformless application? Pin
Grumpet26-Jul-09 22:03
Grumpet26-Jul-09 22:03 
QuestionCatching the mouse events Pin
Amieiro11-Mar-09 11:39
Amieiro11-Mar-09 11:39 
GeneralGreat Work Pin
followmeyaar18-Sep-08 10:31
followmeyaar18-Sep-08 10:31 
GeneralVery Usefull Pin
DBSSoftware Inc. (Business)13-Aug-08 21:44
DBSSoftware Inc. (Business)13-Aug-08 21:44 
GeneralGood Pin
vikas amin13-Aug-08 5:33
vikas amin13-Aug-08 5:33 

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.