Crear hotkeys globales fuera del form, usando ComboBoxes.
Solo hay que añadir dos comboboxes al form (los valores se añaden al crear la ventana):
Solo hay que añadir dos comboboxes al form (los valores se añaden al crear la ventana):
Código (vbnet) [Seleccionar]
#Region " Set Global Hotkeys using ComboBoxes "
' [ Set Global Hotkeys using ComboBoxes Example ]
'
' // By Elektro H@cker
'
' Instructions :
' Instructions:
' 1. Add the "GlobalHotkeys Class" Class to the project.
' 2. Add a ComboBox in the Form with the name "ComboBox_SpecialKeys", with DropDownStyle property.
' 3. Add a ComboBox in the Form with the name "ComboBox_NormalKeys", with DropDownStyle property.
Dim SpecialKeys As String() = {"NONE", "ALT", "CTRL", "SHIFT"}
Dim NormalKeys As String() = { _
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", _
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", _
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", _
"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"}
Dim SpecialKey As String = SpecialKeys(0)
Dim NormalKey As System.Windows.Forms.Keys
Dim WithEvents HotKey_Global As Shortcut
' Form load
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each Item In SpecialKeys
ComboBox_SpecialKeys.Items.Add(Item)
Application.DoEvents()
Next
For Each Item In NormalKeys
ComboBox_NormalKeys.Items.Add(Item)
Application.DoEvents()
Next
ComboBox_SpecialKeys.SelectedItem = SpecialKeys(0)
' ComboBox_NormalKeys.SelectedItem = NormalKeys(0)
End Sub
' ComboBoxes SelectedKeys
Private Sub ComboBoxes_SelectedIndexChanged(sender As Object, e As EventArgs) Handles _
ComboBox_SpecialKeys.SelectedIndexChanged, _
ComboBox_NormalKeys.SelectedIndexChanged
SpecialKey = ComboBox_SpecialKeys.Text
Try : Select Case ComboBox_SpecialKeys.Text
Case "ALT"
NormalKey = [Enum].Parse(GetType(System.Windows.Forms.Keys), ComboBox_NormalKeys.Text, True)
HotKey_Global = Shortcut.Create(Shortcut.Modifier.Alt, NormalKey)
Case "CTRL"
NormalKey = [Enum].Parse(GetType(System.Windows.Forms.Keys), ComboBox_NormalKeys.Text, True)
HotKey_Global = Shortcut.Create(Shortcut.Modifier.Ctrl, NormalKey)
Case "SHIFT"
NormalKey = [Enum].Parse(GetType(System.Windows.Forms.Keys), ComboBox_NormalKeys.Text, True)
HotKey_Global = Shortcut.Create(Shortcut.Modifier.Shift, NormalKey)
Case "NONE"
Dim Number_RegEx As New System.Text.RegularExpressions.Regex("\D")
If Number_RegEx.IsMatch(ComboBox_NormalKeys.Text) Then
NormalKey = [Enum].Parse(GetType(System.Windows.Forms.Keys), ComboBox_NormalKeys.Text, True)
Else
NormalKey = [Enum].Parse(GetType(System.Windows.Forms.Keys), (ComboBox_NormalKeys.Text + 96), False)
End If
HotKey_Global = Shortcut.Create(Shortcut.Modifier.None, NormalKey)
End Select
Catch : End Try
End Sub
' Hotkey is pressed
Private Sub HotKey_Press(ByVal s As Object, ByVal e As Shortcut.HotKeyEventArgs) Handles HotKey_Global.Press
MsgBox("hotkey clicked: " & SpecialKey & "+" & NormalKey.ToString)
End Sub
#End Region
#Region " GlobalHotkeys Class "
Class Shortcut
Inherits NativeWindow
Implements IDisposable
Protected Declare Function UnregisterHotKey Lib "user32.dll" (ByVal handle As IntPtr, ByVal id As Integer) As Boolean
Protected Declare Function RegisterHotKey Lib "user32.dll" (ByVal handle As IntPtr, ByVal id As Integer, ByVal modifier As Integer, ByVal vk As Integer) As Boolean
Event Press(ByVal sender As Object, ByVal e As HotKeyEventArgs)
Protected EventArgs As HotKeyEventArgs, ID As Integer
Enum Modifier As Integer
None = 0
Alt = 1
Ctrl = 2
Shift = 4
End Enum
Class HotKeyEventArgs
Inherits EventArgs
Property Modifier As Shortcut.Modifier
Property Key As Keys
End Class
Class RegisteredException
Inherits Exception
Protected Const s As String = "Shortcut combination is in use."
Sub New()
MyBase.New(s)
End Sub
End Class
Private disposed As Boolean
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not disposed Then UnregisterHotKey(Handle, ID)
disposed = True
End Sub
Protected Overrides Sub Finalize()
Dispose(False)
MyBase.Finalize()
End Sub
Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
<DebuggerStepperBoundary()>
Sub New(ByVal modifier As Modifier, ByVal key As Keys)
CreateHandle(New CreateParams)
ID = GetHashCode()
EventArgs = New HotKeyEventArgs With {.Key = key, .Modifier = modifier}
If Not RegisterHotKey(Handle, ID, modifier, key) Then Throw New RegisteredException
End Sub
Shared Function Create(ByVal modifier As Modifier, ByVal key As Keys) As Shortcut
Return New Shortcut(modifier, key)
End Function
Protected Sub New()
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case 786
RaiseEvent Press(Me, EventArgs)
Case Else
MyBase.WndProc(m)
End Select
End Sub
End Class
#End Region