Context menus and keyboard hooks?

  • Thread starter =?ISO-8859-1?Q?Christoph_Br=FCser?=
  • Start date
?

=?ISO-8859-1?Q?Christoph_Br=FCser?=

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
 
J

John Puopolo

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
 
?

=?ISO-8859-1?Q?Christoph_Br=FCser?=

John said:
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.
 
?

=?ISO-8859-1?Q?Christoph_Br=FCser?=

I did some further testing, and it seems that an application always
crashes when you have a hook installed and receive an event through that
hook while a context menu is showing. Very weird. I hope somebody can
tell me what is happening here.

Thanks again,
Christoph Brüser
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top