Problem with the keyboard hooks

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2006
Posts: 1
Reputation: hemapriya is an unknown quantity at this point 
Solved Threads: 0
hemapriya hemapriya is offline Offline
Newbie Poster

Problem with the keyboard hooks

 
0
  #1
Apr 8th, 2006
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:

  1. #region Hook Variables
  2.  
  3. private delegate int KeyboardHookProcDelegate(int nCode, int wParam,ref int lParam);
  4.  
  5. [MarshalAs(UnmanagedType.FunctionPtr)]
  6. private KeyboardHookProcDelegate kbcatcher;
  7.  
  8. public struct KeyboardHookStruct
  9. {
  10. public int vkCode;
  11. public int scanCode;
  12. public int flags;
  13. public int time;
  14. public int dwExtraInfo;
  15.  
  16. };
  17. private const int HC_ACTION = 0;
  18. private const int LLKHF_EXTENDED = 0x01;
  19. private const int LLKHF_ALTDOWN = 0x20;
  20. private const int VK_T = 0x54;
  21. private const int VK_P = 0x50;
  22. private const int VK_W = 0x57;
  23. private const int VK_TAB = 0x9;
  24. private const int VK_CONTROL = 0x11; private const int VK_ESCAPE = 0x1B;
  25.  
  26. private const int WH_KEYBOARD_LL=13;
  27.  
  28. private IntPtr KeyboardHandle = IntPtr.Zero;
  29. private static int mHook=0;
  30. #endregion
  31.  
  32. public static int KeyboardHookProc(int nCode, int wParam,ref int lParam)
  33. {
  34. KeyboardHookStruct HookStruct;
  35. int ret = 0;
  36. long vkCode=0;
  37. int flag=0;
  38. MessageBox.Show("inside HookProc");//S
  39. IntPtr pAlloc=IntPtr.Zero;
  40.  
  41. #region PtrStructure
  42. try
  43. {
  44. MessageBox.Show("Value of lParam:"+lParam.ToString()+"\nValue of wParam:"+wParam.ToString());//S
  45. try
  46. {
  47. pAlloc=Marshal.AllocHGlobal(Marshal.SizeOf(ret)*5);
  48. if(pAlloc!=IntPtr.Zero)
  49. {
  50. MessageBox.Show("Allocation done");
  51. }
  52. else
  53. MessageBox.Show("no allocation");
  54. }
  55. catch(Exception e)
  56. {
  57. MessageBox.Show(e.ToString());//S
  58. }
  59.  
  60. //IntPtr pDest=pAlloc;
  61. try
  62. {
  63. CopyMemory(pAlloc,lParam,Marshal.SizeOf(ret)*5);
  64. MessageBox.Show("memory copied");
  65. }
  66. catch(Exception e)
  67. {
  68. MessageBox.Show(e.ToString());//S
  69. }
  70. try
  71. {
  72. HookStruct = ((KeyboardHookStruct) Marshal.PtrToStructure(pAlloc, typeof(KeyboardHookStruct)));
  73. MessageBox.Show("conversion done");//S
  74. vkCode= HookStruct.vkCode;
  75. flag = HookStruct.flags;
  76. MessageBox.Show("Value of vkCode:"+vkCode.ToString()+"\nValue of flag:"+flag.ToString());//S
  77. }
  78. catch(Exception e)
  79. {
  80. MessageBox.Show(e.ToString());//S
  81. }
  82.  
  83. }
  84. catch(Exception e)
  85. {
  86. MessageBox.Show(e.ToString());//S
  87. }
  88. finally
  89. {
  90. Marshal.FreeHGlobal(pAlloc);
  91. pAlloc=IntPtr.Zero;
  92. }
  93. #endregion
  94.  
  95. #region KeyCheck
  96. if(nCode==HC_ACTION)
  97. {
  98. MessageBox.Show("inside nCode==HC_ACTION");//S
  99. //System.Windows.Forms.Keys KeyPressed = (Keys)wParam.ToInt32();
  100.  
  101. // Insert + T OR Alt + T
  102. if(vkCode == VK_T )
  103. {
  104. MessageBox.Show(" found T ");
  105. if((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0)
  106. {
  107. MessageBox.Show("Play");
  108. ret=1;
  109. }
  110. else if((flag & LLKHF_ALTDOWN)!=0)
  111. {
  112. MessageBox.Show("Stop");
  113. ret=1;
  114. }
  115. else
  116. MessageBox.Show("Neither Ctrl nor Alt");
  117. }
  118. else if(vkCode == VK_P)
  119. {
  120. MessageBox.Show(" found P ");
  121. if((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0)
  122. { //Insert + P
  123. MessageBox.Show("pause");
  124. ret = 1 ;
  125. }
  126. else
  127. MessageBox.Show("not ctrl");
  128. }
  129. else if(vkCode == VK_W)
  130. {
  131. MessageBox.Show(" found W ");
  132. if((GetAsyncKeyState(VK_CONTROL) & 0x8000) != 0)
  133. {
  134. // Alt + S
  135. MessageBox.Show("Save");
  136. ret = 1 ;
  137. }
  138. }
  139. else
  140. MessageBox.Show("none of the conditions satisfied");
  141.  
  142. /*/ CopyMemory(HookStruct,lParam,sizeof(HookStruct);
  143. if(IsHooked(HookStruct))
  144. {
  145. MyKeyboardProc=1;
  146. }*/
  147.  
  148. // MyKeyboardProc=1;
  149. }
  150. #endregion
  151.  
  152. //#region CallNextHookProc call
  153. if( ret == 0 )
  154. {
  155. ret = CallNextHookEx(mHook,nCode,wParam,lParam);
  156. }
  157. // #endregion
  158. return ret;
  159. }
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 4
Reputation: asm4pic is an unknown quantity at this point 
Solved Threads: 0
asm4pic asm4pic is offline Offline
Newbie Poster

Re: Problem with the keyboard hooks

 
1
  #2
Jul 29th, 2006
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/de...abouthooks.asp

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

Ahmed Samieh
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 233
Reputation: Lord Soth is an unknown quantity at this point 
Solved Threads: 4
Lord Soth's Avatar
Lord Soth Lord Soth is offline Offline
Posting Whiz in Training

Re: Problem with the keyboard hooks

 
0
  #3
Aug 5th, 2006
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
Best regards,
Loren Soth

Crimson K. Software _________________________________________________________________ Crimson K. Blog
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum


Views: 7729 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC