|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionA system wide hotkey is a key combination that fires a specific event regardless of which application has the input focus. For example, if you press WIN+E, then a new instance of Explorer is fired up, or if you press CTRL+PRINTSCREEN the currently active window is screen printed to the clipboard. The Win32 API has a couple of calls which are used to set up and respond to hotkeys - specifically <DllImport("user32", EntryPoint:="RegisterHotKey", _
SetLastError:=True, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Function RegisterHotkey(ByVal hwnd As IntPtr, _
ByVal Id As Int32, _
<MarshalAs(UnmanagedType.U4)> ByVal fsModifiers As Int32, _
<MarshalAs(UnmanagedType.U4)> ByVal vkey As Int32) As Boolean
End Function
<DllImport("user32", EntryPoint:="UnregisterHotKey", _
SetLastError:=True, _
ExactSpelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Function UnregisterHotkey(ByVal hwnd As Int32, _
ByVal Id As Int32) As Boolean
End Function
After a hotkey has been registered by the API call Specifying the key combinationThe key that triggers the event is defined by the two parameters
Public Enum HotkeyModifierFlags
MOD_ALT = &H1
MOD_CONTROL = &H2
MOD_SHIFT = &H4
MOD_WIN = &H8
End Enum
Thus, for example, if you want a hotkey which is CTRL+ALT+C, then the modifier would be
The Specifying a unique hotkey IDThe <DllImport("kernel32", EntryPoint:="GlobalAddAtom", _
SetLastError:=True, _
ExactSpelling:=False)> _
Public Function GlobalAddAtom(<MarshalAs(UnmanagedType.LPTStr)> _
ByVal lpString As String) As Int32
End Function
Creating a window to listen for the WM_HOTKEY messageThe final parameter is a window handle to listen for the The second option is achieved by creating a new class that derives from Public Class GlobalHotkeyListener
Inherits NativeWindow
#Region "Private member variables"
Private windowHandle As Integer
Private mwh As ManualResetEvent
#End Region
Public Sub New(ByVal Id As Int32, _
ByVal fsModifiers As Int32, _
ByVal vkey As Int32, _
ByRef wh As ManualResetEvent)
'\\ Get a local copy of the wait handle
mwh = wh
Dim cp As CreateParams = New CreateParams()
' Fill in the CreateParams details.
cp.Caption = ""
cp.ClassName = "STATIC"
' Set the position on the form
cp.X = 0
cp.Y = 0
cp.Height = 0
cp.Width = 0
'\\ Set the style and extended style flags
cp.Style = WindowStyleBits.WS_MINIMIZE
cp.ExStyle = WindowStyleExtendedBits.WS_EX_NOACTIVATE
' Create the actual window
Me.CreateHandle(cp)
Try
If Not RegisterHotkey(MyBase.Handle, _
Id, fsModifiers, vkey) Then
Throw New Win32Exception()
End If
Catch e As Exception
System.Diagnostics.Debug.WriteLine(e.ToString)
End Try
End Sub
And the listening for the <System.Security.Permissions._
PermissionSetAttribute(System.Security.Permissions_
.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub WndProc(ByRef m As Message)
' Listen for messages that are sent to the
' button window. Some messages are sent
' to the parent window instead of the button's window.
Select Case (m.Msg)
Case WM_HOTKEY
' Respond to the hotkey message (asynchronously??)
If Not mwh Is Nothing Then
mwh.Set()
End If
End Select
MyBase.WndProc(m)
End Sub
Future improvementsIdeally this control should have a
|
||||||||||||||||||||||