使用全局熱鍵。這樣你就不必擔心你的應用程序有重點。對於VB.Net一個很好的例子是在這裏:
http://www.kirsbo.com/How_to_add_global_hotkeys_to_applications_in_VB.NET
來概括。定義熱鍵類:
Public Class Hotkey
#Region "Declarations - WinAPI, Hotkey constant and Modifier Enum"
''' <summary>
''' Declaration of winAPI function wrappers. The winAPI functions are used to register/unregister a hotkey
''' </summary>
Public Declare Function RegisterHotKey Lib "user32" _
(ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer
Public Const WM_HOTKEY As Integer = &H312
Enum KeyModifier
None = 0
Alt = &H1
Control = &H2
Shift = &H4
Winkey = &H8
End Enum 'This enum is just to make it easier to call the registerHotKey function: The modifier integer codes are replaced by a friendly "Alt","Shift" etc.
#End Region
#Region "Hotkey registration, unregistration and handling"
Public Shared Sub registerHotkey(ByRef sourceForm As Form, ByVal triggerKey As String, ByVal modifier As KeyModifier)
RegisterHotKey(sourceForm.Handle, 1, modifier, Asc(triggerKey.ToUpper))
End Sub
Public Shared Sub unregisterHotkeys(ByRef sourceForm As Form)
UnregisterHotKey(sourceForm.Handle, 1) 'Remember to call unregisterHotkeys() when closing your application.
End Sub
Public Shared Sub handleHotKeyEvent(ByVal hotkeyID As IntPtr)
MsgBox("The hotkey was pressed")
End Sub
#End Region
End Class
以下Sub然後添加到您的主要形式有:
'System wide hotkey event handling
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = Hotkey.WM_HOTKEY Then
Hotkey.handleHotKeyEvent(m.WParam)
End If
MyBase.WndProc(m)
End Sub
然後註冊在啓動代碼熱鍵爲您的表單:
Hotkey.RegisterHotKey(Me, "X", Hotkey.KeyModifier.Alt)
第一參數是形式,第二個是要處理的關鍵,第三個是修飾鍵。一旦這個熱鍵被註冊,它將觸發代碼HotKey.handleHotKeyEvent
。
然後,您可以使用某種回調來觸發調用表單中的方法,如果您願意的話。
可能重複[VB.NET檢測按鍵雖然最小化](http://stackoverflow.com/questions/5216222/vb-net-detecting-keypress-while-minimized) –