RegisterHotKey
-
Hi, I use for some years this code that worked.
Public Class HotKey
' 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 ShortPublic Const MOD\_ALT As Integer = 1 Public Const MOD\_CONTROL As Integer = 2 Public Const MOD\_SHIFT As Integer = 4 Public Const MOD\_WIN As Integer = 8 Private f As Form Public Enum EKeyModifier Neutral Alt Control Control\_Alt Shift Shift\_Alt Shift\_Control Shift\_Alt\_Control Windows Win\_Alt Win\_Control Win\_Control\_Alt Win\_Shift Win\_Shift\_Alt Win\_Shift\_Control Win\_Shift\_Alt\_Control Norepeat = &H4000 End Enum Private \_Modifier As EKeyModifier Private \_Key As System.Windows.Forms.Keys ' the id for the hotkey Dim hotkeyID As Short Sub SDummy() MsgBox("Dummy routine ShortKey") End Sub Public Property Key() As System.Windows.Forms.Keys Get Return \_Key End Get Set(ByVal value As System.Windows.Forms.Keys) UnregisterGlobalHotKey() RegisterGlobalHotKey(value, \_Modifier, f) \_Key = value End Set End Property Public Property Modifier() As EKeyModifier Get Return \_Modifier End Get Set(ByVal value As EKeyModifier) UnregisterGlobalHotKey() RegisterGlobalHotKey(\_Key, value, f) \_Modifier = value End Set End Property ' register a global hot key Function RegisterGlobalHotKey(ByVal hotkey As EKeyModifier, ByVal modifiers As Integer, ByVal frm As Form) As Boolean Try f = frm ' use the GlobalAddAtom API to get a unique ID (as suggested by M
-
Hi, I use for some years this code that worked.
Public Class HotKey
' 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 ShortPublic Const MOD\_ALT As Integer = 1 Public Const MOD\_CONTROL As Integer = 2 Public Const MOD\_SHIFT As Integer = 4 Public Const MOD\_WIN As Integer = 8 Private f As Form Public Enum EKeyModifier Neutral Alt Control Control\_Alt Shift Shift\_Alt Shift\_Control Shift\_Alt\_Control Windows Win\_Alt Win\_Control Win\_Control\_Alt Win\_Shift Win\_Shift\_Alt Win\_Shift\_Control Win\_Shift\_Alt\_Control Norepeat = &H4000 End Enum Private \_Modifier As EKeyModifier Private \_Key As System.Windows.Forms.Keys ' the id for the hotkey Dim hotkeyID As Short Sub SDummy() MsgBox("Dummy routine ShortKey") End Sub Public Property Key() As System.Windows.Forms.Keys Get Return \_Key End Get Set(ByVal value As System.Windows.Forms.Keys) UnregisterGlobalHotKey() RegisterGlobalHotKey(value, \_Modifier, f) \_Key = value End Set End Property Public Property Modifier() As EKeyModifier Get Return \_Modifier End Get Set(ByVal value As EKeyModifier) UnregisterGlobalHotKey() RegisterGlobalHotKey(\_Key, value, f) \_Modifier = value End Set End Property ' register a global hot key Function RegisterGlobalHotKey(ByVal hotkey As EKeyModifier, ByVal modifiers As Integer, ByVal frm As Form) As Boolean Try f = frm ' use the GlobalAddAtom API to get a unique ID (as suggested by M