XTPKeyboardManager.cpp
上传用户:szled88
上传日期:2015-04-09
资源大小:43957k
文件大小:5k
源码类别:

对话框与窗口

开发平台:

Visual C++

  1. // XTPHookManager.cpp : implementation of the CXTPHookManager class.
  2. //
  3. // This file is a part of the XTREME COMMANDBARS MFC class library.
  4. // (c)1998-2008 Codejock Software, All Rights Reserved.
  5. //
  6. // THIS SOURCE FILE IS THE PROPERTY OF CODEJOCK SOFTWARE AND IS NOT TO BE
  7. // RE-DISTRIBUTED BY ANY MEANS WHATSOEVER WITHOUT THE EXPRESSED WRITTEN
  8. // CONSENT OF CODEJOCK SOFTWARE.
  9. //
  10. // THIS SOURCE CODE CAN ONLY BE USED UNDER THE TERMS AND CONDITIONS OUTLINED
  11. // IN THE XTREME TOOLKIT PRO LICENSE AGREEMENT. CODEJOCK SOFTWARE GRANTS TO
  12. // YOU (ONE SOFTWARE DEVELOPER) THE LIMITED RIGHT TO USE THIS SOFTWARE ON A
  13. // SINGLE COMPUTER.
  14. //
  15. // CONTACT INFORMATION:
  16. // support@codejock.com
  17. // http://www.codejock.com
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. #include "stdafx.h"
  21. #include "XTPToolBar.h"
  22. #include "XTPKeyboardManager.h"
  23. #include "XTPMouseManager.h"
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29. //////////////////////////////////////////////////////////////////////////
  30. // CXTPKeyboardManager
  31. CThreadLocal<CXTPKeyboardManager> CXTPKeyboardManager::_xtpKeyboardThreadState;
  32. CXTPKeyboardManager::CXTPKeyboardManager()
  33. {
  34. m_hHookKeyboard = 0;
  35. m_hHookCallWndProc = 0;
  36. m_hHookGetMessage = 0;
  37. #ifdef _AFXDLL
  38. m_pModuleState = 0;
  39. #endif
  40. }
  41. void CXTPKeyboardManager::SetupKeyboardHook(BOOL bSetup)
  42. {
  43. if (bSetup && m_hHookKeyboard == 0)
  44. {
  45. m_hHookKeyboard = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, AfxGetInstanceHandle(), GetCurrentThreadId());
  46. }
  47. if (!bSetup && m_hHookKeyboard)
  48. {
  49. UnhookWindowsHookEx(m_hHookKeyboard);
  50. m_hHookKeyboard = 0;
  51. }
  52. }
  53. void CXTPKeyboardManager::SetupCallWndProcHook(BOOL bSetup)
  54. {
  55. if (bSetup && m_hHookCallWndProc == 0)
  56. {
  57. m_hHookCallWndProc = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, AfxGetInstanceHandle(), GetCurrentThreadId());
  58. }
  59. if (!bSetup && m_hHookCallWndProc)
  60. {
  61. UnhookWindowsHookEx(m_hHookCallWndProc);
  62. m_hHookCallWndProc = 0;
  63. }
  64. }
  65. CXTPKeyboardManager::~CXTPKeyboardManager()
  66. {
  67. SetupKeyboardHook(FALSE);
  68. SetupCallWndProcHook(FALSE);
  69. }
  70. void CXTPKeyboardManager::HookKeyboard(CXTPHookManagerHookAble* pHook)
  71. {
  72. SetupKeyboardHook(TRUE);
  73. SetupCallWndProcHook(TRUE);
  74. #ifdef _AFXDLL
  75. m_pModuleState = AfxGetModuleState();
  76. #endif
  77. if (m_lstKeyboardHooks.Find(pHook) == NULL)
  78. {
  79. m_lstKeyboardHooks.AddTail(pHook);
  80. }
  81. }
  82. void CXTPKeyboardManager::UnhookKeyboard(CXTPHookManagerHookAble* pHook)
  83. {
  84. POSITION pos = m_lstKeyboardHooks.Find(pHook);
  85. if (pos != NULL)
  86. {
  87. m_lstKeyboardHooks.RemoveAt(pos);
  88. }
  89. if (m_lstKeyboardHooks.IsEmpty())
  90. {
  91. SetupCallWndProcHook(FALSE);
  92. SetupKeyboardHook(FALSE);
  93. }
  94. }
  95. BOOL CXTPKeyboardManager::ProcessKeyboardHooks(UINT nMessage, WPARAM wParam, LPARAM lParam)
  96. {
  97. POSITION pos = m_lstKeyboardHooks.GetTailPosition();
  98. while (pos)
  99. {
  100. CXTPHookManagerHookAble* pHook = m_lstKeyboardHooks.GetPrev(pos);
  101. LRESULT lResult = 0;
  102. BOOL bResult = pHook->OnHookMessage(0, nMessage, wParam, lParam, lResult);
  103. if (bResult)
  104. {
  105. return bResult != FALSE_EXIT;
  106. }
  107. }
  108. return TRUE;
  109. }
  110. LRESULT CALLBACK CXTPKeyboardManager::CallWndProc(int code, WPARAM wParam, LPARAM lParam)
  111. {
  112. CXTPKeyboardManager* pKeyboardManager = XTPKeyboardManager();
  113. CWPSTRUCT* pMSG = (CWPSTRUCT*)lParam;
  114. if (pMSG && pMSG->message == WM_ACTIVATEAPP && !pMSG->wParam)
  115. {
  116. #ifdef _AFXDLL
  117. AFX_MANAGE_STATE(pKeyboardManager->m_pModuleState);
  118. #endif
  119. XTPMouseManager()->SendTrackLost();
  120. }
  121. return CallNextHookEx(pKeyboardManager->m_hHookCallWndProc, code, wParam, lParam);
  122. }
  123. LRESULT CALLBACK CXTPKeyboardManager::KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
  124. {
  125. CXTPKeyboardManager* pKeyboardManager = XTPKeyboardManager();
  126. if (code != HC_ACTION)
  127. return CallNextHookEx(pKeyboardManager->m_hHookKeyboard, code, wParam, lParam);
  128. if (pKeyboardManager->IsKeyboardHooked() && (!(HIWORD(lParam) & KF_UP)))
  129. {
  130. SAFE_MANAGE_STATE(pKeyboardManager->m_pModuleState);
  131. if (!(HIWORD(lParam) & KF_ALTDOWN))
  132. {
  133. if (pKeyboardManager->ProcessKeyboardHooks(WM_KEYDOWN, wParam, lParam))
  134. {
  135. return TRUE;
  136. }
  137. }
  138. else
  139. {
  140. if (wParam == VK_MENU && !XTPMouseManager()->IsMouseLocked())
  141. {
  142. if (!(HIWORD(lParam) & KF_REPEAT))
  143. {
  144. XTPMouseManager()->SendTrackLost();
  145. return TRUE;
  146. }
  147. }
  148. else
  149. {
  150. if (pKeyboardManager->ProcessKeyboardHooks(WM_KEYDOWN, wParam, lParam))
  151. {
  152. return TRUE;
  153. }
  154. }
  155. }
  156. }
  157. return CallNextHookEx(pKeyboardManager->m_hHookKeyboard, code, wParam, lParam);
  158. }