Answered by:
How to Assign Hot Key in Visual Basic 2010

Question
-
hello
how to assign Hot Key? like F2, F3 and F5 from form
i am created two forms. if press F5 key. then
goto to the Second form
with regards
Monday, January 9, 2012 10:34 PM
Answers
-
Hello Mind-reader,
please try this code:
regards Ellen
Imports System.Runtime.InteropServices Imports System.Diagnostics Public Class Form1 Friend WithEvents HotkeyControl1 As HotkeyControl Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load HotkeyControl1 = New HotkeyControl() Try HotkeyControl1.RegisterHotkey(Keys.M Or Keys.Alt) HotkeyControl1.RegisterHotkey(Keys.M Or Keys.Control) HotkeyControl1.RegisterHotkey(Keys.M Or Keys.Shift) Catch ex As Exception MsgBox("register " & ex.Message) End Try End Sub ' ------- HOTKEY Taskmanager -------------------- Private Sub HotkeyControl1_HotkeyPressed(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyEventArgs _ ) Handles HotkeyControl1.HotkeyPressed MsgBox(String.Format("Modifier: {0} Key: {1}", e.Modifiers.ToString(), e.KeyCode.ToString())) If e.Modifiers = Keys.Alt Then If e.KeyCode = Keys.M Then Process.Start("notepad") End If End If End Sub End Class Public Class HotkeyControl Inherits System.ComponentModel.Component Private m_HotkeyWindow As HotkeyWindow Public Event HotkeyPressed As KeyEventHandler Public Sub New() End Sub Protected Overrides Sub Dispose(ByVal disposing As Boolean) If m_HotkeyWindow IsNot Nothing Then m_HotkeyWindow.UnregisterHotkeys() m_HotkeyWindow.DestroyHandle() m_HotkeyWindow = Nothing End If MyBase.Dispose(disposing) End Sub Friend Sub OnHotkey(ByVal hotkey As Keys) Dim lKeyEvent As New KeyEventArgs(hotkey) RaiseEvent HotkeyPressed(Me, lKeyEvent) End Sub Public Function RegisterHotkey(ByVal hotkey As Keys) As Boolean If m_HotkeyWindow IsNot Nothing AndAlso m_HotkeyWindow.Contains(hotkey) Then Throw New ArgumentException("Hotkey is already registered.") End If If m_HotkeyWindow Is Nothing Then m_HotkeyWindow = New HotkeyWindow(Me) End If Return m_HotkeyWindow.RegisterHotkey(hotkey) End Function Public Sub UnregisterHotkey(ByVal hotkey As Keys) If m_HotkeyWindow IsNot Nothing AndAlso Not m_HotkeyWindow.Contains(hotkey) Then Throw New ArgumentException("Hotkey is not registered.") End If If m_HotkeyWindow IsNot Nothing Then m_HotkeyWindow.UnregisterHotkey(hotkey) End If End Sub Private Class HotkeyWindow Inherits NativeWindow Private m_Owner As HotkeyControl Private m_Hotkeys As Dictionary(Of Keys, Integer) Private m_LastID As Integer Public Sub New() m_Hotkeys = New Dictionary(Of Keys, Integer) m_LastID = 0 End Sub Public Sub New(ByVal owner As HotkeyControl) Me.New() m_Owner = owner End Sub Public Function Contains(ByVal hotkey As Keys) As Boolean Return m_Hotkeys.ContainsKey(hotkey) End Function Protected Overridable Sub Create() If Me.Handle = IntPtr.Zero Then Dim lCP As New CreateParams() lCP.Caption = Me.GetType().Name lCP.Style = 0 lCP.ExStyle = 0 lCP.ClassStyle = 0 MyBase.CreateHandle(lCP) End If End Sub Public Overrides Sub DestroyHandle() Me.UnregisterHotkeys() MyBase.DestroyHandle() End Sub Public Function RegisterHotkey(ByVal hotkey As Keys) As Boolean Dim lResult As Integer Dim lID As Integer Dim lModifier As UInteger Dim lVKey As UInteger = CType(hotkey And Not Keys.Modifiers, UInteger) If (hotkey And Keys.Control) = Keys.Control Then lModifier = NativeMethods.MOD_CONTROL End If If (hotkey And Keys.Alt) = Keys.Alt Then lModifier = lModifier Or NativeMethods.MOD_ALT End If If (hotkey And Keys.Shift) = Keys.Shift Then lModifier = lModifier Or NativeMethods.MOD_SHIFT End If lID = m_LastID + 1 If Me.Handle = IntPtr.Zero Then Me.Create() End If lResult = NativeMethods.RegisterHotKey(Me.Handle, lID, lModifier, lVKey) If CBool(lResult) Then m_Hotkeys.Add(hotkey, lID) m_LastID = lID Return True End If End Function Public Sub UnregisterHotkey(ByVal hotkey As Keys) NativeMethods.UnregisterHotKey(Me.Handle, m_Hotkeys(hotkey)) m_Hotkeys.Remove(hotkey) End Sub Public Sub UnregisterHotkeys() For Each item As KeyValuePair(Of Keys, Integer) In m_Hotkeys NativeMethods.UnregisterHotKey(Me.Handle, item.Value) Next m_Hotkeys.Clear() m_LastID = 0 End Sub Public Function LoWord(ByVal value As Integer) As Integer Return value And &HFFFF End Function Public Function HiWord(ByVal value As Integer) As Integer Return (value And &HFFFF0000) \ &H10000 End Function Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg = NativeMethods.WM_HOTKEY Then Dim lHotkey As Keys Dim lModifier As Integer = LoWord(m.LParam.ToInt32) Dim lKey As Integer = HiWord(m.LParam.ToInt32) If (lModifier And NativeMethods.MOD_CONTROL) = NativeMethods.MOD_CONTROL Then lHotkey = Keys.Control End If If (lModifier And NativeMethods.MOD_ALT) = NativeMethods.MOD_ALT Then lHotkey = lHotkey Or Keys.Alt End If If (lModifier And NativeMethods.MOD_SHIFT) = NativeMethods.MOD_SHIFT Then lHotkey = lHotkey Or Keys.Shift End If lHotkey = lHotkey Or CType(lKey, Keys) m_Owner.OnHotkey(lHotkey) Else MyBase.WndProc(m) End If End Sub End Class Friend Class NativeMethods Public Const WM_HOTKEY As Integer = &H312 Public Const MOD_ALT As UInteger = &H1 Public Const MOD_CONTROL As UInteger = &H2 Public Const MOD_SHIFT As UInteger = &H4 Public Const MOD_WIN As UInteger = &H8 <DllImport("user32.dll")> _ Public Shared Function RegisterHotKey(ByVal hWnd As IntPtr, _ ByVal id As Integer, _ ByVal fsModifiers As UInteger, _ ByVal vk As UInteger) As Integer End Function <DllImport("user32.dll")> _ Public Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, _ ByVal id As Integer) As Integer End Function End Class End Class
Ich benutze/ I'm using VB2008 & VB2010Tuesday, January 10, 2012 1:09 PM
All replies
-
Hello Mind-Reader,
hello
how to assign Hot Key? like F2, F3 and F5 from form
i am created two forms. if press F5 key. then
goto to the Second form
with regards
handling the KeyDown event of the main form, and setting the property to true KeyPreview the Main Form
Private Sub Form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If e.KeyCode.Equals(Keys.F5) Then Dim fr2 As New Form2 fr2.ShowDialog() End If End Sub
Regards.Monday, January 9, 2012 10:54 PM -
hello. Carmelo La Monica
Thanks for your reply
that code not worked for me.
cursor focus in DataGridView
any other way?
with regards
Tuesday, January 10, 2012 10:36 AM -
Hello Mind-reader,
please try this code:
regards Ellen
Imports System.Runtime.InteropServices Imports System.Diagnostics Public Class Form1 Friend WithEvents HotkeyControl1 As HotkeyControl Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load HotkeyControl1 = New HotkeyControl() Try HotkeyControl1.RegisterHotkey(Keys.M Or Keys.Alt) HotkeyControl1.RegisterHotkey(Keys.M Or Keys.Control) HotkeyControl1.RegisterHotkey(Keys.M Or Keys.Shift) Catch ex As Exception MsgBox("register " & ex.Message) End Try End Sub ' ------- HOTKEY Taskmanager -------------------- Private Sub HotkeyControl1_HotkeyPressed(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyEventArgs _ ) Handles HotkeyControl1.HotkeyPressed MsgBox(String.Format("Modifier: {0} Key: {1}", e.Modifiers.ToString(), e.KeyCode.ToString())) If e.Modifiers = Keys.Alt Then If e.KeyCode = Keys.M Then Process.Start("notepad") End If End If End Sub End Class Public Class HotkeyControl Inherits System.ComponentModel.Component Private m_HotkeyWindow As HotkeyWindow Public Event HotkeyPressed As KeyEventHandler Public Sub New() End Sub Protected Overrides Sub Dispose(ByVal disposing As Boolean) If m_HotkeyWindow IsNot Nothing Then m_HotkeyWindow.UnregisterHotkeys() m_HotkeyWindow.DestroyHandle() m_HotkeyWindow = Nothing End If MyBase.Dispose(disposing) End Sub Friend Sub OnHotkey(ByVal hotkey As Keys) Dim lKeyEvent As New KeyEventArgs(hotkey) RaiseEvent HotkeyPressed(Me, lKeyEvent) End Sub Public Function RegisterHotkey(ByVal hotkey As Keys) As Boolean If m_HotkeyWindow IsNot Nothing AndAlso m_HotkeyWindow.Contains(hotkey) Then Throw New ArgumentException("Hotkey is already registered.") End If If m_HotkeyWindow Is Nothing Then m_HotkeyWindow = New HotkeyWindow(Me) End If Return m_HotkeyWindow.RegisterHotkey(hotkey) End Function Public Sub UnregisterHotkey(ByVal hotkey As Keys) If m_HotkeyWindow IsNot Nothing AndAlso Not m_HotkeyWindow.Contains(hotkey) Then Throw New ArgumentException("Hotkey is not registered.") End If If m_HotkeyWindow IsNot Nothing Then m_HotkeyWindow.UnregisterHotkey(hotkey) End If End Sub Private Class HotkeyWindow Inherits NativeWindow Private m_Owner As HotkeyControl Private m_Hotkeys As Dictionary(Of Keys, Integer) Private m_LastID As Integer Public Sub New() m_Hotkeys = New Dictionary(Of Keys, Integer) m_LastID = 0 End Sub Public Sub New(ByVal owner As HotkeyControl) Me.New() m_Owner = owner End Sub Public Function Contains(ByVal hotkey As Keys) As Boolean Return m_Hotkeys.ContainsKey(hotkey) End Function Protected Overridable Sub Create() If Me.Handle = IntPtr.Zero Then Dim lCP As New CreateParams() lCP.Caption = Me.GetType().Name lCP.Style = 0 lCP.ExStyle = 0 lCP.ClassStyle = 0 MyBase.CreateHandle(lCP) End If End Sub Public Overrides Sub DestroyHandle() Me.UnregisterHotkeys() MyBase.DestroyHandle() End Sub Public Function RegisterHotkey(ByVal hotkey As Keys) As Boolean Dim lResult As Integer Dim lID As Integer Dim lModifier As UInteger Dim lVKey As UInteger = CType(hotkey And Not Keys.Modifiers, UInteger) If (hotkey And Keys.Control) = Keys.Control Then lModifier = NativeMethods.MOD_CONTROL End If If (hotkey And Keys.Alt) = Keys.Alt Then lModifier = lModifier Or NativeMethods.MOD_ALT End If If (hotkey And Keys.Shift) = Keys.Shift Then lModifier = lModifier Or NativeMethods.MOD_SHIFT End If lID = m_LastID + 1 If Me.Handle = IntPtr.Zero Then Me.Create() End If lResult = NativeMethods.RegisterHotKey(Me.Handle, lID, lModifier, lVKey) If CBool(lResult) Then m_Hotkeys.Add(hotkey, lID) m_LastID = lID Return True End If End Function Public Sub UnregisterHotkey(ByVal hotkey As Keys) NativeMethods.UnregisterHotKey(Me.Handle, m_Hotkeys(hotkey)) m_Hotkeys.Remove(hotkey) End Sub Public Sub UnregisterHotkeys() For Each item As KeyValuePair(Of Keys, Integer) In m_Hotkeys NativeMethods.UnregisterHotKey(Me.Handle, item.Value) Next m_Hotkeys.Clear() m_LastID = 0 End Sub Public Function LoWord(ByVal value As Integer) As Integer Return value And &HFFFF End Function Public Function HiWord(ByVal value As Integer) As Integer Return (value And &HFFFF0000) \ &H10000 End Function Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg = NativeMethods.WM_HOTKEY Then Dim lHotkey As Keys Dim lModifier As Integer = LoWord(m.LParam.ToInt32) Dim lKey As Integer = HiWord(m.LParam.ToInt32) If (lModifier And NativeMethods.MOD_CONTROL) = NativeMethods.MOD_CONTROL Then lHotkey = Keys.Control End If If (lModifier And NativeMethods.MOD_ALT) = NativeMethods.MOD_ALT Then lHotkey = lHotkey Or Keys.Alt End If If (lModifier And NativeMethods.MOD_SHIFT) = NativeMethods.MOD_SHIFT Then lHotkey = lHotkey Or Keys.Shift End If lHotkey = lHotkey Or CType(lKey, Keys) m_Owner.OnHotkey(lHotkey) Else MyBase.WndProc(m) End If End Sub End Class Friend Class NativeMethods Public Const WM_HOTKEY As Integer = &H312 Public Const MOD_ALT As UInteger = &H1 Public Const MOD_CONTROL As UInteger = &H2 Public Const MOD_SHIFT As UInteger = &H4 Public Const MOD_WIN As UInteger = &H8 <DllImport("user32.dll")> _ Public Shared Function RegisterHotKey(ByVal hWnd As IntPtr, _ ByVal id As Integer, _ ByVal fsModifiers As UInteger, _ ByVal vk As UInteger) As Integer End Function <DllImport("user32.dll")> _ Public Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, _ ByVal id As Integer) As Integer End Function End Class End Class
Ich benutze/ I'm using VB2008 & VB2010Tuesday, January 10, 2012 1:09 PM