Threads and KeyBoard Hook
-
Hi all, i am using a clasic low-level hook for the keyboard and runing it in the main class: kBHook = new KeyBoardHook(); kBHook.KeyPress+=new KeyPressEventHandler(kBHook_KeyPress);/// kBHook.KeyDown+=new KeyEventHandler(kBHook_KeyDown); kBHook.KeyUp+=new KeyEventHandler(kBHook_KeyUp); so KeyBoardHook is another class on my project. it happens that when my program is busy doing calculations the keyboard class misses KEYUP/KEYDOWN messages. so my questions are: 1. how can i make another thread that always listen to messages? 2. can i try to hook to keyboard in a way that i will always get the messages (i need my program to get those msgs before others) Thank you,
R.Z
-
Hi all, i am using a clasic low-level hook for the keyboard and runing it in the main class: kBHook = new KeyBoardHook(); kBHook.KeyPress+=new KeyPressEventHandler(kBHook_KeyPress);/// kBHook.KeyDown+=new KeyEventHandler(kBHook_KeyDown); kBHook.KeyUp+=new KeyEventHandler(kBHook_KeyUp); so KeyBoardHook is another class on my project. it happens that when my program is busy doing calculations the keyboard class misses KEYUP/KEYDOWN messages. so my questions are: 1. how can i make another thread that always listen to messages? 2. can i try to hook to keyboard in a way that i will always get the messages (i need my program to get those msgs before others) Thank you,
R.Z
I'm not sure about #2, but for #1, the only thing you'll most likely need is to set the apartment state of the thread to STA. You can do this using the SetApartmentState method on the Thread class.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
I'm not sure about #2, but for #1, the only thing you'll most likely need is to set the apartment state of the thread to STA. You can do this using the SetApartmentState method on the Thread class.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Hi, thanks for quick reply. i don't start my program as a Thread t=new Thread... the program's thread starts like this (and it is STA) : [STAThread] static void Main() { Application.Run(new Form1()); } any ideas?
R.Z
Sounds like the keyboard hook class is not functional. couple things to check: if you're using native Win32 hooks, I assume you have a callback function? Well, your delegate to that function must be manually kept alive, IRRC. Aside from that, all I can suggest is narrowing the problem down: are you sure it only occurs after work is occurring? Are the events not being sent at all? what happens after the work is completed, do the hooks resume working?
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
-
Sounds like the keyboard hook class is not functional. couple things to check: if you're using native Win32 hooks, I assume you have a callback function? Well, your delegate to that function must be manually kept alive, IRRC. Aside from that, all I can suggest is narrowing the problem down: are you sure it only occurs after work is occurring? Are the events not being sent at all? what happens after the work is completed, do the hooks resume working?
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango
my keyboard class is supposed to prevent keys from being sent to the current active window. when there is no heavy work on my program it blocks all chars and Notepad do not show any chars. when i write (for testing) Thread.Sleep(1000) in the KeyDown event in the main program , Notepad then display the characters even that i blocked them (return 1 on the hook). i have seen then even if my Hook gets the message later, the Notepad already displaying the char. this is the main core of the listening: KeyboardHookProcedure = new HookProc(KeyboardHookProc); hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0]),0); the SetWindowsHookEx defined like that: [DllImport("user32.dll", harSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall, etLastError=true)] private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); Whats is IRRC and how do i do that? Thanks,
R.Z
-
my keyboard class is supposed to prevent keys from being sent to the current active window. when there is no heavy work on my program it blocks all chars and Notepad do not show any chars. when i write (for testing) Thread.Sleep(1000) in the KeyDown event in the main program , Notepad then display the characters even that i blocked them (return 1 on the hook). i have seen then even if my Hook gets the message later, the Notepad already displaying the char. this is the main core of the listening: KeyboardHookProcedure = new HookProc(KeyboardHookProc); hKeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL, KeyboardHookProcedure, Marshal.GetHINSTANCE( Assembly.GetExecutingAssembly().GetModules()[0]),0); the SetWindowsHookEx defined like that: [DllImport("user32.dll", harSet=CharSet.Auto,CallingConvention=CallingConvention.StdCall, etLastError=true)] private static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId); Whats is IRRC and how do i do that? Thanks,
R.Z
IIRC is "if I recall correctly". :) When you pass a delegate instance in for the lpfn parameter of SetWindowsHookEx, make that delegate be a field in your class so that it doesn't get disposed; as I recall, since the object is being passed into a native method, then never used again, the garbage collector cleans up the delegate, causing your hook to fail. So one thing to check is make sure the delegate you pass into SetWindowsHookEx stays alive. Windows may have some mechanism so that if the process eating the keyboard is not responding (i.e. busy do to lots of work) it may send out the key events to other processes. Can you make your work occur on another thread, thus keeping the hook procedure thread free? That'd be ideal.
Tech, life, family, faith: Give me a visit. I'm currently blogging about: God-as-Judge, God-as-Forgiver The apostle Paul, modernly speaking: Epistles of Paul Judah Himango