Descripcion: Registrar teclas para un HotKey
Controles: 1 Boton
En una clase:
Public Enum ModConst
MOD_ALT = &H1
MOD_CONTROL = &H2
MOD_SHIFT = &H4
MOD_WINKEY = &H8
End Enum
Public Class cHotKey
Public hID As Integer
Public hModifier As ModConst
Public hKeyCode As Integer
Public hAction As Integer
Public hDescription As String
'Constructores
Sub New()
End Sub
Sub New(ByVal ID As Integer, ByVal Modifier As ModConst, ByVal KeyCode As Integer, Optional ByVal Action As Integer = 0, Optional ByVal Description As String = "")
hID = ID
hModifier = Modifier
hKeyCode = KeyCode
hAction = Action
hDescription = Description
End Sub
Friend Function SetValues(ByVal ID As Integer, ByVal Modifier As ModConst, ByVal KeyCode As Integer, Optional ByVal Action As Integer = 0, Optional ByVal Description As String = "") As Boolean
hID = ID
hModifier = Modifier
hKeyCode = KeyCode
hAction = Action
hDescription = Description
End Function
Public Overrides Function ToString() As String
Return hID & "," & hModifier & "," & hKeyCode & "," & hAction & "," & hDescription
End Function
End Class
Public Class HotKeysManager
'Heredo de Form para tener el sub wndProc
Inherits System.Windows.Forms.Form
Private g_hWnd As Integer
'Colección de cHotKeys donde se guardarán los datos de las hotkeys registradas
Private g_HotKeys As New Collection
'Hotkey Message. used in WndProc method
Public Const WM_HOTKEY = &H312
Public Event HotKeyPressed(ByVal oHotKey As cHotKey)
'Constructores
Sub New()
g_hWnd = Me.Handle.ToInt32
End Sub
Private Declare Function RegisterHotKey Lib "user32" (ByVal hWnd As Integer, ByVal id As Integer, ByVal fsModifiers As ModConst, ByVal vk As Integer) As Boolean
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hWnd As Integer, ByVal id As Integer) As Boolean
'Elimina de la colección todas las hotkeys y las desregistra
Public Function Clear()
Dim tmp As cHotKey
For Each tmp In g_HotKeys
DelHotKey(tmp.hID)
Next
End Function
'Agrega una hotkey a la colección y la registra
Public Function AddHotKey(ByVal HotKeyID As Integer, ByVal Modifer As ModConst, ByVal KeyCode As Integer, Optional ByVal Action As Integer = 0, Optional ByVal Description As String = "") As Boolean
Dim bRet As Boolean
'borro el elemento y lo desregistro (si existía)
Dim tmp As cHotKey
For Each tmp In g_HotKeys
If tmp.hID = HotKeyID Then
DelHotKey(HotKeyID)
End If
Next
'Registro la HotKey en el SO
bRet = RegisterHotKey(g_hWnd, HotKeyID, Modifer, KeyCode)
If bRet Then
'Creo un nuevo objeto de la clase cHotKey y lo inserto a la colección
Dim newHotKey As New cHotKey(HotKeyID, Modifer, KeyCode, Action, Description)
g_HotKeys.Add(newHotKey, HotKeyID.ToString)
End If
Return bRet
End Function
'Eliminar una hotkey de la colección y la desregistra
Public Function DelHotKey(ByVal HotKeyID As Integer) As Boolean
Dim bRet As Boolean = False
If g_HotKeys.Count > 0 Then
bRet = UnregisterHotKey(g_hWnd, HotKeyID)
If bRet Then
g_HotKeys.Remove(HotKeyID.ToString)
End If
End If
Return bRet
End Function
'Devuelve el ítem de la colección a partir de su índice
Public Function Item(ByVal index As Integer) As cHotKey
Return g_HotKeys.Item(index)
End Function
'Devuelve el ítem de la colección a partir key
Public Function Item(ByVal key As String) As cHotKey
Return g_HotKeys.Item(key)
End Function
'Devuelve la cantidad de hotkeys en la colección
Public Function Count() As Integer
Return g_HotKeys.Count
End Function
'Aquí se reciben los mensajes (al presionar las hotkeys)
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY And g_HotKeys.Count > 0 Then
RaiseEvent HotKeyPressed(g_HotKeys(m.WParam.ToString))
End If
MyBase.WndProc(m)
End Sub
End Class
Friend Structure POINTAPI
Dim x As Integer
Dim y As Integer
End Structure
Friend Structure Msg
Dim hWnd As Integer
Dim Message As Integer
Dim wParam As Integer
Dim lParam As Integer
Dim time As Integer
Dim pt As POINTAPI
End Structure
En el form:
Public Class Form1
Public WithEvents hk As New HotKeysManager
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
hk.AddHotKey(1, ModConst.MOD_CONTROL, Asc("Q"), 1, "Esta es la hot-key 1 (CTRL-Q)")
hk.AddHotKey(2, ModConst.MOD_CONTROL, Asc("W"), 1, "Esta es la hot-key 2 (CTRL-W)")
End Sub
Private Sub hk_HotKeyPressed(ByVal oHotKey As cHotKey) Handles hk.HotKeyPressed
MsgBox(oHotKey.hDescription)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Text = "Registrar HotKeys (CTRL-Q y CTRL-W)"
End Sub
End Class
Presiona Ctrl + Q / Ctrl + W