Results 1 to 6 of 6

Thread: [2005] Register Multiple Global Hotkeys (E.G Ctrl + G + E)

  1. #1

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    [2005] Register Multiple Global Hotkeys (E.G Ctrl + G + E)

    I am having two problems:
    Problem 1: I am unable to register a series of key combinations, such as Ctrl + Shift + E + K, I can only register E or K...
    Problem 2: when I put Keys.Control, or Keys.Shift in the second last parameter of RegisterHotKey (which is the fsModifiers parameter) the hotkey doesn't work... even if I put in the last parameter as Keys.Control where normally Keys.E would go, it doesn't launch when I press control...
    Does anyone know why?

    The code I am using:

    vb.net Code:
    1. Public Class Form1    
    2.       Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer    
    3.       Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer    
    4.       Public Const WM_HOTKEY As Integer = &H312    
    5.       Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)                  
    6.       If m.Msg = WM_HOTKEY Then        ' Pressed the hotkey
    7.          Debug.WriteLine(m.Msg.ToString & ":" & m.WParam.ToString & ":" & m.LParam.ToString)      
    8.       End If
    9.       MyBase.WndProc(m)
    10.       End Sub    
    11.       Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles e.FormClosing
    12.            Call UnregisterHotKey(Me.Handle.toint32, 0)
    13.       End Sub    
    14.       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    15.            Call RegisterHotKey(Me.Handle.toint32, 0, 0, 27)
    16.       End Sub
    17.       End Class

    Thanks
    Last edited by Icyculyr; Jan 28th, 2008 at 02:05 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Register Multiple Global Hotkeys (E.G Ctrl + G + E)

    You can only have one non-modifier in a key combination. A valid key combination is Ctrl alone or Ctrl with Shift and/or Alt and one other non-control key.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] Register Multiple Global Hotkeys (E.G Ctrl + G + E)

    I see, so how can I register this hotkey:

    Ctrl + Shift + Alt + L
    If you could tell me, that would be great,
    I tried 'Call RegisterHotKey(Me.Handle.toint32, 0, 17, 65)'
    17 is ctrl, 65 is A (I think), but not only does this key combination not work (as soon as I add a modifier it stops working), but I wonder how I would add multiple modifiers....

    Can anyone help me?

    Thanks

  4. #4

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] Register Multiple Global Hotkeys (E.G Ctrl + G + E)

    Ok, I found working code, but one line that says 'warning', I am worried about, the warning is roughly "AppDomain.GetManagedThreadID is obsolete, it has been deprecated because it provides an unstable ID when working with fibers (aka lightweight threads), to get a stable ID, use the ManageThreadID property on thread"


    I was unsure on how to do this? I know how to do

    Dim thrd As New Thread()
    thrd.GetManagedThreadID(), but I don't think that's what it wants, I think I need the thread my form is on? or I just don't know, thats why I am asking how to do this:

    Specific Part:
    (the line is "Dim atomName As String = AppDomain.GetCurrentThreadId.ToString("X8") & Me.Name")
    vb.net Code:
    1. ' register a global hot key
    2.     Public Sub RegisterGlobalHotKey(ByVal hotkey As Keys, ByVal modifiers As Integer)
    3.         Try
    4.             ' use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs
    5.             Dim atomName As String = AppDomain.GetCurrentThreadId.ToString("X8") & Me.Name
    6.             hotkeyID = GlobalAddAtom(atomName)
    7.             If hotkeyID = 0 Then
    8.                 Throw New Exception("Unable to generate unique hotkey ID. Error code: " & _
    9.                    Marshal.GetLastWin32Error().ToString)
    10.             End If
    11.  
    12.             ' register the hotkey, throw if any error
    13.             If RegisterHotKey(Me.Handle, hotkeyID, modifiers, CInt(hotkey)) = 0 Then
    14.                 Throw New Exception("Unable to register hotkey. Error code: " & _
    15.                    Marshal.GetLastWin32Error.ToString)
    16.             End If
    17.         Catch ex As Exception
    18.             ' clean up if hotkey registration failed
    19.             UnregisterGlobalHotKey()
    20.         End Try
    21.     End Sub

    Whole Code:

    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2. Public Class Form1
    3.     Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, _
    4.        ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
    5.     Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) _
    6.        As Integer
    7.     Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As _
    8.        String) As Short
    9.     Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Short) As Short
    10.  
    11.     Private Const MOD_ALT As Integer = 1
    12.     Private Const MOD_CONTROL As Integer = 2
    13.     Private Const MOD_SHIFT As Integer = 4
    14.     Private Const MOD_WIN As Integer = 8
    15.  
    16.  
    17.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    18.         ' register the Shift+Ctrl+F6 hot key
    19.         RegisterGlobalHotKey(Keys.F6, MOD_SHIFT Or MOD_CONTROL)
    20.     End Sub
    21.  
    22.     Private Sub Form1_Closed(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Closed
    23.         ' unregister the hotkey (NEVER FORGET THIS!)
    24.         UnregisterGlobalHotKey()
    25.     End Sub
    26.  
    27.  
    28.     ' unregister a global hotkey
    29.     Public Sub UnregisterGlobalHotKey()
    30.         If Me.hotkeyID <> 0 Then
    31.             UnregisterHotKey(Me.Handle, hotkeyID)
    32.             ' clean up the atom list
    33.             GlobalDeleteAtom(hotkeyID)
    34.             hotkeyID = 0
    35.         End If
    36.     End Sub
    37.  
    38.  
    39.     ' the id for the hotkey
    40.     Dim hotkeyID As Short
    41.  
    42.     ' register a global hot key
    43.     Public Sub RegisterGlobalHotKey(ByVal hotkey As Keys, ByVal modifiers As Integer)
    44.         Try
    45.             ' use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs
    46.             Dim atomName As String = AppDomain.GetCurrentThreadId.ToString("X8") & Me.Name
    47.             hotkeyID = GlobalAddAtom(atomName)
    48.             If hotkeyID = 0 Then
    49.                 Throw New Exception("Unable to generate unique hotkey ID. Error code: " & _
    50.                    Marshal.GetLastWin32Error().ToString)
    51.             End If
    52.  
    53.             ' register the hotkey, throw if any error
    54.             If RegisterHotKey(Me.Handle, hotkeyID, modifiers, CInt(hotkey)) = 0 Then
    55.                 Throw New Exception("Unable to register hotkey. Error code: " & _
    56.                    Marshal.GetLastWin32Error.ToString)
    57.             End If
    58.         Catch ex As Exception
    59.             ' clean up if hotkey registration failed
    60.             UnregisterGlobalHotKey()
    61.         End Try
    62.     End Sub
    63.  
    64.  
    65.     Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    66.         ' let the base class process the message
    67.         MyBase.WndProc(m)
    68.  
    69.         ' if this is a WM_HOTKEY message, notify the parent object
    70.         Const WM_HOTKEY As Integer = &H312
    71.         If m.Msg = WM_HOTKEY Then
    72.             ' do whatever you wish to do when the hotkey is pressed
    73.             ' in this example we activate the form and display a messagebox
    74.             Me.WindowState = FormWindowState.Normal
    75.             Me.Activate()
    76.         End If
    77.     End Sub
    78.  
    79. End Class


    Thanks

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Register Multiple Global Hotkeys (E.G Ctrl + G + E)

    You get a reference to the current thread with the Thread.CurrentThread property, so you'd get the ManagedThreadId of that Thread.
    Last edited by jmcilhinney; Jan 29th, 2008 at 07:44 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Frenzied Member Icyculyr's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    1,934

    Re: [2005] Register Multiple Global Hotkeys (E.G Ctrl + G + E)

    Whats the current thread? form1?

    Edit: hold on, I see.
    Last edited by Icyculyr; Jan 29th, 2008 at 03:33 PM.

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