By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,953 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,953 IT Pros & Developers. It's quick & easy.

Context menus and keyboard hooks?

P: n/a
Hi,

in my application I want to react to certain keys when a context menu
is showing. So I installed a keyboard hook, but now whenever a key is
pressed when the menu is showing, the application crashes and sometimes
throws a NullReferenceException in system.windows.forms.dll. I assume
that the hook somehow messes up the context menu's message loop or
something like this. I also had this problem with a mouse hook, so i
guess its a problem with hooks in general. But I have no idea how to
work around it. My code goes like this:

<code>
....

private int keyHook = 0;
private delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

//Import for SetWindowsHookEx function.
//Use this function to install thread-specific hook.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
IntPtr hInstance, int threadId);

//Import for CallNextHookEx.
//Use this function to pass the hook information to next hook procedure
in chain.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
private static extern int CallNextHookEx(int idHook, int nCode, IntPtr
wParam, IntPtr lParam);

//Hook the keyboard and show the menu
private void label1_Click(object sender, System.EventArgs e)
{
if(keyHook == 0)
{
keyHook = SetWindowsHookEx(WH_KEYBOARD, new
HookProc(KeyHookProc), IntPtr.Zero, AppDomain.GetCurrentThreadId());
}
contextMenu1.Show( label1, new Point(0, label1.Height) );
}

private int KeyHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
//my handler code would be here
}
return CallNextHookEx(keyHook, nCode, wParam, lParam);
}

....
</code>

Any help is greatly appreciated.

Thanks
Christoph Brüser
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi...

Are there mulitple threads involved?
If you are manipulating UI controls from another thread in response to the
hook, your application can crash or exhibit strange behaviour -- Windows
controls must be accessed on the thread that created them (you can use
Contol.Invoke()) to do this.

John Puopolo

"Christoph Brüser" <ch***************@rwth-aachen.de> wrote in message
news:3b*************@news.dfncis.de...
Hi,

in my application I want to react to certain keys when a context menu
is showing. So I installed a keyboard hook, but now whenever a key is
pressed when the menu is showing, the application crashes and sometimes
throws a NullReferenceException in system.windows.forms.dll. I assume
that the hook somehow messes up the context menu's message loop or
something like this. I also had this problem with a mouse hook, so i
guess its a problem with hooks in general. But I have no idea how to
work around it. My code goes like this:

<code>
...

private int keyHook = 0;
private delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

//Import for SetWindowsHookEx function.
//Use this function to install thread-specific hook.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn,
IntPtr hInstance, int threadId);

//Import for CallNextHookEx.
//Use this function to pass the hook information to next hook procedure
in chain.
[DllImport("user32.dll",CharSet=CharSet.Auto,
CallingConvention=CallingConvention.StdCall)]
private static extern int CallNextHookEx(int idHook, int nCode, IntPtr
wParam, IntPtr lParam);

//Hook the keyboard and show the menu
private void label1_Click(object sender, System.EventArgs e)
{
if(keyHook == 0)
{
keyHook = SetWindowsHookEx(WH_KEYBOARD, new
HookProc(KeyHookProc), IntPtr.Zero, AppDomain.GetCurrentThreadId());
}
contextMenu1.Show( label1, new Point(0, label1.Height) );
}

private int KeyHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
//my handler code would be here
}
return CallNextHookEx(keyHook, nCode, wParam, lParam);
}

...
</code>

Any help is greatly appreciated.

Thanks
Christoph Brüser

Nov 17 '05 #2

P: n/a
John Puopolo wrote:
Are there mulitple threads involved?


Nope, at least no threads that I created. And I dont even manipulate the
UI, I just listen to keyboard events via a hook. But I believe there
might be some internal threading issue. But I have no idea how to work
around it.
Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.