| | |
Problem with the keyboard hooks
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Apr 2006
Posts: 1
Reputation:
Solved Threads: 0
hi all,
I am a student from India.I am developing an application that is a BHO that hooks the keyboard. I aiming to invoke some functionalities when some specific keys are pressed. Have the developed the code.Though my keyboard hook is getting installed, its not working! I found the reason to be some issue related to the place where i am getting a copy of the lParam of my HookProc and converting it to my KKHOOKSTRUCT structure. I dont know wats wrong in my code.It seems to be right. I am adding the hookproc and related declarations alone below:
The problem is that none of the keys are detected.The values for lParam and wParam are not the same. Some problem with the way the structure getting copied.
I have not used the wParam anywhere.Should i?
This is a global hook.Actually i want the hook only when IE window is in focus. How to do this?
Please help me to solve this.This is my project in C# and window hooks is very new to me.
Thanking u.
Regards,
hema
I am a student from India.I am developing an application that is a BHO that hooks the keyboard. I aiming to invoke some functionalities when some specific keys are pressed. Have the developed the code.Though my keyboard hook is getting installed, its not working! I found the reason to be some issue related to the place where i am getting a copy of the lParam of my HookProc and converting it to my KKHOOKSTRUCT structure. I dont know wats wrong in my code.It seems to be right. I am adding the hookproc and related declarations alone below:
C# Syntax (Toggle Plain Text)
#region Hook Variables private delegate int KeyboardHookProcDelegate(int nCode, int wParam,ref int lParam); [MarshalAs(UnmanagedType.FunctionPtr)] private KeyboardHookProcDelegate kbcatcher; public struct KeyboardHookStruct { public int vkCode; public int scanCode; public int flags; public int time; public int dwExtraInfo; }; private const int HC_ACTION = 0; private const int LLKHF_EXTENDED = 0x01; private const int LLKHF_ALTDOWN = 0x20; private const int VK_T = 0x54; private const int VK_P = 0x50; private const int VK_W = 0x57; private const int VK_TAB = 0x9; private const int VK_CONTROL = 0x11; private const int VK_ESCAPE = 0x1B; private const int WH_KEYBOARD_LL=13; private IntPtr KeyboardHandle = IntPtr.Zero; private static int mHook=0; #endregion public static int KeyboardHookProc(int nCode, int wParam,ref int lParam) { KeyboardHookStruct HookStruct; int ret = 0; long vkCode=0; int flag=0; MessageBox.Show("inside HookProc");//S IntPtr pAlloc=IntPtr.Zero; #region PtrStructure try { MessageBox.Show("Value of lParam:"+lParam.ToString()+"\nValue of wParam:"+wParam.ToString());//S try { pAlloc=Marshal.AllocHGlobal(Marshal.SizeOf(ret)*5); if(pAlloc!=IntPtr.Zero) { MessageBox.Show("Allocation done"); } else MessageBox.Show("no allocation"); } catch(Exception e) { MessageBox.Show(e.ToString());//S } //IntPtr pDest=pAlloc; try { CopyMemory(pAlloc,lParam,Marshal.SizeOf(ret)*5); MessageBox.Show("memory copied"); } catch(Exception e) { MessageBox.Show(e.ToString());//S } try { HookStruct = ((KeyboardHookStruct) Marshal.PtrToStructure(pAlloc, typeof(KeyboardHookStruct))); MessageBox.Show("conversion done");//S vkCode= HookStruct.vkCode; flag = HookStruct.flags; MessageBox.Show("Value of vkCode:"+vkCode.ToString()+"\nValue of flag:"+flag.ToString());//S } catch(Exception e) { MessageBox.Show(e.ToString());//S } } catch(Exception e) { MessageBox.Show(e.ToString());//S } finally { Marshal.FreeHGlobal(pAlloc); pAlloc=IntPtr.Zero; } #endregion #region KeyCheck if(nCode==HC_ACTION) { MessageBox.Show("inside nCode==HC_ACTION");//S //System.Windows.Forms.Keys KeyPressed = (Keys)wParam.ToInt32(); // Insert + T OR Alt + T if(vkCode == VK_T ) { MessageBox.Show(" found T "); if((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0) { MessageBox.Show("Play"); ret=1; } else if((flag & LLKHF_ALTDOWN)!=0) { MessageBox.Show("Stop"); ret=1; } else MessageBox.Show("Neither Ctrl nor Alt"); } else if(vkCode == VK_P) { MessageBox.Show(" found P "); if((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0) { //Insert + P MessageBox.Show("pause"); ret = 1 ; } else MessageBox.Show("not ctrl"); } else if(vkCode == VK_W) { MessageBox.Show(" found W "); if((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0) { // Alt + S MessageBox.Show("Save"); ret = 1 ; } } else MessageBox.Show("none of the conditions satisfied"); /*/ CopyMemory(HookStruct,lParam,sizeof(HookStruct); if(IsHooked(HookStruct)) { MyKeyboardProc=1; }*/ // MyKeyboardProc=1; } #endregion //#region CallNextHookProc call if( ret == 0 ) { ret = CallNextHookEx(mHook,nCode,wParam,lParam); } // #endregion return ret; }
I have not used the wParam anywhere.Should i?
This is a global hook.Actually i want the hook only when IE window is in focus. How to do this?
Please help me to solve this.This is my project in C# and window hooks is very new to me.
Thanking u.
Regards,
hema
•
•
Join Date: Jul 2006
Posts: 4
Reputation:
Solved Threads: 0
hi hema, did u read the documentation of Hooking in MSDN
http://msdn.microsoft.com/library/de...abouthooks.asp
you can get more information from this example
http://www.codeproject.com/csharp/gl...mhook.asp#xxxx
Ahmed Samieh
•
•
•
•
A global hook monitors messages for all threads in the same desktop as the calling thread. A thread-specific hook monitors messages for only an individual thread. A global hook procedure can be called in the context of any application in the same desktop as the calling thread, so the procedure must be in a separate dynamic-link library (DLL) module. A thread-specific hook procedure is called only in the context of the associated thread. If an application installs a hook procedure for one of its own threads, the hook procedure can be in either the same module as the rest of the application's code or in a DLL. If the application installs a hook procedure for a thread of a different application, the procedure must be in a DLL
you can get more information from this example
http://www.codeproject.com/csharp/gl...mhook.asp#xxxx
Ahmed Samieh
Hi,
I doubt that you can create that separate hook dll with managed code but you can create the app managed and the dll native code (C++, Delphi).
Loren Soth
I doubt that you can create that separate hook dll with managed code but you can create the app managed and the dll native code (C++, Delphi).
Loren Soth
Best regards,
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
Loren Soth
Crimson K. Software _________________________________________________________________ Crimson K. Blog
![]() |
Similar Threads
- Keyboard Problem (USB Devices and other Peripherals)
- Wierd keyboard problem (USB Devices and other Peripherals)
- PS2 Keyboard problem in XP (USB Devices and other Peripherals)
- Will Keyboard Hooks solve my problem? (C++)
- Mouse hesitation problem fixed - virus (Windows 95 / 98 / Me)
- Can't get multimedia keys to work on mac keyboard (Apple Hardware)
- Windows ME Keyboard lock up (Windows 95 / 98 / Me)
Other Threads in the C# Forum
- Previous Thread: need to install MSDE through C# coding
- Next Thread: aspx page not loading!!!! Method called too soon
Views: 7729 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gcd gdi+ http httpwebrequest image index input java list listbox listener login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml





