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:

#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;
		}

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

hi hema, did u read the documentation of Hooking in MSDN

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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/hooks/abouthooks.asp

you can get more information from this example
http://www.codeproject.com/csharp/globalsystemhook.asp#xxxx

Ahmed Samieh

commented: sound advice +9

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.