Results 1 to 21 of 21

Thread: Register Global HotKeys

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member DavesChillaxin's Avatar
    Join Date
    Mar 2011
    Location
    WNY
    Posts
    451

    Register Global HotKeys

    UPDATE: New updates have been made for ease of use and implementation in any project. Attached is a new working example.

    Here's a class which gives you the ability to add hotkeys globally! This means regardless the forms focus state, your hotkeys guaranteed to fire.
    Note: Hotkeys can be overwritten if you will by other applications. If you register Ctrl+A, then afterwards a game does too. The game now has that hotkey and yours will no longer fire.

    Class
    vbnet Code:
    1. Public NotInheritable Class HotKeyRegistryClass
    2.  
    3.     Private Declare Function RegisterHotKey Lib "user32.dll" (ByVal handle As IntPtr, ByVal id As Int32, ByVal fsModifier As Int32, ByVal vk As Int32) As Int32
    4.     Private Declare Function UnregisterHotKey Lib "user32.dll" (ByVal handle As IntPtr, ByVal id As Int32) As Int32
    5.  
    6.     Private Handle As IntPtr = IntPtr.Zero
    7.     Private Registry As New System.Collections.Generic.List(Of Int32)
    8.  
    9.     Public Enum Messages
    10.         [WM_HOTKEY] = &H312
    11.     End Enum
    12.     Public Enum Modifiers
    13.         [MOD_ALT] = &H1
    14.         [MOD_CTRL] = &H2
    15.         [MOD_SHIFT] = &H4
    16.     End Enum
    17.  
    18.  
    19.     Sub New(ByVal Handle As IntPtr)
    20.         Me.Handle = Handle
    21.     End Sub
    22.  
    23.     Public Function Register(ByVal Modifier As Int32, ByVal Key As System.Windows.Forms.Keys) As Int32
    24.         Dim ret As Int32
    25.  
    26.         ret = NextAvailableIndex()
    27.         Call RegisterHotKey(Me.Handle, ret, Modifier, Key)
    28.         Registry.Insert(ret, ret)
    29.         Return ret
    30.     End Function
    31.     Public Sub Unregister(ByVal ID As Int32)
    32.         Call UnregisterHotKey(Me.Handle, ID)
    33.         Registry.Remove(ID)
    34.     End Sub
    35.  
    36.     Private Function NextAvailableIndex() As Int32
    37.         Dim ret As Int32 = 0
    38.         Dim n As Int32 = 0
    39.  
    40.         For i As Int32 = 0 To Registry.Count - 1
    41.             If Registry(i) = n Then
    42.                 n = n + 1
    43.             ElseIf n < Registry(i) Then
    44.                 Return n
    45.             End If
    46.         Next
    47.         If n = Registry.Count Then
    48.             Return Registry.Count
    49.         End If
    50.         Return ret
    51.     End Function
    52.  
    53. End Class


    Finally you must place this in the form you registered your new instance of the hotkey registry class with.

    vbnet Code:
    1. Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    2.         If m.Msg = HotKeyRegistryClass.Messages.WM_HOTKEY Then
    3.             Dim ID As String = m.WParam.ToString()
    4.             Select Case ID
    5.                 case 0
    6.                 case 1
    7.                 case 2
    8.                 case ...
    9.             End Select
    10.         End If
    11.         MyBase.WndProc(m)
    12.     End Sub
    Attached Files Attached Files
    Last edited by DavesChillaxin; Aug 21st, 2012 at 03:06 PM.
    Please rate if my post was helpful!
    Per favore e grazie!




    Code Bank:
    Advanced Algebra Class *Update | True Gradient Label Control *Dev | A Smarter TextBox *Update | Register Global HotKey *Update
    Media Library Beta *Dev | Mouse Tracker (Available in VB.net and C#.net) *New | On-Screen Numpad (VB.net) *New

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width