473,322 Members | 1,259 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,322 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 5380
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. ...
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
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.