全域熱鍵 範例

Imports System.Runtime.InteropServices
Public Class Form1
    Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
    Private Declare Sub UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer)
    Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpStr As String) As Short
    Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Short) As Short

    Private Const MOD_ALT As Integer = 1
    Private Const MOD_CONTROL As Integer = 2
    Private Const MOD_SHIFT As Integer = 4
    Private Const MOD_WIN As Integer = 8

    Private count As Integer = 0

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        ' register the Shift+Ctrl+P hot key
        RegisterGlobalHotKey(Keys.P, MOD_SHIFT Or MOD_CONTROL)
    End Sub

    Private Sub Form1_Closed(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Closed
        ' unregister the hotkey (NEVER FORGET THIS!)
        UnregisterGlobalHotKey()
    End Sub


    ' unregister a global hotkey
    Public Sub UnregisterGlobalHotKey()
        If Me.hotkeyID <> 0 Then
            UnregisterHotKey(Me.Handle, hotkeyID)
            ' clean up the atom list
            GlobalDeleteAtom(hotkeyID)
            hotkeyID = 0
        End If
    End Sub


    ' the id for the hotkey
    Dim hotkeyID As Short

    ' register a global hot key
    Public Sub RegisterGlobalHotKey(ByVal hotkey As Keys, ByVal modifiers As Integer)
        Try
            ' use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs
            Dim atomName As String = Me.Name 'AppDomain.GetCurrentThreadId.ToString("X8") & Me.Name
            hotkeyID = GlobalAddAtom(atomName)
            If hotkeyID = 0 Then
                Throw New Exception("Unable to generate unique hotkey ID. Error code: " & _
                Marshal.GetLastWin32Error().ToString)
            End If

            ' register the hotkey, throw if any error
            If RegisterHotKey(Me.Handle, hotkeyID, modifiers, CInt(hotkey)) = 0 Then
                Throw New Exception("Unable to register hotkey. Error code: " & _
                Marshal.GetLastWin32Error.ToString)
            End If
        Catch ex As Exception
            ' clean up if hotkey registration failed
            UnregisterGlobalHotKey()
        End Try
    End Sub


    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        ' let the base class process the message
        MyBase.WndProc(m)

        ' if this is a WM_HOTKEY message, notify the parent object
        Const WM_HOTKEY As Integer = &H312
        If m.Msg = WM_HOTKEY Then
            ' do whatever you wish to do when the hotkey is pressed
            ' in this example we activate the form and display a messagebox
            count += 1
            lb1.Text = count.ToString
            Me.WindowState = FormWindowState.Normal
            Me.Activate()
        End If
    End Sub

End Class