Low level Keyboard Hook in C#
I'm making a module that will control and move pan and tilt device via KEYBOARD. The module is a C# .DLL and being loaded dynamiclall开发者_运维知识库y via other module using reflection. The module CAN NOT have any form (visible).
I'm using a code that I found on the http://blogs.msdn.com/b/toub/archive/2006/05/03/589423.aspx for a low level kb hook. The only problem that is it uses consol application and calls Application.Run() to take care of a message loop, which is required for hooks to work properly. Since I'm having a .dll and not a console application, I can not make it to work and catch the keys pressed.
The question: How can I replace the call for Application.Run() to take care of a message loop in the .dll to catch the KB hooks?
Thx!!!!
For a message-loop there is no need to have Console or GUI - it can remain hidden, and intercept messages.
Your DLL should use the message loop from the program loading it. However if that is not possible you can have your DLL start a new thread and call Application.Run() from inside that thread and use it's own message loop.
public static void InjectionPoint()
{
Thread thread = new Thread(new ThreadStart(DLLMessageLoop));
thread.IsBackground = true;
thread.Start();
}
public static void DLLMessageLoop()
{
_hookID = SetHook(_proc);
Application.Run();
UnhookWindowsHookEx(_hookID);
}
精彩评论