473,287 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Context menus and keyboard hooks?

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
2 5374
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Don Riesbeck Jr. | last post by:
I'm working on an application (OEM) using C# that utilizes input from a keyboard, and USB Barcode Scanner. The scanner is a HID Keyboard device, and input from it is sent to the system as if it...
1
by: Jigar mehta | last post by:
Hye, How to create systemwide hooks for keyboard for keys like WIN, ALT+TAB, CTRL+ALT+DEL even when the application does not have the focus on it... Thanks,
8
by: Dennis C. Drumm | last post by:
Is there a way to modify the standard context menu shown when someone right clicks in a windows text box and that would work for all open windows applications? The standard context menu for...
2
by: genojoe | last post by:
I have a Windows form application running that contains a button and textbox. An easy thing to do is to monitor the last time a keystroke was executed in the textbox. I can then click the button...
7
by: jpierson | last post by:
Hi, I am tryin to create a keyboard hook that sends the keystroke ctrl + pause/break. I haven't used keyboard hooks before so I'm not too sure how to use them public int MyKeyboardProc(int...
13
by: Hema | last post by:
hello all, I am working on a project related to Internet Explorer. I want my application to be invoked by a keypress( single key stroke or a combination). But this must get invoked only when the...
0
by: Hema | last post by:
Hi all, I have developed a BHO that installs low level keybaord hooks and CBT hooks globally whenever IE is opened.The keys that are pressed when IE is active are to be captured.I am trying to...
9
by: Kbalz | last post by:
I have an application that minimizes itself, and I want it to listen for certain key commands, and when they are pressed, my program can react to them. So far I've gotten my application to react...
0
by: Studlyami | last post by:
I have a simple application which includes a default window (named Window1) with no additional xaml. Window1 consists of a timerclass object which contains a timer running at 60hrz. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.