|
-
Jan 28th, 2008, 01:53 AM
#1
Thread Starter
Frenzied Member
[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:
Public Class Form1 Public Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer Public Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) As Integer Public Const WM_HOTKEY As Integer = &H312 Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg = WM_HOTKEY Then ' Pressed the hotkey Debug.WriteLine(m.Msg.ToString & ":" & m.WParam.ToString & ":" & m.LParam.ToString) End If MyBase.WndProc(m) End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles e.FormClosing Call UnregisterHotKey(Me.Handle.toint32, 0) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Call RegisterHotKey(Me.Handle.toint32, 0, 0, 27) End Sub End Class
Thanks
Last edited by Icyculyr; Jan 28th, 2008 at 02:05 AM.
-
Jan 28th, 2008, 07:19 AM
#2
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.
-
Jan 29th, 2008, 04:38 AM
#3
Thread Starter
Frenzied Member
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
-
Jan 29th, 2008, 05:38 AM
#4
Thread Starter
Frenzied Member
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:
' register a global hot key
Public Sub RegisterGlobalHotKey(ByVal hotkey As Keys, ByVal modifiers As Integer)
Try
' use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs
Dim atomName As String = AppDomain.GetCurrentThreadId.ToString("X8") & Me.Name
hotkeyID = GlobalAddAtom(atomName)
If hotkeyID = 0 Then
Throw New Exception("Unable to generate unique hotkey ID. Error code: " & _
Marshal.GetLastWin32Error().ToString)
End If
' register the hotkey, throw if any error
If RegisterHotKey(Me.Handle, hotkeyID, modifiers, CInt(hotkey)) = 0 Then
Throw New Exception("Unable to register hotkey. Error code: " & _
Marshal.GetLastWin32Error.ToString)
End If
Catch ex As Exception
' clean up if hotkey registration failed
UnregisterGlobalHotKey()
End Try
End Sub
Whole Code:
vb.net Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Declare Function RegisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer, _
ByVal fsModifiers As Integer, ByVal vk As Integer) As Integer
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As IntPtr, ByVal id As Integer) _
As Integer
Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As _
String) As Short
Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Short) As Short
Private Const MOD_ALT As Integer = 1
Private Const MOD_CONTROL As Integer = 2
Private Const MOD_SHIFT As Integer = 4
Private Const MOD_WIN As Integer = 8
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
' register the Shift+Ctrl+F6 hot key
RegisterGlobalHotKey(Keys.F6, MOD_SHIFT Or MOD_CONTROL)
End Sub
Private Sub Form1_Closed(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Closed
' unregister the hotkey (NEVER FORGET THIS!)
UnregisterGlobalHotKey()
End Sub
' unregister a global hotkey
Public Sub UnregisterGlobalHotKey()
If Me.hotkeyID <> 0 Then
UnregisterHotKey(Me.Handle, hotkeyID)
' clean up the atom list
GlobalDeleteAtom(hotkeyID)
hotkeyID = 0
End If
End Sub
' the id for the hotkey
Dim hotkeyID As Short
' register a global hot key
Public Sub RegisterGlobalHotKey(ByVal hotkey As Keys, ByVal modifiers As Integer)
Try
' use the GlobalAddAtom API to get a unique ID (as suggested by MSDN docs
Dim atomName As String = AppDomain.GetCurrentThreadId.ToString("X8") & Me.Name
hotkeyID = GlobalAddAtom(atomName)
If hotkeyID = 0 Then
Throw New Exception("Unable to generate unique hotkey ID. Error code: " & _
Marshal.GetLastWin32Error().ToString)
End If
' register the hotkey, throw if any error
If RegisterHotKey(Me.Handle, hotkeyID, modifiers, CInt(hotkey)) = 0 Then
Throw New Exception("Unable to register hotkey. Error code: " & _
Marshal.GetLastWin32Error.ToString)
End If
Catch ex As Exception
' clean up if hotkey registration failed
UnregisterGlobalHotKey()
End Try
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
' let the base class process the message
MyBase.WndProc(m)
' if this is a WM_HOTKEY message, notify the parent object
Const WM_HOTKEY As Integer = &H312
If m.Msg = WM_HOTKEY Then
' do whatever you wish to do when the hotkey is pressed
' in this example we activate the form and display a messagebox
Me.WindowState = FormWindowState.Normal
Me.Activate()
End If
End Sub
End Class
Thanks
-
Jan 29th, 2008, 06:09 AM
#5
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.
-
Jan 29th, 2008, 03:22 PM
#6
Thread Starter
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|