I'm writing a simple program in VB2008 that runs in the system tray. I would like to set a hotkey combination (for instance Ctrl+Alt+Shift+K) that will trigger a subroutine.
So far, I've been able to get the program to respond to a key combination, but only if the form is
not minimized to the system tray. Here is some example code to demonstrate:
Imports System.Runtime.InteropServices
Public Partial Class MainForm
Public Const MOD_ALT As Integer = &H1
Public Const MOD_CONTROL As Integer = &H2
Public Const MOD_SHIFT As Integer = &H4
Public Const WM_HOTKEY As Integer = &H312
<DllImport("User32.dll")> _
Public Shared Function RegisterHotKey(ByVal hwnd As IntPtr, _
ByVal id As Integer, ByVal fsModifiers As Integer, _
ByVal vk As Integer) As Integer
End Function
<DllImport("User32.dll")> _
Public Shared Function UnregisterHotKey(ByVal hwnd As IntPtr, _
ByVal id As Integer) As Integer
End Function
Public Sub New()
Me.InitializeComponent()
End Sub
Sub MainFormLoad(sender As Object, e As EventArgs)
RegisterHotKey(Me.Handle, 100, MOD_CONTROL + MOD_ALT + MOD_SHIFT, Keys.K)
ContextMenuStrip1.Enabled = False
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
Dim id As IntPtr = m.WParam
Select Case (id.ToString)
Case "100"
MsgBox("Ctrl + Alt + Shift + K was pressed")
End Select
End If
MyBase.WndProc(m)
End Sub
Sub MainFormFormClosing(sender As Object, e As FormClosingEventArgs)
e.Cancel = True
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
ContextMenuStrip1.Enabled = True
End Sub
Sub ShowToolStripMenuItemClick(sender As Object, e As EventArgs)
Me.WindowState = FormWindowState.Normal
Me.ShowInTaskbar = True
ContextMenuStrip1.Enabled = False
End Sub
Sub ExitToolStripMenuItemClick(sender As Object, e As EventArgs)
UnregisterHotKey(Me.Handle, 100)
End
End Sub
Sub MainFormSizeChanged(sender As Object, e As EventArgs)
If Me.WindowState = FormWindowState.Minimized Then
ShowInTaskbar = False
ContextMenuStrip1.Enabled = True
End If
End Sub
End Class
If I change
ShowInTaskbar to
True in the MainFormSizeChanged handler, the program will respond to the key combination even when minimized to the system tray (presumably because it is still in the taskbar). While this is a solution, its not as 'proper' as it should be.
I think the solution is to use a
global keyboard hook, but this is going beyond my level of experience.
I've added a notify icon and context menu script to
this code[
^](which uses a keyboard hook to capture the printscreen key) and confirmed that the keyboard hook code does work even when minimized to the tray. But I'm having a hard time seeing how to adapt the code to work with my key combination.
A slightly different keyhook implementation can be found in
this code[
^]. I also added systray functionality to this code and it too works when minimized. But again, I'm not able to make the leap and adapt this to respond to my key combination.
Does anyone have any code suggestions or links that might help me get this working?