disrupt
asked on
VB Shortcut Key For Method
I 'm using Visual Basic 2008 and I have a program that sits in the tray and I want to execute a method by pressing shift + f5. How can I do this? Basically I want the keyboard shortcut to work even when the program is not in focus and sitting in the tray.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I was able to find it looking for Windows API's RegisterHotKey thanks !
Well, here's some VB code for ya. Wasn't too complicated so I was able to move into VB.Net, but I'm not particularly comfortable with VB.Net, so it may need a little tweaking.
You can use this class to register a global hotkey, and it will raise an event. Use it like this:
Public Class Form1
Private WithEvents ghk As GlobalHotkey
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ghk = New GlobalHotkey()
ghk.SetHotKey(Keys.F5, GlobalHotkey.KeyModifiers. MOD_SHIFT)
End Sub
Private Sub ghk_HotKeyPress(ByVal sender As Object, ByVal e As System.EventArgs) Handles ghk.HotKeyPress
MessageBox.Show("F5 KeyPress Detected")
End Sub
End Class
You can use this class to register a global hotkey, and it will raise an event. Use it like this:
Public Class Form1
Private WithEvents ghk As GlobalHotkey
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ghk = New GlobalHotkey()
ghk.SetHotKey(Keys.F5, GlobalHotkey.KeyModifiers.
End Sub
Private Sub ghk_HotKeyPress(ByVal sender As Object, ByVal e As System.EventArgs) Handles ghk.HotKeyPress
MessageBox.Show("F5 KeyPress Detected")
End Sub
End Class
Public Class GlobalHotkey
Inherits NativeWindow
Implements IDisposable
Private Const WM_HOTKEY As Integer = &H312
Public Event HotKeyPress As EventHandler
<Flags()> _
Public Enum KeyModifiers As Integer
None = &H0
MOD_ALT = &H1
MOD_CONTROL = &H2
MOD_SHIFT = &H4
MOD_WIN = &H8
End Enum
Private Declare Function RegisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As KeyModifiers, ByVal vk As Integer) As Boolean
Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
Private cp As CreateParams
Private hotKeyHandle As IntPtr
Private hotKeyId As Integer
Private hKey As Boolean = False
Public Sub New()
cp = New CreateParams()
Me.CreateHandle(cp)
hotKeyId = Me.GetHashCode()
hotKeyHandle = Me.Handle
End Sub
Public Function SetHotKey(ByVal Key As System.Windows.Forms.Keys, ByVal Modifiers As KeyModifiers) As Boolean
hKey = RegisterHotKey(hotKeyHandle, hotKeyId, Modifiers, Key)
Return hKey
End Function
Public Function UnsetHotKey() As Boolean
If hKey Then
Return UnregisterHotKey(hotKeyHandle, hotKeyId)
Else
Return True
End If
End Function
Protected Overridable Sub OnHotKeyPress(ByVal e As EventArgs)
RaiseEvent HotKeyPress(Me, e)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
If m.Msg = WM_HOTKEY Then
OnHotKeyPress(New EventArgs())
End If
End Sub
#Region "IDisposable Support"
Private disposedValue As Boolean = False ' To detect redundant calls
' IDisposable
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
Me.DestroyHandle()
End If
Try
UnsetHotKey()
Catch ex As Exception
End Try
End If
Me.disposedValue = True
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
ASKER