?
=?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
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