65.9K
CodeProject 正在变化。 阅读更多。
Home

HotkeyManager 类

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.82/5 (12投票s)

2008 年 8 月 5 日

CPOL
viewsIcon

44884

downloadIcon

1234

HotkeyManager 类使为应用程序设置全局热键变得容易。

引言

HotkeyManager 类使为应用程序设置全局热键变得容易。它是一个包装类,每当 HotkeyManager 类注册的热键被按下时,都会引发一个 HotkeyPressed 事件。该类维护一个已注册热键的集合(以 Hotkey 数据类型形式),并且通过 HotkeyMananger 对象的 Hotkeys 属性可供开发人员使用。HotkeyManager 可用于注册、注销或替换(由 HotkeyManager 注册的)热键。

Hotkeys/Hotkey.png

背景

要使用(创建)一个 HotkeyManager 对象,您应该将项目中的一个有效的窗口(Form)作为参数传递给其构造函数。HotkeyManager 使用两个 API 方法(RegisterHotKeyUnregisterHotKey)来注册、注销和替换热键。HotkeyManager 的所有方法都接受一个 Hotkey 类型的参数。

另一方面,Hotkey 结构也有非常有用的方法,例如 ToString,它返回热键的字符串表示形式(例如:Ctrl+H)和许多属性(如 System.Windows.Forms.Keys)。

使用代码

以下是如何使用 HotkeyManager 类的一个示例

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

历史

  • 最初发布于 2008/05/08。
© . All rights reserved.