focusdll.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <windows.h>
  2. #include <assert.h>
  3. #include "focusdll.h"
  4. BYTE GetKeyEventType(LPARAM lParam);
  5. #pragma data_seg (".shared")
  6. HHOOK hHook=NULL; //
  7. HWND hFocusWnd=NULL; // window that last gained the focus
  8. HWND hNotifyWnd=NULL; // window to send message to when focus changes
  9. UINT uMessage=0; // wm_message to send in the above case
  10. HHOOK g_hKeyboardHook = NULL; //
  11. bool g_CaptureKeys = FALSE;
  12. UINT g_uKeyboardMessage = 0;
  13. HWND g_hKeyboardNotifyWnd = NULL;
  14. #pragma data_seg ()
  15. #pragma comment(linker, "/SECTION:.shared,RWS")
  16. HINSTANCE hDllInst;
  17. #define KH_KEY_DOWN 0x01 // Key-down event
  18. #define KH_KEY_UP 0x02 // Key-up event
  19. #define KH_KEY_REPEAT 0x04 // Key-repeat event, the key is held down for long enough
  20. BOOL WINAPI DllMain(HINSTANCE hInstance,DWORD dwReason,LPVOID lpReserved)
  21. {
  22. switch(dwReason)
  23. {
  24. case DLL_PROCESS_ATTACH:
  25. hDllInst = hInstance;
  26. case DLL_THREAD_ATTACH:
  27. case DLL_PROCESS_DETACH:
  28. case DLL_THREAD_DETACH:
  29. break;
  30. }
  31. return TRUE;
  32. }
  33. static LRESULT WINAPI HookProc(int code, WPARAM wParam, LPARAM lParam)
  34. {
  35. if(code == HCBT_SETFOCUS)
  36. {
  37. //Focus has changed, record new focus window
  38. hFocusWnd = (HWND)wParam;
  39. //And if notification requested, send a windows message.
  40. if(hNotifyWnd)
  41. PostMessage(hNotifyWnd, uMessage, wParam, lParam);
  42. }
  43. return CallNextHookEx(hHook,code,wParam,lParam);
  44. }
  45. LRESULT CALLBACK KeyboardProc(INT nCode, WPARAM wParam, LPARAM lParam)
  46. {
  47. if (nCode != HC_ACTION)
  48. return ::CallNextHookEx(g_hKeyboardHook, nCode, wParam, lParam);
  49. const BYTE VKCODE = (BYTE)wParam;
  50. const BYTE KEYEVENT = GetKeyEventType(lParam);
  51. if(g_CaptureKeys && KEYEVENT == KH_KEY_DOWN)
  52. {
  53. if(g_hKeyboardNotifyWnd)
  54. {
  55. PostMessage(g_hKeyboardNotifyWnd, g_uKeyboardMessage, wParam, lParam);
  56. //Don't send this message on
  57. return 1;
  58. }
  59. }
  60. return ::CallNextHookEx(g_hKeyboardHook, nCode, wParam, lParam);
  61. }
  62. BYTE GetKeyEventType(LPARAM lParam)
  63. {
  64. // Reference: WM_KEYDOWN on MSDN
  65. if (lParam & 0x80000000) // check bit 31 for up/down
  66. {
  67. return KH_KEY_UP;
  68. }
  69. else
  70. {
  71. if (lParam & 0x40000000) // check bit 30 for previous up/down
  72. return KH_KEY_REPEAT; // It was pressed down before this key-down event, so it's a key-repeat for sure
  73. else
  74. return KH_KEY_DOWN;
  75. }
  76. }
  77. DLLEXPORT DWORD WINAPI MonitorFocusChanges(HWND hWnd, UINT message)
  78. {
  79. if(hHook)
  80. {
  81. UnhookWindowsHookEx(hHook);
  82. }
  83. hHook = SetWindowsHookEx(WH_CBT,HookProc,hDllInst,0);
  84. hNotifyWnd = hWnd;
  85. uMessage = message;
  86. return TRUE;
  87. }
  88. DLLEXPORT DWORD WINAPI StopMonitoringFocusChanges()
  89. {
  90. if(hHook)
  91. UnhookWindowsHookEx(hHook);
  92. hHook = NULL;
  93. hFocusWnd = NULL;
  94. return TRUE;
  95. }
  96. DLLEXPORT DWORD WINAPI MonitorKeyboardChanges(HWND hWnd,UINT message)
  97. {
  98. if(g_hKeyboardHook)
  99. {
  100. UnhookWindowsHookEx(g_hKeyboardHook);
  101. }
  102. g_hKeyboardHook = ::SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, hDllInst, 0);
  103. g_uKeyboardMessage = message;
  104. g_hKeyboardNotifyWnd = hWnd;
  105. return TRUE;
  106. }
  107. DLLEXPORT DWORD WINAPI StopMonitoringKeyboardChanges()
  108. {
  109. if(g_hKeyboardHook)
  110. UnhookWindowsHookEx(g_hKeyboardHook);
  111. g_hKeyboardHook = NULL;
  112. hFocusWnd = NULL;
  113. return TRUE;
  114. }
  115. DLLEXPORT HWND WINAPI GetCurrentFocus()
  116. {
  117. return hFocusWnd;
  118. }
  119. DLLEXPORT void WINAPI SetCaptureKeys(bool bCapture)
  120. {
  121. g_CaptureKeys = bCapture;
  122. }
  123. DLLEXPORT bool WINAPI GetCaptureKeys()
  124. {
  125. return g_CaptureKeys;
  126. }