Hook.cpp
上传用户:aokegd
上传日期:2009-12-14
资源大小:1276k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // Hook.cpp : Defines the initialization routines for the DLL.
  2. #include "stdafx.h"
  3. #include "hook.h"
  4. #include <windowsx.h>
  5. #ifdef _DEBUG
  6. #define new DEBUG_NEW
  7. #undef THIS_FILE
  8. static char THIS_FILE[] = __FILE__;
  9. #endif
  10. #define MAX_KEY 100
  11. #define CTRLBIT 0x04
  12. #define ALTBIT  0x02
  13. #define SHIFTBIT 0x01 
  14. /////////////////////////////////////////////////////////////////
  15. #pragma data_seg("shareddata")
  16. HHOOK hHook =NULL;
  17. UINT nHookCount =0;
  18. static UCHAR HotKey[MAX_KEY] = {0}; //hotkey
  19. static UCHAR HotKeyMask[MAX_KEY] = {0}; //flag for hotkey, value is VK_CONTRL or VK_NEMU or VK_SHIFT
  20. static HWND hCallWnd[MAX_KEY] = {0}; //window associated with hotkey
  21. static int KeyCount =0;
  22. static UCHAR MaskBits =0;    //00000 Ctrl Alt Shift
  23. #pragma data_seg()
  24. ////////////////////////////////////////////////////////////////
  25. HINSTANCE hins;
  26. void VerifyWindow();
  27. /////////////////////////////////////////////////////////////////////////////
  28. // CHookApp
  29. BEGIN_MESSAGE_MAP(CHookApp, CWinApp)
  30. //{{AFX_MSG_MAP(CHookApp)
  31. // NOTE - the ClassWizard will add and remove mapping macros here.
  32. //    DO NOT EDIT what you see in these blocks of generated code!
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CHookApp construction
  37. CHookApp::CHookApp()
  38. {
  39. // TODO: add construction code here,
  40. // Place all significant initialization in InitInstance
  41. }
  42. /////////////////////////////////////////////////////////////////////////////
  43. // The one and only CHookApp object
  44. CHookApp theApp;
  45. LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam,LPARAM lParam)
  46. {
  47. BOOL bProcessed=FALSE;
  48. if(HC_ACTION==nCode)
  49. {
  50. if((lParam&0xc0000000)==0xc0000000){// Key up
  51. switch(wParam)
  52. {
  53. case VK_MENU:
  54. MaskBits&=~ALTBIT;
  55. break;
  56. case VK_CONTROL:
  57. MaskBits&=~CTRLBIT;
  58. break;
  59. case VK_SHIFT:
  60. MaskBits&=~SHIFTBIT;
  61. break;
  62. default: //judge the key and send message
  63. break;
  64. }
  65. for(int index=0;index<MAX_KEY;index++){
  66. if(hCallWnd[index]==NULL)
  67. continue;
  68. if(IsWindow(hCallWnd[index])&&(HotKey[index]==wParam)&&(HotKeyMask[index]==MaskBits)){
  69. SendMessage(hCallWnd[index],WM_HOTKEY,wParam,WM_KEYUP);
  70. bProcessed=TRUE;
  71. }
  72. }
  73. }
  74. else if((lParam&0xc000ffff)==1){ //Key down
  75. switch(wParam)
  76. {
  77. case VK_MENU:
  78. MaskBits|=ALTBIT;
  79. break;
  80. case VK_CONTROL:
  81. MaskBits|=CTRLBIT;
  82. break;
  83. case VK_SHIFT:
  84. MaskBits|=SHIFTBIT;
  85. break;
  86. default: //judge the key and send message
  87. break;
  88. }
  89. for(int index=0;index<MAX_KEY;index++){
  90. if(hCallWnd[index]==NULL)
  91. continue;
  92. if(IsWindow(hCallWnd[index])&&(HotKey[index]==wParam)&&(HotKeyMask[index]==MaskBits)){
  93. SendMessage(hCallWnd[index],WM_HOTKEY,wParam,WM_KEYDOWN);
  94. bProcessed=TRUE;
  95. }
  96. }
  97. }
  98. if(!bProcessed){
  99. for(int index=0;index<MAX_KEY;index++){
  100. if(hCallWnd[index]==NULL)
  101. continue;
  102. if(IsWindow(hCallWnd[index])&&(HotKey[index]==0)&&(HotKeyMask[index]==0))
  103. SendMessage(hCallWnd[index],WM_HOTKEY,WM_HOTKEY,lParam);
  104. }
  105. }
  106. }
  107. return CallNextHookEx( hHook, nCode, wParam, lParam );
  108. }
  109. BOOL InitHotkey()
  110. {
  111. if(hHook!=NULL){
  112. nHookCount++;
  113. return TRUE;
  114. }
  115. else
  116. hHook=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,0);
  117. if(hHook!=NULL)
  118. nHookCount++;
  119. return (hHook!=NULL);
  120. }
  121. BOOL UnInit()
  122. {   
  123. if(nHookCount>1){
  124. nHookCount--;
  125. return TRUE;
  126. }
  127. BOOL unhooked = UnhookWindowsHookEx(hHook);
  128. if(unhooked==TRUE){
  129. nHookCount=0;
  130. hHook=NULL;
  131. }
  132. return unhooked;
  133. BOOL __declspec(dllexport) __stdcall AddHotkey(HWND hWnd,UCHAR cKey,UCHAR cMask)
  134. {
  135. BOOL bAdded=FALSE;
  136. for(int index=0;index<MAX_KEY;index++){
  137. if(hCallWnd[index]==0){
  138. hCallWnd[index]=hWnd;
  139. HotKey[index]=cKey;
  140. HotKeyMask[index]=cMask;
  141. bAdded=TRUE;
  142. KeyCount++;
  143. break;
  144. }
  145. }
  146. return bAdded;
  147. }
  148. BOOL __declspec(dllexport) __stdcall DeleteHotkey(HWND hWnd,UCHAR cKey,UCHAR cMask)
  149. {
  150. BOOL bRemoved=FALSE;
  151. for(int index=0;index<MAX_KEY;index++){
  152. if(hCallWnd[index]==hWnd){
  153. if(HotKey[index]==cKey&&HotKeyMask[index]==cMask){
  154. hCallWnd[index]=NULL;
  155. HotKey[index]=0;
  156. HotKeyMask[index]=0;
  157. bRemoved=TRUE;
  158. KeyCount--;
  159. break;
  160. }
  161. }
  162. }
  163. return bRemoved;
  164. }
  165. void VerifyWindow()
  166. {
  167. for(int i=0;i<MAX_KEY;i++){
  168. if(hCallWnd[i]!=NULL){
  169. if(!IsWindow(hCallWnd[i])){
  170. hCallWnd[i]=NULL;
  171. HotKey[i]=0;
  172. HotKeyMask[i]=0;
  173. KeyCount--;
  174. }
  175. }
  176. }
  177. }
  178. BOOL CHookApp::InitInstance() 
  179. {
  180. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  181. hins=AfxGetInstanceHandle();
  182. InitHotkey();
  183. return CWinApp::InitInstance();
  184. }
  185. int CHookApp::ExitInstance() 
  186. {
  187. VerifyWindow();
  188. UnInit();
  189. return CWinApp::ExitInstance();
  190. }