Hotkeys Class

In this post I will talk about Hotkeys class that 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 Hotkeys class is pressed. The class is a collection of registered hotkeys (in a Hotkey data type). The Hotkeys class can be used to register, unregister, or replace (registered by the Hotkeys class) hotkeys.

Screen Shot


Background:

The Hotkeys class uses two (RegisterHotKey and UnregisterHotKey) Windows API methods to register, unregister, and replace hotkeys. Most of the methods of Hotkeys class take an argument of Hotkey type. On its turn, the Hotkey type has very useful methods such as ToString which returns the string representation of the hotkey (example: Ctrl+H) and has many properties as System.Windows.Forms.Keys.

Code Example (VB.Net)

To use (create) a Hotkeys object, you should pass a valid window (Form) within the project as an argument to its constructor method. Here is a complete example:

Public Class Form1

    'Declaration of Hotkeys object.
    Dim WithEvents hkM As Hotkeys

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Creat a new instance of Hotkeys
        'objec and pass this form as its argument.
        Me.hkM = New Hotkeys(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.Add(hk)
        Catch ex As Exception
            'An exception is throw, show the exception message.
            MessageBox.Show(ex.Message, ex.GetType().ToString())
        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
            'Replace previously registered hotkey 
            'with the Id = 100 (Alt+G) with Shift+B.
            Me.hkM.Replace(100, Keys.Shift Or Keys.B)
        Catch ex As Exception
            'An exception is throw, show the exception message.
            MessageBox.Show(ex.Message, ex.GetType().ToString())
        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.Remove(100)
        Catch ex As Exception
            'An exception is throw, show the exception message.
            MessageBox.Show(ex.Message, ex.GetType().ToString())
        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.Name
    End Sub

End Class

System Requirements

  • Visual Studio
  • Dot Net Framework

Downloads



blog comments powered by Disqus