473,396 Members | 1,816 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,396 software developers and data experts.

hooking keyboard input twice

Hi,
I'm building a program that at some point needs to hook the keyboard input to prevent the user from pressing some key combinations. Then, at a later point I need to unhook the keyboard, and then, I need to hook again the keyboard.

I succeed in hooking the keyboard for the first time and then unhhok the keyboard, but when I try to hook it again using the same code, it seems that the hooking-method is not called at all, although 'SetWindowsHookEx' method succeeds.

Here is how I do it:

First, declarations:

[DllImport("user32", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, SetLastError = true, ExactSpelling = true)]
private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);

[DllImport("user32", EntryPoint = "UnhookWindowsHookEx", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, SetLastError = true, ExactSpelling = true)]
private static extern int UnhookWindowsHookEx(int hHook);

[DllImport("user32", EntryPoint = "CallNextHookEx", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int CallNextHookEx(int hHook, int nCode, int wParam, IntPtr lParam);

private static int LowLevelKeyboardProc(int nCode, int wParam, IntPtr lParam)
{
...
return CallNextHookEx(intLLKeyboard, nCode, wParam, lParam);
}

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

private static HookProc KeyboardDelegate = null; //Static member

private static int intLLKeyboard = 0; //Static member



Then, setting the hook:

KeyboardDelegate = new HookProc(LowLevelKeyboardProc);

System.Reflection.Module[] modules = System.Reflection.Assembly.GetExecutingAssembly(). GetModules();
intLLKeyboard = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANC E(modules[0]), 0);



Later, unhooking :

UnhookWindowsHookEx(intLLKeyboard);




Then, trying to hook again :

KeyboardDelegate = new HookProc(LowLevelKeyboardProc);

System.Reflection.Module[] modules = System.Reflection.Assembly.GetExecutingAssembly(). GetModules();
intLLKeyboard = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardDelegate, System.Runtime.InteropServices.Marshal.GetHINSTANC E(modules[0]), 0);


At this point 'intLLKeyboard' is not null, but nevertheless, it seems that the hooking procedure isn't called at all.
Nov 15 '11 #1
0 1762

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Claudio Grondi | last post by:
German Windows 2000, SP 4 Python 2.3.4 (#53, May 25 2004, 21:17:02) IPython 0.6.10 -- An enhanced Interactive Python. Is it already known, that after switching the keyboard input scheme on...
0
by: Ray | last post by:
I have English Windows XP Pro and Office 2003 Pro on my computer. When I enter data into fields of tables, queries and forms of Access 2003, it automatically switches to Chinese keyboard input. ...
4
by: Ralf Toender | last post by:
Hi! Does anybody know how to send a keystroke C#? In C++ it's done by: keybd_event ( ... ) or SendInput ( ... ). What namespace does include this counterpart? Thanks Ralf
3
by: FabFreddy | last post by:
Hello, I'm writing a ASP.NET application. A applicatie that first was written in Basic and now i'm writting it in VB.NET, but I have one huge problem. I don't know how to get keyboard input. The...
0
by: Freddy | last post by:
In my Access db (which I have created to help me learn Greek) I am using text box controls on a form. Some of these controls require English input and some require Greek input. Currently I have...
2
by: pelletier.thomas | last post by:
Hello everybody. I'm trying to code a very little OS. But I have a problem with the keyboard input: It show thechar + "~" :s Can you help me ? The source is there :...
0
by: Srin | last post by:
Hi, I'm in the process of writing a small C# App, in which i have two groupboxes in a form. The form has the KeyPreview property set to true. The groupbox1 contains a set of labels. The second...
8
by: RJ45 | last post by:
Hello, I am writing a shell in C. I need to intercept Signals like CTRL+C or CTRL+D and set to ignore them. This is on Unix, using gcc. my goal is to avoid users escaping the shell with SIGINT...
3
by: bwgames | last post by:
Hi there, I have a script that takes two inputs from the keyboard to change some settings on some equipment. I would usually run this as e.g. ben@srv001:/$ change-settings<enter> new-setting...
3
by: =?Utf-8?B?cHJvZ2dlcg==?= | last post by:
I have a C# application that hosts an AxWebBrowser control which I automate by sending mouse clicks and keyboard input. I have had various problems in doing this due to a bug in the AxWebBrowser...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.