XKeymacs for 64bit Windows
リビジョン | b4bded9b58307b6db4d095034412198939dbbe4f (tree) |
---|---|
日時 | 2011-05-27 08:35:43 |
作者 | Kazuhiro Fujieda <fujieda@user...> |
コミッター | Kazuhiro Fujieda |
Make the keyboard hook thread local.
Set and release the hook when DLL ataches and detaches to threads or
processes. Disable the keyboard hook when other hooks are released.
@@ -18,7 +18,8 @@ static char THIS_FILE[] = __FILE__; | ||
18 | 18 | |
19 | 19 | static AFX_EXTENSION_MODULE XkeymacsdllDLL = { NULL, NULL }; |
20 | 20 | |
21 | -static HINSTANCE g_hDllInst = NULL; | |
21 | +static __declspec(thread) HINSTANCE g_hDllInst = NULL; | |
22 | +static __declspec(thread) HHOOK g_hHookKeyboard = NULL; | |
22 | 23 | |
23 | 24 | extern "C" int APIENTRY |
24 | 25 | DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) |
@@ -28,7 +29,8 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) | ||
28 | 29 | // Remove this if you use lpReserved |
29 | 30 | UNREFERENCED_PARAMETER(lpReserved); |
30 | 31 | |
31 | - if (dwReason == DLL_PROCESS_ATTACH) { | |
32 | + switch (dwReason) { | |
33 | + case DLL_PROCESS_ATTACH: | |
32 | 34 | TRACE0("XKEYMACSDLL.DLL Initializing!\n"); |
33 | 35 | |
34 | 36 | // Extension DLL one-time initialization |
@@ -55,10 +57,15 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) | ||
55 | 57 | e->Delete(); |
56 | 58 | // CUtils::Log("DllMain: 'new' threw an exception"); |
57 | 59 | } |
58 | - } else if (dwReason == DLL_PROCESS_DETACH) { | |
60 | + case DLL_THREAD_ATTACH: | |
61 | + CXkeymacsDll::SetKeyboardHook(); | |
62 | + break; | |
63 | + case DLL_PROCESS_DETACH: | |
59 | 64 | TRACE0("XKEYMACSDLL.DLL Terminating!\n"); |
60 | 65 | // Terminate the library before destructors are called |
61 | 66 | AfxTermExtensionModule(XkeymacsdllDLL); |
67 | + case DLL_THREAD_DETACH: | |
68 | + CXkeymacsDll::ReleaseKeyboardHook(); | |
62 | 69 | } |
63 | 70 | return 1; // ok |
64 | 71 | } |
@@ -69,7 +76,7 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) | ||
69 | 76 | |
70 | 77 | #include "xkeymacsDll.h" |
71 | 78 | #pragma data_seg(".xkmcs") |
72 | - HHOOK CXkeymacsDll::m_hHookKeyboard = NULL; | |
79 | + BOOL CXkeymacsDll::m_bEnableKeyboardHook = FALSE; | |
73 | 80 | HHOOK CXkeymacsDll::m_hHookCallWnd = NULL; |
74 | 81 | HHOOK CXkeymacsDll::m_hHookCallWndRet = NULL; |
75 | 82 | HHOOK CXkeymacsDll::m_hHookGetMessage = NULL; |
@@ -206,19 +213,23 @@ void CXkeymacsDll::DeleteShell_NotifyIcon(ICON_TYPE icon) | ||
206 | 213 | // set hooks |
207 | 214 | void CXkeymacsDll::SetHooks() |
208 | 215 | { |
209 | - m_hHookKeyboard = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, g_hDllInst, 0); | |
216 | + m_bEnableKeyboardHook = TRUE; | |
210 | 217 | m_hHookCallWnd = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)CallWndProc, g_hDllInst, 0); |
211 | 218 | m_hHookCallWndRet = SetWindowsHookEx(WH_CALLWNDPROCRET, (HOOKPROC)CallWndRetProc, g_hDllInst, 0); |
212 | 219 | m_hHookGetMessage = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)GetMsgProc, g_hDllInst, 0); |
213 | 220 | m_hHookShell = SetWindowsHookEx(WH_SHELL, (HOOKPROC)ShellProc, g_hDllInst, 0); |
214 | 221 | } |
215 | 222 | |
223 | +void CXkeymacsDll::SetKeyboardHook() | |
224 | +{ | |
225 | + if (!g_hHookKeyboard) | |
226 | + SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, g_hDllInst, GetCurrentThreadId()); | |
227 | +} | |
228 | + | |
216 | 229 | // release hooks |
217 | 230 | void CXkeymacsDll::ReleaseHooks() |
218 | 231 | { |
219 | - if (m_hHookKeyboard) | |
220 | - UnhookWindowsHookEx(m_hHookKeyboard); | |
221 | - m_hHookKeyboard = NULL; | |
232 | + m_bEnableKeyboardHook = FALSE; | |
222 | 233 | if (m_hHookCallWnd) |
223 | 234 | UnhookWindowsHookEx(m_hHookCallWnd); |
224 | 235 | m_hHookCallWnd = NULL; |
@@ -233,6 +244,13 @@ void CXkeymacsDll::ReleaseHooks() | ||
233 | 244 | m_hHookShell = NULL; |
234 | 245 | } |
235 | 246 | |
247 | +void CXkeymacsDll::ReleaseKeyboardHook() | |
248 | +{ | |
249 | + if (g_hHookKeyboard) | |
250 | + UnhookWindowsHookEx(g_hHookKeyboard); | |
251 | + g_hHookKeyboard = NULL; | |
252 | +} | |
253 | + | |
236 | 254 | void CXkeymacsDll::SetKeyboardHookFlag(BOOL bFlag) |
237 | 255 | { |
238 | 256 | m_bHook = bFlag; |
@@ -679,6 +697,9 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa | ||
679 | 697 | |
680 | 698 | // CUtils::Log(_T("nCode = %#x, nKey = %#x, lParam = %#x"), nCode, nKey, lParam); |
681 | 699 | |
700 | + if (!m_bEnableKeyboardHook) | |
701 | + return CallNextHookEx(g_hHookKeyboard, nCode, wParam, lParam); | |
702 | + | |
682 | 703 | if (nCode < 0 || nCode == HC_NOREMOVE) { |
683 | 704 | goto DO_NOTHING; |
684 | 705 | } |
@@ -1129,7 +1150,7 @@ DO_NOTHING: | ||
1129 | 1150 | bDefiningMacro = m_bDefiningMacro; |
1130 | 1151 | } |
1131 | 1152 | |
1132 | - return CallNextHookEx(m_hHookKeyboard, nCode, wParam, lParam); | |
1153 | + return CallNextHookEx(g_hHookKeyboard, nCode, wParam, lParam); | |
1133 | 1154 | |
1134 | 1155 | RECURSIVE: |
1135 | 1156 | Kdu(RECURSIVE_KEY, 1, FALSE); |
@@ -194,10 +194,12 @@ public: | ||
194 | 194 | void SetAtIbeamCursorOnly(int nApplicationID, int nCommandType, int nKey, BOOL bAtIbeamCursorOnly); |
195 | 195 | void SetApplicationName(int nApplicationID, CString szApplicationName); |
196 | 196 | static void ReleaseHooks(); |
197 | + static void ReleaseKeyboardHook(); | |
197 | 198 | void SetEnableCUA(int nApplicationID, BOOL bEnableCUA); |
198 | 199 | void SetIgnoreUndefinedC_x(int nApplicationID, BOOL bIgnoreUndefinedC_x); |
199 | 200 | void SetIgnoreUndefinedMetaCtrl(int nApplicationID, BOOL bIgnoreUndefinedMetaCtrl); |
200 | 201 | static void SetHooks(); |
202 | + static void SetKeyboardHook(); | |
201 | 203 | void SetSettingStyle(int nApplicationID, int nSettingStyle); |
202 | 204 | void SetUseDialogSetting(int nApplicationID, BOOL bUseDialogSetting); |
203 | 205 | static void AddKillRing(BOOL bNewData = TRUE); |
@@ -232,7 +234,7 @@ private: | ||
232 | 234 | static LRESULT CALLBACK CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam); |
233 | 235 | static BOOL IsMatchWindowText(CString szWindowText); |
234 | 236 | static TCHAR m_szWindowText[MAX_APP][WINDOW_TEXT_LENGTH]; |
235 | - static HHOOK m_hHookKeyboard; | |
237 | + static BOOL m_bEnableKeyboardHook; | |
236 | 238 | static HHOOK m_hHookCallWnd; |
237 | 239 | static HHOOK m_hHookGetMessage; |
238 | 240 | static HHOOK m_hHookShell; |