I have a few applications which I control using hotkeys. What I want to be able to do is simulate these hotkeys from a vb.net app. So for example if Program1 is launched by pressing CTRL+SHIFT+S I want to be able to click a button that simulates that key combination.
I'm fairly newly migrated from vb6 to .NET and also new to this site, so apologies if I've missed something obvious!
Thanks in advance,
Rich.
Loading
Scott LyslePosted Jan 11, 2008, 11:44 AM
Rich:
Francesco Balena wrote a pretty good article about registering hotkeys in VB.NET; the code same provided works fine; the full article is here: http://www.dotnet2themax.com/ShowContent.aspx?ID=103cca7a-0323-47eb-b210-c2bb7075ba78
The code provided for VB (the article also addresses C# is this):
Imports System.Runtime.InteropServices Public Class Form1 ' Windows API functions and constants 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 Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString 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 ' the id for the hotkey Dim hotkeyID As Short ' register a global hot key 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 = 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 ' unregister a global hotkey Sub UnregisterGlobalHotKey() If Me.hotkeyID <> 0 Then UnregisterHotKey(Me.Handle, hotkeyID) ' clean up the atom list GlobalDeleteAtom(hotkeyID) hotkeyID = 0 End If End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load ' register the Shift+Ctrl+F6 hot key RegisterGlobalHotKey(Keys.F6, 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 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 Me.Activate() MessageBox.Show("Hotkey has been pressed") End If End Sub End Class