• Log in or sign up for an account here: https://www.sythe.org/login

Hot keys made easy

Flaming Idiots

Active Member
Joined
Dec 22, 2005
Sythe Gold
0
Messages
235
Age
36
Location
Alberta Canada
Hot keys made easy
I made a small component which will make hot keys very easy. It can be used in less than 10 lines of code easily.

If you have no idea how to use delegates, then you should read this: http://abstractvb.com/code.asp?A=1084

Create a new class file, and add this code to it.
Code:
<System.ComponentModel.DefaultEvent("HotKeyRaised")> _
Public Class HotKeysManager
    Inherits System.ComponentModel.Component
    Private WithEvents HotKeyListener As New Listener
    Public Event HotKeyRaised(ByVal sender As Object, ByVal e As HotKeysEventArgs)
    Public Delegate Sub Method()
    Private ReadOnly HotKeyList As New Dictionary(Of Integer, HotKeysEventArgs)
    <Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function RegisterHotKey _
     (ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, _
      ByVal vlc As Integer) As Boolean
    End Function
    <Runtime.InteropServices.DllImport("user32.dll")> _
    Private Shared Function UnregisterHotKey _
        (ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
    End Function
    Private Shared Function MakeLWord(ByVal LoWord As Integer, ByVal HiWord As Integer) As Long
        Return (HiWord * &H10000) Or (LoWord And &HFFFF&)
    End Function
    Public Sub AddHotKey(ByVal Key As Keys, ByVal Modifier As HotkeyModifier, ByVal Method As Method)
        RegisterHotKey(Me.HotKeyListener.Handle, Method.Method.MethodHandle.Value, Modifier, Key)
        HotKeyList.Add(MakeLWord(Modifier, Key), New HotKeysEventArgs(Modifier, Keys.KeyCode, Method))
    End Sub
    Public Function RemoveHotKey(ByVal Key As Keys, ByVal Modifier As HotkeyModifier) As Boolean
        If Me.HotKeyList.ContainsKey(MakeLWord(Modifier, Key)) Then
            Dim V As HotKeysEventArgs = Me.HotKeyList(MakeLWord(Modifier, Key))
            Me.HotKeyList.Remove(MakeLWord(Modifier, Key))
            Return UnregisterHotKey(Me.HotKeyListener.Handle, V.ID)
        Else
            Return False
        End If
    End Function
    Public Class Listener
        Inherits NativeWindow
        Sub New()
            Dim cp As CreateParams = New CreateParams()
            cp.Caption = ""
            cp.ClassName = "STATIC"
            cp.X = 0
            cp.Y = 0
            cp.Height = 0
            cp.Width = 0
            cp.Style = 0
            cp.ExStyle = 0
            Me.CreateHandle(cp)
        End Sub
        Public Event OnHotkey(ByVal M As Message)
        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            MyBase.WndProc(m)
            If m.Msg = &H312 Then
                RaiseEvent OnHotkey(m)
            End If
        End Sub
    End Class
    Public Class HotKeysEventArgs
        Inherits EventArgs
        Sub New(ByVal [Mod] As HotkeyModifier, ByVal HotKey As Keys, ByVal InvokedMethod As Method)
            _Modifier = [Mod]
            _Key = HotKey
            _Method = InvokedMethod
        End Sub
        Private _Modifier As HotkeyModifier = HotkeyModifier.None
        Public ReadOnly Property Modifier() As HotkeyModifier
            Get
                Return _Modifier
            End Get
        End Property
        Private _Key As Keys = Keys.None
        Public ReadOnly Property Key() As Keys
            Get
                Return _Key
            End Get
        End Property

        Private _Method As Method
        Public ReadOnly Property Method() As Method
            Get
                Return _Method
            End Get
        End Property
        Private _ID As Integer
        Public ReadOnly Property ID() As Integer
            Get
                Return _ID
            End Get
        End Property
    End Class
    Public Enum HotkeyModifier
        None = &H0
        Alt = &H1
        Control = &H2
        Shift = &H4
        Win = &H8
    End Enum
    Private Sub HotKeyListener_OnHotkey(ByVal M As System.Windows.Forms.Message) Handles HotKeyListener.OnHotkey
        If Me.HotKeyList.ContainsKey(M.LParam) Then RaiseEvent HotKeyRaised(Me, Me.HotKeyList(M.LParam))
    End Sub
End Class
And here an example on how to use it with a form.

When you press Escape, it will close the form.
When you press Enter, it will hide the form.
When you press Space, it will show the form.
Code:
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.HotKeysManager1.AddHotKey(Keys.Escape, HotKeysManager.HotkeyModifier.None, AddressOf Me.Close)
        Me.HotKeysManager1.AddHotKey(Keys.Enter, HotKeysManager.HotkeyModifier.None, AddressOf Me.Hide)
        Me.HotKeysManager1.AddHotKey(Keys.Space, HotKeysManager.HotkeyModifier.None, AddressOf Me.Show)
    End Sub

    Private Sub HotKeysManager1_HotKeyRaised(ByVal sender As System.Object, ByVal e As HotKeysManager.HotKeysEventArgs) Handles HotKeysManager1.HotKeyRaised
        e.Method.Invoke()
    End Sub
the reason why you invoke it from the HotKeyRaised event is so that you can check if the form has focus or something like that to prevent errors. So you could do this to stop it from closing if the form is hidden.

Code:
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.HotKeysManager1.AddHotKey(Keys.Escape, HotKeysManager.HotkeyModifier.None, AddressOf Close)
        Me.HotKeysManager1.AddHotKey(Keys.Enter, HotKeysManager.HotkeyModifier.None, AddressOf Me.Hide)
        Me.HotKeysManager1.AddHotKey(Keys.Space, HotKeysManager.HotkeyModifier.None, AddressOf Me.Show)
    End Sub

    Private Sub HotKeysManager1_HotKeyRaised(ByVal sender As System.Object, ByVal e As HotKeysManager.HotKeysEventArgs) Handles HotKeysManager1.HotKeyRaised
        If e.Method.Method.MethodHandle.Value <> New HotKeysManager.Method(AddressOf Show).Method.MethodHandle.Value Then
            If Me.Visible = False Then Return
        End If
        e.Method.Invoke()
    End Sub
 
Runescape Gamblingrunescape gambling
Back
Top