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