1 // xkeymacsdll.cpp : Defines the initialization routines for the DLL.
\r
5 #include "resource.h"
\r
7 #include "Commands.h"
\r
14 #define new DEBUG_NEW
\r
16 static char THIS_FILE[] = __FILE__;
\r
24 static const Modifier Modifiers[] = {
\r
25 // { _T("A-"), ALT },
\r
26 { _T("C-"), CONTROL},
\r
27 // { _T("H-"), HYPER },
\r
29 { _T("S-"), SHIFT },
\r
30 // { _T("s-"), SUPER },
\r
31 { _T("Ctrl+"), WIN_CTRL },
\r
32 { _T("Alt+"), WIN_ALT },
\r
33 { _T("Win+"), WIN_WIN },
\r
35 static const int MAX_MODIFIER = _countof(Modifiers);
\r
37 static const KeyName KeyNames[] = {
\r
38 // { VK_LBUTTON, _T("mouse-1") }, // does not work well
\r
39 // { VK_RBUTTON, _T("mouse-3") }, // does not work well
\r
40 { VK_CANCEL, _T("break") },
\r
41 // { VK_MBUTTON, _T("mouse-2") }, // does not work well
\r
42 { VK_BACK, _T("backspace") },
\r
43 { VK_TAB, _T("tab") },
\r
44 { VK_RETURN, _T("return") },
\r
45 { VK_CAPITAL, _T("capslock") },
\r
46 { VK_KANA, _T("kana") },
\r
47 { VK_KANJI, _T("kanji") },
\r
48 { VK_ESCAPE, _T("escape") },
\r
49 { VK_CONVERT, _T("convert") },
\r
50 { VK_NONCONVERT, _T("nonconvert") },
\r
51 // { VK_SPACE, _T("SPC") }, // [? ]
\r
52 { VK_PRIOR, _T("prior") },
\r
53 { VK_NEXT, _T("next") },
\r
54 { VK_END, _T("end") },
\r
55 { VK_HOME, _T("home") },
\r
56 { VK_LEFT, _T("left") },
\r
57 { VK_UP, _T("up") },
\r
58 { VK_RIGHT, _T("right") },
\r
59 { VK_DOWN, _T("down") },
\r
60 { VK_SELECT, _T("select") },
\r
61 { VK_PRINT, _T("print") },
\r
62 { VK_EXECUTE, _T("execute") },
\r
63 { VK_SNAPSHOT, _T("printscreen") }, // work as print
\r
64 { VK_INSERT, _T("insert") },
\r
65 { VK_DELETE, _T("delete") },
\r
66 { VK_LWIN, _T("lwindow") },
\r
67 { VK_RWIN, _T("rwindow") },
\r
68 { VK_APPS, _T("apps") },
\r
69 { VK_SLEEP, _T("sleep") },
\r
70 { VK_NUMPAD0, _T("kp-0") },
\r
71 { VK_NUMPAD1, _T("kp-1") },
\r
72 { VK_NUMPAD2, _T("kp-2") },
\r
73 { VK_NUMPAD3, _T("kp-3") },
\r
74 { VK_NUMPAD4, _T("kp-4") },
\r
75 { VK_NUMPAD5, _T("kp-5") },
\r
76 { VK_NUMPAD6, _T("kp-6") },
\r
77 { VK_NUMPAD7, _T("kp-7") },
\r
78 { VK_NUMPAD8, _T("kp-8") },
\r
79 { VK_NUMPAD9, _T("kp-9") },
\r
80 { VK_MULTIPLY, _T("kp-multiply") },
\r
81 { VK_ADD, _T("kp-add") },
\r
82 { VK_SUBTRACT, _T("kp-subtract") },
\r
83 { VK_DECIMAL, _T("kp-decimal") },
\r
84 { VK_DIVIDE, _T("kp-divide") },
\r
85 // { VK_F1, _T("f1") }, // FIXME
\r
86 // { VK_F2, _T("f2") }, // Move at the end of definition of funcgtion keys to keep away confliction f1/f2 and f1?/f2? by _tcsncmp() i.e. strncmp()
\r
87 { VK_F3, _T("f3") },
\r
88 { VK_F4, _T("f4") },
\r
89 { VK_F5, _T("f5") },
\r
90 { VK_F6, _T("f6") },
\r
91 { VK_F7, _T("f7") },
\r
92 { VK_F8, _T("f8") },
\r
93 { VK_F9, _T("f9") },
\r
94 { VK_F10, _T("f10") },
\r
95 { VK_F11, _T("f11") },
\r
96 { VK_F12, _T("f12") },
\r
97 { VK_F13, _T("f13") },
\r
98 { VK_F14, _T("f14") },
\r
99 { VK_F15, _T("f15") },
\r
100 { VK_F16, _T("f16") },
\r
101 { VK_F17, _T("f17") },
\r
102 { VK_F18, _T("f18") },
\r
103 { VK_F19, _T("f19") },
\r
104 { VK_F20, _T("f20") },
\r
105 { VK_F21, _T("f21") },
\r
106 { VK_F22, _T("f22") },
\r
107 { VK_F23, _T("f23") },
\r
108 { VK_F24, _T("f24") },
\r
109 { VK_F1, _T("f1") },
\r
110 { VK_F2, _T("f2") },
\r
111 { VK_NUMLOCK, _T("kp-numlock") },
\r
112 { VK_SCROLL, _T("scroll") },
\r
113 { 0xa6, _T("browser-back") }, // VK_BROWSER_BACK
\r
114 { 0xa7, _T("browser-forward") }, // VK_BROWSER_FORWARD
\r
115 { 0xa8, _T("browser-refresh") }, // VK_BROWSER_REFRESH
\r
116 { 0xa9, _T("browser-stop") }, // VK_BROWSER_STOP
\r
117 { 0xaa, _T("browser-search") }, // VK_BROWSER_SEARCH
\r
118 { 0xab, _T("browser-favorites") }, // VK_BROWSER_FAVORITES
\r
119 { 0xac, _T("browser-home") }, // VK_BROWSER_HOME
\r
120 { 0xad, _T("volume-mute") }, // VK_VOLUME_MUTE
\r
121 { 0xae, _T("volume-down") }, // VK_VOLUME_DOWN
\r
122 { 0xaf, _T("volume-up") }, // VK_VOLUME_UP
\r
123 { 0xb0, _T("media-next-track") }, // VK_MEDIA_NEXT_TRACK
\r
124 { 0xb1, _T("media-prev-track") }, // VK_MEDIA_PREV_TRACK
\r
125 { 0xb2, _T("media-stop") }, // VK_MEDIA_STOP
\r
126 { 0xb3, _T("media-play-pause") }, // VK_MEDIA_PLAY_PAUSE
\r
127 { 0xb4, _T("launch-mail") }, // VK_LAUNCH_MAIL
\r
128 { 0xb5, _T("launch-media-select") }, // VK_LAUNCH_MEDIA_SELECT
\r
129 { 0xb6, _T("launch-1") }, // VK_LAUNCH_APP1
\r
130 { 0xb7, _T("launch-2") }, // VK_LAUNCH_APP2
\r
132 static const int MAX_KEYNAME = _countof(KeyNames);
\r
134 static AFX_EXTENSION_MODULE XkeymacsdllDLL = { NULL, NULL };
\r
136 HINSTANCE g_hDllInst = NULL;
\r
137 UINT g_ImeManipulationMessage = 0;
\r
138 #pragma data_seg(".xkmcs")
\r
139 HHOOK g_hHookKeyboard = NULL;
\r
140 HHOOK g_hHookDummy = NULL;
\r
143 inline bool IsWow64(HANDLE mod) {
\r
144 typedef BOOL (WINAPI *pfnIsWow64Process_t)(HANDLE, PBOOL);
\r
145 if (const pfnIsWow64Process_t IsWow64Process = (pfnIsWow64Process_t)GetProcAddress(GetModuleHandle(_T("kernel32")), "IsWow64Process")) {
\r
147 return IsWow64Process(mod, &b) && b;
\r
152 inline bool Is64System() {
\r
154 GetNativeSystemInfo(&info);
\r
155 return info.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_INTEL;
\r
158 inline bool Is64Process(HANDLE mod) {
\r
159 return Is64System() && !IsWow64(mod);
\r
162 const bool IsDll64 = sizeof(void *) == 8;
\r
164 inline bool Is64ProcessHwnd(HWND hwnd) {
\r
166 GetWindowThreadProcessId(hwnd, &pid);
\r
167 HANDLE hmod = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);
\r
168 bool b = Is64Process(hmod);
\r
173 extern "C" int APIENTRY
\r
174 DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
\r
176 g_hDllInst = hInstance;
\r
178 // Remove this if you use lpReserved
\r
179 UNREFERENCED_PARAMETER(lpReserved);
\r
181 switch (dwReason) {
\r
182 case DLL_PROCESS_ATTACH:
\r
183 TRACE0("XKEYMACSDLL.DLL Initializing!\n");
\r
184 g_ImeManipulationMessage = RegisterWindowMessage(_T("XkManipulateIME"));
\r
186 // Extension DLL one-time initialization
\r
187 if (!AfxInitExtensionModule(XkeymacsdllDLL, hInstance)) {
\r
191 // Insert this DLL into the resource chain
\r
192 // NOTE: If this Extension DLL is being implicitly linked to by
\r
193 // an MFC Regular DLL (such as an ActiveX Control)
\r
194 // instead of an MFC application, then you will want to
\r
195 // remove this line from DllMain and put it in a separate
\r
196 // function exported from this Extension DLL. The Regular DLL
\r
197 // that uses this Extension DLL should then explicitly call that
\r
198 // function to initialize this Extension DLL. Otherwise,
\r
199 // the CDynLinkLibrary object will not be attached to the
\r
200 // Regular DLL's resource chain, and serious problems will
\r
204 new CDynLinkLibrary(XkeymacsdllDLL);
\r
206 catch (CMemoryException* e) {
\r
208 // CUtils::Log("DllMain: 'new' threw an exception");
\r
211 case DLL_PROCESS_DETACH:
\r
212 TRACE0("XKEYMACSDLL.DLL Terminating!\n");
\r
213 // Terminate the library before destructors are called
\r
214 AfxTermExtensionModule(XkeymacsdllDLL);
\r
220 //////////////////////////////////////////////////////////////////////
\r
221 // CXkeymacsDll Class
\r
222 //////////////////////////////////////////////////////////////////////
\r
224 #include "xkeymacsDll.h"
\r
225 #pragma data_seg(".xkmcs")
\r
226 DWORD CXkeymacsDll::m_nHookAltRelease = 0;
\r
227 HHOOK CXkeymacsDll::m_hHookCallWnd = NULL;
\r
228 HHOOK CXkeymacsDll::m_hHookCallWndRet = NULL;
\r
229 HHOOK CXkeymacsDll::m_hHookGetMessage = NULL;
\r
230 HHOOK CXkeymacsDll::m_hHookShell = NULL;
\r
231 BOOL CXkeymacsDll::m_bRightControl = FALSE;
\r
232 BOOL CXkeymacsDll::m_bRightAlt = FALSE;
\r
233 BOOL CXkeymacsDll::m_bRightShift = FALSE;
\r
234 BOOL CXkeymacsDll::m_bHook = TRUE;
\r
235 CList<CClipboardSnap *, CClipboardSnap *> CXkeymacsDll::m_oKillRing;
\r
236 int CXkeymacsDll::m_nKillRing = 0;
\r
237 int CXkeymacsDll::m_nOriginal[MAX_COMMAND_TYPE][MAX_KEY] = {'\0'};
\r
238 int CXkeymacsDll::m_nApplicationID = 0;
\r
239 int CXkeymacsDll::m_nAccelerate = 0;
\r
240 int CXkeymacsDll::m_nKeyboardSpeed = 31;
\r
241 HCURSOR CXkeymacsDll::m_hCursor[MAX_STATUS] = {'\0'};
\r
242 HCURSOR CXkeymacsDll::m_hCurrentCursor = NULL;
\r
243 BOOL CXkeymacsDll::m_bCursor = FALSE;
\r
244 TCHAR CXkeymacsDll::m_M_xTip[128] = "";
\r
245 CONFIG CXkeymacsDll::m_Config = {0};
\r
247 BOOL CXkeymacsDll::m_bRecordingMacro = FALSE;
\r
248 BOOL CXkeymacsDll::m_bDown[MAX_KEY] = {0};
\r
249 std::list<KbdMacro> CXkeymacsDll::m_Macro;
\r
251 //////////////////////////////////////////////////////////////////////
\r
252 // Construction/Destruction
\r
253 //////////////////////////////////////////////////////////////////////
\r
255 CXkeymacsDll::CXkeymacsDll()
\r
260 CXkeymacsDll::~CXkeymacsDll()
\r
265 BOOL CXkeymacsDll::SaveConfig()
\r
267 TCHAR szTmp[MAX_PATH];
\r
268 if (!GetTempPath(MAX_PATH, szTmp))
\r
270 if (_tmakepath_s(szTmp, NULL, szTmp, _T("xkeymacs"), _T("tmp")))
\r
272 HANDLE hFile = CreateFile(szTmp, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
\r
273 if (hFile == INVALID_HANDLE_VALUE)
\r
276 BOOL res = WriteFile(hFile, &m_Config, sizeof(m_Config), &written, NULL) || written != sizeof(m_Config);
\r
277 CloseHandle(hFile);
\r
281 BOOL CXkeymacsDll::LoadConfig()
\r
283 TCHAR szTmp[MAX_PATH];
\r
284 if (!GetTempPath(MAX_PATH, szTmp))
\r
286 if (_tmakepath_s(szTmp, NULL, szTmp, _T("xkeymacs"), _T("tmp")))
\r
288 HANDLE hFile = CreateFile(szTmp, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
\r
289 if (hFile == INVALID_HANDLE_VALUE)
\r
292 BOOL res = ReadFile(hFile, &m_Config, sizeof(m_Config), &read, NULL) && read == sizeof(m_Config);
\r
293 CloseHandle(hFile);
\r
298 LRESULT WINAPI DummyProc(int code, WPARAM wp, LPARAM lp) {
\r
299 return CallNextHookEx(0, code, wp, lp);
\r
302 void CXkeymacsDll::SetHooks()
\r
304 m_hHookCallWnd = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)CallWndProc, g_hDllInst, 0);
\r
305 m_hHookCallWndRet = SetWindowsHookEx(WH_CALLWNDPROCRET, (HOOKPROC)CallWndRetProc, g_hDllInst, 0);
\r
306 m_hHookGetMessage = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)GetMsgProc, g_hDllInst, 0);
\r
307 m_hHookShell = SetWindowsHookEx(WH_SHELL, (HOOKPROC)ShellProc, g_hDllInst, 0);
\r
308 g_hHookDummy = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)DummyProc, g_hDllInst, 0);
\r
309 g_hHookKeyboard = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, g_hDllInst, 0);
\r
312 inline void unhook(HHOOK &hh) {
\r
314 UnhookWindowsHookEx(hh);
\r
318 void CXkeymacsDll::ResetHooks()
\r
325 void CXkeymacsDll::ReleaseHooks()
\r
327 unhook(m_hHookCallWnd);
\r
328 unhook(m_hHookCallWndRet);
\r
329 unhook(m_hHookGetMessage);
\r
330 unhook(m_hHookShell);
\r
331 unhook(g_hHookKeyboard);
\r
332 unhook(g_hHookDummy);
\r
335 void CXkeymacsDll::ToggleKeyboardHookState()
\r
337 m_bHook = !m_bHook;
\r
338 ShowKeyboardHookState();
\r
341 void CXkeymacsDll::ShowKeyboardHookState()
\r
343 ICONMSG msg = {MAIN_ICON,};
\r
345 if (CCommands::IsTemporarilyDisableXKeymacs()) {
\r
346 msg.nState = STATUS_DISABLE_TMP;
\r
347 m_hCurrentCursor = m_hCursor[STATUS_DISABLE_TMP];
\r
349 msg.nState = STATUS_ENABLE;
\r
350 m_hCurrentCursor = m_hCursor[STATUS_ENABLE];
\r
353 msg.nState = STATUS_DISABLE_WOCQ;
\r
355 if (m_Config.nSettingStyle[m_nApplicationID] == SETTING_DISABLE
\r
356 || (!_tcsicmp(m_Config.szSpecialApp[m_nApplicationID], _T("Default"))
\r
357 && CUtils::IsDefaultIgnoreApplication())) {
\r
358 msg.nState = STATUS_DISABLE;
\r
359 m_hCurrentCursor = m_hCursor[STATUS_DISABLE];
\r
361 SendIconMessage(&msg, 1);
\r
365 BOOL CXkeymacsDll::IsKeyboardHook()
\r
370 LRESULT CALLBACK CXkeymacsDll::CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)
\r
373 CallNextHookEx(m_hHookCallWnd, nCode, wParam, lParam);
\r
374 const CWPSTRUCT *cwps = reinterpret_cast<CWPSTRUCT *>(lParam);
\r
375 switch (cwps->message) {
\r
376 case WM_IME_STARTCOMPOSITION:
\r
377 InitKeyboardProc(TRUE);
\r
379 case WM_IME_ENDCOMPOSITION:
\r
380 InitKeyboardProc(FALSE);
\r
383 if (cwps->hwnd == GetForegroundWindow()) {
\r
384 InitKeyboardProc(FALSE);
\r
385 ShowKeyboardHookState();
\r
388 case WM_NCACTIVATE:
\r
389 if (cwps->wParam && cwps->hwnd == GetForegroundWindow()) {
\r
390 InitKeyboardProc(FALSE);
\r
391 ShowKeyboardHookState();
\r
395 return CallNextHookEx(m_hHookCallWnd, nCode, wParam, lParam);
\r
398 LRESULT CALLBACK CXkeymacsDll::CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)
\r
401 const CWPRETSTRUCT *cwprets = reinterpret_cast<CWPRETSTRUCT *>(lParam);
\r
402 switch (cwprets->message) {
\r
404 if (cwprets->hwnd == GetForegroundWindow())
\r
405 InitKeyboardProc(FALSE);
\r
412 return CallNextHookEx(m_hHookCallWndRet, nCode, wParam, lParam);
\r
415 LRESULT CALLBACK CXkeymacsDll::GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
\r
417 const MSG *msg = reinterpret_cast<MSG *>(lParam);
\r
418 if (msg->message == g_ImeManipulationMessage) {
\r
420 CCommands::DoSetInputMethodOpenStatus(static_cast<INPUT_METHOD_OPEN_STATUS>(msg->wParam), static_cast<BOOL>(msg->lParam));
\r
423 switch (msg->message) {
\r
424 case WM_IME_STARTCOMPOSITION:
\r
425 InitKeyboardProc(TRUE);
\r
427 case WM_IME_ENDCOMPOSITION:
\r
428 InitKeyboardProc(FALSE);
\r
431 return CallNextHookEx(m_hHookGetMessage, nCode, wParam, lParam);
\r
434 LRESULT CALLBACK CXkeymacsDll::ShellProc(int nCode, WPARAM wParam, LPARAM lParam)
\r
436 if (nCode == HSHELL_WINDOWACTIVATED) {
\r
437 TCHAR className[256];
\r
438 GetClassName((HWND)wParam, className, 255);
\r
439 if (!_tcsicmp(className, _T("ConsoleWindowClass"))) {
\r
440 InitKeyboardProc(FALSE);
\r
441 ShowKeyboardHookState();
\r
444 return CallNextHookEx( m_hHookShell, nCode, wParam, lParam );
\r
447 UINT CXkeymacsDll::GetModifierState(BOOL bPhysicalKey)
\r
450 if (IsDown(VK_SHIFT, bPhysicalKey))
\r
452 if (IsDown(VK_CONTROL, bPhysicalKey))
\r
454 if (IsDown(VK_MENU, bPhysicalKey))
\r
459 void CXkeymacsDll::SetModifierState(const UINT after, const UINT before)
\r
461 if (after & SHIFT && !(before & SHIFT))
\r
462 DepressKey(VK_SHIFT);
\r
463 else if (!(after & SHIFT) && before & SHIFT)
\r
464 ReleaseKey(VK_SHIFT);
\r
466 if (after & CONTROL && !(before & CONTROL)) {
\r
467 UpdateKeyboardState(VK_CONTROL, 1);
\r
468 DepressKey(VK_CONTROL);
\r
469 } else if (!(after & CONTROL) && before & CONTROL) {
\r
470 ReleaseKey(VK_CONTROL);
\r
471 UpdateKeyboardState(VK_CONTROL, 0);
\r
474 const BOOL bHookApp = CUtils::IsVisualCpp() || CUtils::IsFirefox() || CUtils::IsVisualStudio() || CUtils::IsInternetExplorer();
\r
475 if (after & META && !(before & META)) {
\r
477 m_nHookAltRelease |= HOOK_ALT_LATER;
\r
478 DepressKey(VK_MENU);
\r
479 } else if (!(after & META) && before & META) {
\r
481 m_nHookAltRelease++;
\r
482 ReleaseKey(VK_MENU);
\r
486 BOOL CXkeymacsDll::UpdateKeyboardState(BYTE bVk, BYTE bState)
\r
488 BYTE ks[256] = {'\0'};
\r
489 if (!GetKeyboardState(ks))
\r
492 return SetKeyboardState(ks);
\r
495 BOOL CXkeymacsDll::IsDown(BYTE bVk, BOOL bPhysicalKey)
\r
497 return bPhysicalKey ? GetAsyncKeyState(bVk) < 0 : GetKeyState(bVk) < 0;
\r
501 void CXkeymacsDll::DoKeybd_event(BYTE bVk, DWORD dwFlags)
\r
503 // Set KEYEVENTF_EXTENDEDKEY if needed
\r
506 if (m_bRightControl)
\r
507 dwFlags |= KEYEVENTF_EXTENDEDKEY;
\r
512 dwFlags |= KEYEVENTF_EXTENDEDKEY;
\r
516 if (m_bRightShift) {
\r
517 if (CUtils::IsXPorLater())
\r
520 dwFlags |= KEYEVENTF_EXTENDEDKEY;
\r
524 if (IsDown(VK_CONTROL, FALSE)) // Break
\r
525 dwFlags |= KEYEVENTF_EXTENDEDKEY;
\r
539 dwFlags |= KEYEVENTF_EXTENDEDKEY;
\r
542 // CUtils::Log(_T("b: %x, %x, %x, %#hx, %#hx"), bVk, dwFlags, GetMessageExtraInfo(), GetKeyState(bVk), GetAsyncKeyState(bVk));
\r
543 keybd_event(bVk, 0, dwFlags, GetMessageExtraInfo());
\r
544 // CUtils::Log(_T("a: %x, %x, %x, %#hx, %#hx"), bVk, dwFlags, GetMessageExtraInfo(), GetKeyState(bVk), GetAsyncKeyState(bVk));
\r
547 // the key is being depressed
\r
548 void CXkeymacsDll::DepressKey(BYTE bVk, BOOL bOriginal) // bVk is virtual-key code, MSDN said
\r
551 // CUtils::Log(_T("i: %x, %d, %d, %d, %d, %d, %d, %d, %d"), bVk,
\r
552 // IsDown(VK_CONTROL), IsDown(VK_CONTROL, FALSE), IsDepressedModifier(CCommands::C_), IsDepressedModifier(CCommands::C_, FALSE),
\r
553 // IsDown(VK_MENU), IsDown(VK_MENU, FALSE), IsDepressedModifier(CCommands::MetaAlt), IsDepressedModifier(CCommands::MetaAlt, FALSE));
\r
554 SetOriginal(GetModifierState(), bVk);
\r
556 DoKeybd_event(bVk, 0);
\r
559 // the key is being released
\r
560 void CXkeymacsDll::ReleaseKey(BYTE bVk) // bVk is virtual-key code, MSDN said
\r
562 DoKeybd_event(bVk, KEYEVENTF_KEYUP);
\r
565 // bVk down, bVk up
\r
566 void CXkeymacsDll::Kdu(BYTE bVk, DWORD n, BOOL bOriginal)
\r
569 DepressKey(bVk, bOriginal);
\r
574 void CXkeymacsDll::InitKeyboardProc(BOOL bImeComposition)
\r
576 CUtils::SetApplicationName(bImeComposition);
\r
577 if (_tcsnicmp(m_Config.szSpecialApp[m_nApplicationID], CUtils::GetApplicationName(), 0xF) || !IsMatchWindowText(m_Config.szWindowText[m_nApplicationID])) { // PROCESSENTRY32 has only 0xF bytes of Name
\r
578 m_nApplicationID = -1;
\r
579 for (int nAppID = 0; nAppID < MAX_APP; nAppID++) {
\r
580 if (_tcsnicmp(m_Config.szSpecialApp[nAppID], CUtils::GetApplicationName(), 0xF) || !IsMatchWindowText(m_Config.szWindowText[nAppID]))
\r
582 if (m_nApplicationID < 0)
\r
583 m_nApplicationID = nAppID;
\r
585 const LPCSTR curText = m_Config.szWindowText[m_nApplicationID];
\r
586 const LPCSTR newText = m_Config.szWindowText[nAppID];
\r
587 const int curType = CUtils::GetWindowTextType(curText);
\r
588 const int newType = CUtils::GetWindowTextType(newText);
\r
589 if (curType < newType || curType == newType && _tcscmp(curText, newText) <= 0)
\r
590 m_nApplicationID = nAppID;
\r
593 if (m_nApplicationID < 0)
\r
594 m_nApplicationID = GetAppID(_T("Default"), 0);
\r
596 if (m_Config.nSettingStyle[m_nApplicationID] != SETTING_DISABLE &&
\r
597 (_tcsicmp(m_Config.szSpecialApp[m_nApplicationID], _T("Default")) || !CUtils::IsDefaultIgnoreApplication()) &&
\r
598 !bImeComposition && CUtils::IsDialog() && m_Config.bUseDialogSetting[m_nApplicationID])
\r
599 // Use Dialog Setting
\r
600 m_nApplicationID = GetAppID(_T("Dialog"), m_nApplicationID);
\r
603 {CX_ICON, OFF_ICON, ""},
\r
604 {MX_ICON, OFF_ICON, ""},
\r
605 {META_ICON, OFF_ICON, ""}
\r
607 SendIconMessage(msg, 3);
\r
608 CCommands::SetMark(FALSE);
\r
609 CCommands::SetTemporarilyDisableXKeymacs(FALSE);
\r
610 CCommands::Reset();
\r
613 int CXkeymacsDll::GetAppID(const LPCSTR szName, const int fallback)
\r
615 for (int i = 0; i < MAX_APP; i++)
\r
616 if (!_tcsicmp(m_Config.szSpecialApp[i], szName))
\r
621 LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
\r
623 ASSERT(0 <= wParam && wParam <= UCHAR_MAX);
\r
625 UINT nCommandType = NONE;
\r
626 BYTE nOrigKey = (BYTE)wParam;
\r
628 static BOOL bLocked = FALSE;
\r
629 static const BYTE RECURSIVE_KEY = 0x07;
\r
630 static int (*fCommand)() = NULL;
\r
631 static BYTE nOneShotModifier[MAX_KEY] = {'\0'};
\r
632 static BOOL bCherryOneShotModifier = FALSE;
\r
634 // CUtils::Log(_T("nCode = %#x, nKey = %#x, lParam = %p, %d, %d"), nOrigCode, nKey, lParam, IsDll64, Is64ProcessHwnd(GetForegroundWindow()));
\r
636 if (Is64ProcessHwnd(GetForegroundWindow()) != IsDll64 || CUtils::IsXkeymacs())
\r
637 return CallNextHookEx(g_hHookKeyboard, nCode, wParam, lParam);
\r
639 if (nCode < 0 || nCode == HC_NOREMOVE) {
\r
643 // CUtils::Log(_T("nKey = %#x, ext = %d, rel = %d, pre = %d, %#hx, %#hx"), nOrigKey,
\r
644 // (lParam & EXTENDED_KEY) ? 1 : 0, (lParam & BEING_RELEASED) ? 1 : 0, (lParam & REPEATED_KEY) ? 1 : 0,
\r
645 // GetKeyState(nOrigKey), GetAsyncKeyState(nOrigKey));
\r
647 if (nOrigKey == RECURSIVE_KEY) {
\r
648 if (lParam & BEING_RELEASED) {
\r
649 goto HOOK_RECURSIVE_KEY;
\r
651 goto RECURSIVE_COMMAND;
\r
656 static BOOL bShift = FALSE;
\r
657 if (IsDepressedShiftKeyOnly(nOrigKey)) {
\r
658 if (lParam & BEING_RELEASED) {
\r
660 CCommands::SetMark(FALSE);
\r
670 BYTE nKey = nOrigKey;
\r
673 if (lParam & EXTENDED_KEY) {
\r
674 nKey = VK_RCONTROL;
\r
676 nKey = VK_LCONTROL;
\r
680 if (lParam & EXTENDED_KEY) {
\r
687 if (lParam & EXTENDED_KEY) {
\r
697 if (lParam & BEING_RELEASED) {
\r
698 switch (nOrigKey) {
\r
700 if (m_nHookAltRelease) {
\r
701 if (m_nHookAltRelease & ~HOOK_ALT_LATER)
\r
702 m_nHookAltRelease--;
\r
703 else if (m_nHookAltRelease & HOOK_ALT_LATER)
\r
704 m_nHookAltRelease = 0;
\r
711 for (int i = 0; i < MAX_COMMAND_TYPE; i++) {
\r
712 int (*func)() = Commands[m_Config.nCommandID[m_nApplicationID][i][nKey]].fCommand;
\r
713 if (func && !(nOrigKey == VK_MENU && func == CCommands::MetaAlt))
\r
717 if (nOneShotModifier[nKey]) {
\r
718 ReleaseKey(nOneShotModifier[nKey]);
\r
719 nOneShotModifier[nKey] = 0;
\r
721 if (bCherryOneShotModifier) {
\r
722 bCherryOneShotModifier = FALSE;
\r
729 if (m_Config.nSettingStyle[m_nApplicationID] == SETTING_DISABLE) {
\r
733 // Do Nothing for Meadow, Mule for Win32, ... if those use default setting.
\r
734 if (!_tcsicmp(m_Config.szSpecialApp[m_nApplicationID], _T("Default"))
\r
735 && CUtils::IsDefaultIgnoreApplication()) {
\r
739 switch (IsPassThrough(nKey)) {
\r
740 case GOTO_DO_NOTHING:
\r
751 // set command type
\r
752 nCommandType = IsDown(VK_SHIFT) * SHIFT | IsControl() * CONTROL | IsMeta() * META | CCommands::bC_x() * CONTROLX;
\r
753 // Ignore undefined C-x ?
\r
754 if (nCommandType & CONTROLX) {
\r
755 if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == NULL
\r
756 && m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey] < 0) {
\r
757 if (m_Config.bIgnoreUndefinedC_x[m_nApplicationID]) {
\r
758 CCommands::Reset(GOTO_HOOK);
\r
761 nCommandType &= ~CONTROLX;
\r
764 // Ignore undefined Meta Ctrl+?
\r
765 if (CCommands::bM_() && (nCommandType & CONTROL)) {
\r
766 if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == NULL
\r
767 && m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey] < 0) {
\r
768 if (m_Config.bIgnoreUndefinedMetaCtrl[m_nApplicationID]) {
\r
769 if (CheckOriginal(CONTROL, nKey)) {
\r
772 CCommands::Reset(GOTO_HOOK);
\r
775 nCommandType &= ~META;
\r
779 int nVirtualCommandType = GetModifierState(FALSE);
\r
780 if (nOrigKey == VK_CONTROL)
\r
781 nVirtualCommandType &= ~CONTROL;
\r
782 if (nOrigKey == VK_MENU)
\r
783 nVirtualCommandType &= ~META;
\r
784 if (CheckOriginal(nVirtualCommandType, nOrigKey)) {
\r
788 if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::EnableOrDisableXKeymacs) {
\r
789 ToggleKeyboardHookState();
\r
792 if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::EnableXKeymacs) {
\r
794 ToggleKeyboardHookState();
\r
798 if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::DisableXKeymacs) {
\r
800 ToggleKeyboardHookState();
\r
808 if (CCommands::bM_x()) {
\r
809 static unsigned int index = 0;
\r
810 static TCHAR szPath[MAX_PATH] = {'\0'};
\r
812 if (lParam & BEING_RELEASED) {
\r
814 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::BackwardChar) {
\r
819 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::BeginningOfLine) {
\r
822 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::DeleteBackwardChar) {
\r
825 memmove(&szPath[index], &szPath[index + 1], _tcslen(szPath) - index);
\r
829 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::DeleteChar) {
\r
830 if (index < _tcslen(szPath)) {
\r
831 memmove(&szPath[index], &szPath[index + 1], _tcslen(szPath) - index);
\r
835 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::EndOfLine) {
\r
836 index = _tcslen(szPath);
\r
838 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::ForwardChar) {
\r
839 if (index < _tcslen(szPath)) {
\r
843 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::KeyboardQuit) {
\r
844 CCommands::bM_x(FALSE);
\r
846 memset(szPath, 0, sizeof(szPath));
\r
848 } else if (nKey == VK_RETURN
\r
849 || Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::Newline) {
\r
852 CCommands::bM_x(FALSE);
\r
854 memset(szPath, 0, sizeof(szPath));
\r
856 } else if (index < MAX_PATH - 1) {
\r
857 const BOOL bIsShiftDown = IsDown(VK_SHIFT, FALSE);
\r
858 for (TCHAR nAscii = 1; nAscii != 0; ++nAscii) { // repeat until overflow
\r
859 if (nKey != 0 && a2v(nAscii) == nKey && bIsShiftDown == IsShift(nAscii)) {
\r
860 // CUtils::Log("M-x: %#X (%c), %#X (%c)", nKey, nKey, nAscii, nAscii);
\r
861 if (index < _tcslen(szPath)) {
\r
862 memmove(&szPath[index + 1], &szPath[index], __min(_tcslen(szPath) - index, MAX_PATH - (index + 1) - 1));
\r
864 szPath[index++] = nAscii;
\r
865 // CUtils::Log("M-x: %c(%#04x)", nAscii, nAscii);
\r
873 if (CCommands::bC_u()) {
\r
874 if ((nCommandType == NONE) && ('0' <= nKey) && (nKey <= '9')) {
\r
875 CCommands::NumericArgument(nKey - '0');
\r
878 if ((nCommandType == NONE) && (nKey == 0xBD)) {
\r
879 CCommands::NumericArgumentMinus();
\r
884 if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~CONTROL][nKey]].fCommand == CCommands::OneShotModifierCtrl) {
\r
885 nOneShotModifier[nKey] = VK_CONTROL;
\r
886 DepressKey(VK_CONTROL);
\r
887 bCherryOneShotModifier = TRUE;
\r
889 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::OneShotModifierCtrlRepeat) {
\r
890 nOneShotModifier[nKey] = VK_CONTROL;
\r
891 DepressKey(VK_CONTROL);
\r
892 bCherryOneShotModifier = TRUE;
\r
894 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~CONTROL][nKey]].fCommand == CCommands::OneShotModifierCtrlRepeat) {
\r
895 ReleaseKey(VK_CONTROL);
\r
896 bCherryOneShotModifier = FALSE;
\r
899 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~META][nKey]].fCommand == CCommands::OneShotModifierAlt) {
\r
900 nOneShotModifier[nKey] = VK_MENU;
\r
901 DepressKey(VK_MENU);
\r
902 bCherryOneShotModifier = TRUE;
\r
904 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::OneShotModifierAltRepeat) {
\r
905 nOneShotModifier[nKey] = VK_MENU;
\r
906 DepressKey(VK_MENU);
\r
907 bCherryOneShotModifier = TRUE;
\r
909 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~META][nKey]].fCommand == CCommands::OneShotModifierAltRepeat) {
\r
910 ReleaseKey(VK_MENU);
\r
911 bCherryOneShotModifier = FALSE;
\r
914 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~SHIFT][nKey]].fCommand == CCommands::OneShotModifierShift) {
\r
915 nOneShotModifier[nKey] = VK_SHIFT;
\r
916 DepressKey(VK_SHIFT);
\r
917 bCherryOneShotModifier = TRUE;
\r
919 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::OneShotModifierShiftRepeat) {
\r
920 nOneShotModifier[nKey] = VK_SHIFT;
\r
921 DepressKey(VK_SHIFT);
\r
922 bCherryOneShotModifier = TRUE;
\r
924 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~SHIFT][nKey]].fCommand == CCommands::OneShotModifierShiftRepeat) {
\r
925 ReleaseKey(VK_SHIFT);
\r
926 bCherryOneShotModifier = FALSE;
\r
930 for (int i = 0; i < MAX_KEY; ++i) {
\r
931 if (nOneShotModifier[i] == nKey) {
\r
935 if (i == MAX_KEY) {
\r
936 bCherryOneShotModifier = FALSE;
\r
940 if (0 <= m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey]
\r
941 && m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey] < MAX_FUNCTION
\r
942 && _tcslen(m_Config.szFunctionDefinition[m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey]])) {
\r
943 CallFunction(m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey]);
\r
944 CCommands::Reset(GOTO_HOOK);
\r
948 if (!Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand) {
\r
949 if (nOrigKey == VK_CONTROL || nOrigKey == VK_MENU || nOrigKey == VK_SHIFT) {
\r
953 if (!(nCommandType & SHIFT)) {
\r
954 if (CCommands::IsSetMark()) {
\r
955 if (CCommands::MoveCaret(nKey, nCommandType & CONTROL) != CONTINUE) {
\r
956 CCommands::ClearNumericArgument();
\r
959 CCommands::SetMark(FALSE);
\r
963 if (1 < CCommands::GetNumericArgument()) {
\r
964 Kdu(nKey, CCommands::GetNumericArgument());
\r
965 CCommands::ClearNumericArgument();
\r
972 if (CCommands::IsTemporarilyDisableXKeymacs()
\r
973 && Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand != CCommands::KeyboardQuit) {
\r
974 CCommands::SetTemporarilyDisableXKeymacs(FALSE);
\r
978 m_bRightControl = IsDown(VK_RCONTROL, FALSE);
\r
979 m_bRightAlt = IsDown(VK_RMENU, FALSE);
\r
980 m_bRightShift = IsDown(VK_RSHIFT, FALSE);
\r
984 fCommand = Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand;
\r
986 switch (fCommand()) {
\r
987 case GOTO_DO_NOTHING:
\r
993 case GOTO_RECURSIVE:
\r
1007 goto HOOK_RECURSIVE_KEY;
\r
1011 SetModifierIcons();
\r
1012 if (m_bRecordingMacro && (!(lParam & BEING_RELEASED) || m_bDown[wParam])) {
\r
1013 KbdMacro m = { nCode, wParam, lParam, TRUE };
\r
1014 m_Macro.push_back(m);
\r
1015 m_bDown[wParam] |= !(lParam & BEING_RELEASED);
\r
1017 return CallNextHookEx(g_hHookKeyboard, nCode, wParam, lParam);
\r
1020 Kdu(RECURSIVE_KEY, 1, FALSE);
\r
1023 CCommands::SetLastCommand(fCommand);
\r
1026 SetModifierIcons();
\r
1027 HOOK_RECURSIVE_KEY:
\r
1031 void CXkeymacsDll::SetModifierIcons()
\r
1033 #define IconState(x) ((x) ? ON_ICON : OFF_ICON)
\r
1034 ICONMSG msg[6] = {
\r
1035 {MX_ICON, IconState(CCommands::bM_x()), ""},
\r
1036 {CX_ICON, IconState(CCommands::bC_x()), ""},
\r
1037 {META_ICON, IconState(CCommands::bM_()), ""},
\r
1038 {SHIFT_ICON, IconState(IsDown(VK_SHIFT, FALSE)), ""},
\r
1039 {CTRL_ICON, IconState(IsControl()), ""},
\r
1040 {ALT_ICON, IconState(IsDown(VK_MENU, FALSE)), ""}
\r
1042 _tcscpy_s(msg[0].szTip, m_M_xTip);
\r
1043 SendIconMessage(msg, 6);
\r
1046 void CXkeymacsDll::SetApplicationName(int nApplicationID, CString szApplicationName)
\r
1048 ZeroMemory(m_Config.szSpecialApp[nApplicationID], CLASS_NAME_LENGTH);
\r
1049 _tcsncpy_s(m_Config.szSpecialApp[nApplicationID], szApplicationName, _TRUNCATE);
\r
1052 void CXkeymacsDll::SetWindowText(int nApplicationID, CString szWindowText)
\r
1054 ZeroMemory(m_Config.szWindowText[nApplicationID], WINDOW_TEXT_LENGTH);
\r
1055 _tcsncpy_s(m_Config.szWindowText[nApplicationID], szWindowText, _TRUNCATE);
\r
1058 void CXkeymacsDll::SetCommandID(int nApplicationID, int nCommandType, int nKey, int nCommandID)
\r
1060 m_Config.nCommandID[nApplicationID][nCommandType][nKey] = nCommandID;
\r
1063 void CXkeymacsDll::SetKillRingMax(int nApplicationID, int nKillRingMax)
\r
1065 m_Config.nKillRingMax[nApplicationID] = nKillRingMax;
\r
1068 void CXkeymacsDll::SetUseDialogSetting(int nApplicationID, BOOL bUseDialogSetting)
\r
1070 m_Config.bUseDialogSetting[nApplicationID] = bUseDialogSetting;
\r
1073 // Clear data of nApplicationID
\r
1074 void CXkeymacsDll::Clear(int nApplicationID)
\r
1076 if (0 <= nApplicationID && nApplicationID < MAX_APP) {
\r
1077 ZeroMemory(m_Config.szSpecialApp[nApplicationID], sizeof(m_Config.szSpecialApp[nApplicationID]));
\r
1078 ZeroMemory(m_Config.nCommandID[nApplicationID], sizeof(m_Config.nCommandID[nApplicationID]));
\r
1079 m_Config.nKillRingMax[nApplicationID] = 0;
\r
1080 m_Config.bUseDialogSetting[nApplicationID] = FALSE;
\r
1081 m_Config.nSettingStyle[nApplicationID] = 0;
\r
1087 BOOL CXkeymacsDll::IsValidKey(BYTE bVk)
\r
1089 if (bVk == 0xf0) { // 0xf0: Eisu key. GetAsyncKeyState returns the wrong state of Eisu key.
\r
1105 BOOL CXkeymacsDll::IsDepressedModifier(int (__cdecl *Modifier)(void), BOOL bPhysicalKey)
\r
1109 if (IsValidKey(bVk) && IsDown(bVk, bPhysicalKey) &&
\r
1110 Commands[m_Config.nCommandID[m_nApplicationID][NONE][bVk]].fCommand == Modifier)
\r
1116 BOOL CXkeymacsDll::IsDepressedShiftKeyOnly(BYTE nKey)
\r
1118 if (nKey != VK_SHIFT
\r
1119 && nKey != VK_LSHIFT
\r
1120 && nKey != VK_RSHIFT) {
\r
1126 if (bVk == VK_SHIFT
\r
1127 || bVk == VK_LSHIFT
\r
1128 || bVk == VK_RSHIFT) {
\r
1132 if (IsDown(bVk, FALSE)) {
\r
1139 BOOL CXkeymacsDll::IsControl()
\r
1141 return CCommands::bC_() || IsDepressedModifier(CCommands::C_);
\r
1144 BOOL CXkeymacsDll::IsMeta()
\r
1146 return CCommands::bM_() || IsDepressedModifier(CCommands::MetaAlt);
\r
1149 void CXkeymacsDll::AddKillRing(BOOL bNewData)
\r
1151 if (m_Config.nKillRingMax[m_nApplicationID] == 0) {
\r
1155 CClipboardSnap *pSnap = new CClipboardSnap;
\r
1156 if( !pSnap ) return;
\r
1158 BOOL bCapture = pSnap->Capture();
\r
1159 bCapture = pSnap->Capture(); // for "office drawing shape format". Can CClipboardSnap care this problem?
\r
1163 m_oKillRing.AddHead(pSnap);
\r
1165 if (m_oKillRing.IsEmpty()) {
\r
1166 m_oKillRing.AddHead(pSnap);
\r
1168 for (CClipboardSnap *pParent = m_oKillRing.GetHead(); pParent->GetNext(); pParent = pParent->GetNext()) {
\r
1171 pParent->SetNext(pSnap);
\r
1181 if (m_Config.nKillRingMax[m_nApplicationID] < m_oKillRing.GetCount()) {
\r
1182 CClipboardSnap *pSnap = m_oKillRing.GetTail();
\r
1185 m_oKillRing.RemoveTail();
\r
1189 // Return TRUE if there is another data
\r
1190 // Return FALSE if there is no more data
\r
1191 CClipboardSnap* CXkeymacsDll::GetKillRing(CClipboardSnap* pSnap, BOOL bForce)
\r
1193 if (m_Config.nKillRingMax[m_nApplicationID] == 0) {
\r
1197 if (m_oKillRing.IsEmpty()) {
\r
1201 m_nKillRing %= m_oKillRing.GetCount();
\r
1204 CClipboardSnap oCurrentSnap;
\r
1205 oCurrentSnap.Capture();
\r
1207 CClipboardSnap *pKillRing = m_oKillRing.GetAt(m_oKillRing.FindIndex(m_nKillRing));
\r
1211 for (; pKillRing->GetNext(); pKillRing = pKillRing->GetNext()) {
\r
1214 if (*pKillRing != oCurrentSnap) {
\r
1220 pSnap = m_oKillRing.GetAt(m_oKillRing.FindIndex(m_nKillRing));
\r
1224 return pSnap->GetNext();
\r
1227 void CXkeymacsDll::SetOriginal(UINT nCommandType, BYTE bVk)
\r
1229 m_nOriginal[nCommandType & ~SHIFT][bVk]++;
\r
1232 int CXkeymacsDll::CheckOriginal(UINT nCommandType, BYTE bVk)
\r
1234 nCommandType &= ~SHIFT;
\r
1235 if (m_nOriginal[nCommandType][bVk])
\r
1236 return m_nOriginal[nCommandType][bVk]--;
\r
1240 void CXkeymacsDll::IncreaseKillRingIndex(int nKillRing)
\r
1242 m_nKillRing += nKillRing;
\r
1245 void CXkeymacsDll::SetSettingStyle(int nApplicationID, int nSettingStyle)
\r
1247 m_Config.nSettingStyle[nApplicationID] = nSettingStyle;
\r
1250 void CXkeymacsDll::SetIgnoreUndefinedMetaCtrl(int nApplicationID, BOOL bIgnoreUndefinedMetaCtrl)
\r
1252 m_Config.bIgnoreUndefinedMetaCtrl[nApplicationID] = bIgnoreUndefinedMetaCtrl;
\r
1255 void CXkeymacsDll::SetIgnoreUndefinedC_x(int nApplicationID, BOOL bIgnoreUndefinedC_x)
\r
1257 m_Config.bIgnoreUndefinedC_x[nApplicationID] = bIgnoreUndefinedC_x;
\r
1260 void CXkeymacsDll::SetEnableCUA(int nApplicationID, BOOL bEnableCUA)
\r
1262 m_Config.bEnableCUA[nApplicationID] = bEnableCUA;
\r
1265 BOOL CXkeymacsDll::GetEnableCUA()
\r
1267 return m_Config.bEnableCUA[m_nApplicationID];
\r
1270 void CXkeymacsDll::StartRecordMacro()
\r
1272 if (CCommands::bC_u())
\r
1274 m_bRecordingMacro = TRUE;
\r
1275 m_Macro.erase(m_Macro.begin(), m_Macro.end());
\r
1276 ZeroMemory(m_bDown, MAX_KEY);
\r
1279 void CXkeymacsDll::EndRecordMacro()
\r
1281 m_bRecordingMacro = FALSE;
\r
1282 while (!m_Macro.empty()) { // remove not released push
\r
1283 const KbdMacro& m = m_Macro.back();
\r
1284 if (m.lParam & BEING_RELEASED)
\r
1286 m_Macro.pop_back();
\r
1290 void CXkeymacsDll::CallMacro()
\r
1292 if (m_bRecordingMacro)
\r
1293 m_bRecordingMacro = FALSE;
\r
1294 UINT before = GetModifierState(FALSE);
\r
1295 SetModifierState(0, before);
\r
1296 for (auto m = m_Macro.begin(); m != m_Macro.end(); m++)
\r
1297 if (m->lParam & BEING_RELEASED)
\r
1298 ReleaseKey(static_cast<BYTE>(m->wParam));
\r
1300 DepressKey(static_cast<BYTE>(m->wParam), m->bOriginal);
\r
1301 SetModifierState(before, 0);
\r
1304 void CXkeymacsDll::Set106Keyboard(BOOL b106Keyboard)
\r
1306 m_Config.b106Keyboard = b106Keyboard;
\r
1309 BOOL CXkeymacsDll::Is106Keyboard()
\r
1311 return m_Config.b106Keyboard;
\r
1314 int CXkeymacsDll::IsPassThrough(BYTE nKey)
\r
1319 && (Commands[m_Config.nCommandID[m_nApplicationID][NONE][bVk]].fCommand == CCommands::PassThrough)) {
\r
1320 if (bVk == nKey) {
\r
1324 return GOTO_DO_NOTHING;
\r
1330 void CXkeymacsDll::SetFunctionKey(int nFunctionID, int nApplicationID, int nCommandType, int nKey)
\r
1332 if (nApplicationID < 0 || MAX_APP <= nApplicationID
\r
1333 || nCommandType < 0 || MAX_COMMAND_TYPE <= nCommandType
\r
1334 || nKey < 0 || MAX_KEY <= nKey) {
\r
1338 m_Config.nFunctionID[nApplicationID][nCommandType][nKey] = nFunctionID;
\r
1341 void CXkeymacsDll::ClearFunctionDefinition()
\r
1343 memset(m_Config.nFunctionID, -1, sizeof(m_Config.nFunctionID));
\r
1344 memset(m_Config.szFunctionDefinition, 0, sizeof(m_Config.szFunctionDefinition));
\r
1347 void CXkeymacsDll::SetFunctionDefinition(int nFunctionID, CString szDefinition)
\r
1349 if (nFunctionID < 0 || nFunctionID >= MAX_FUNCTION)
\r
1351 memset(m_Config.szFunctionDefinition[nFunctionID], 0, sizeof(m_Config.szFunctionDefinition[nFunctionID]));
\r
1352 _tcscpy_s(m_Config.szFunctionDefinition[nFunctionID], szDefinition);
\r
1356 // call an original command which is defined in dot.xkeymacs
\r
1357 void CXkeymacsDll::CallFunction(int nFunctionID)
\r
1359 if (nFunctionID < 0 || nFunctionID >= MAX_FUNCTION)
\r
1361 LPCTSTR def = m_Config.szFunctionDefinition[nFunctionID];
\r
1364 std::vector<KeyBind> keybinds;
\r
1365 const LPCTSTR last = def + _tcslen(def) - 1;
\r
1366 if (*def == _T('"') && *last == _T('"')) {
\r
1367 def++; // skip '"'
\r
1368 while (def < last)
\r
1369 keybinds.push_back(ParseKey(def));
\r
1370 } else if (*def == _T('[') && *last == _T(']')) {
\r
1371 while (++def < last) { // skip '[', ']', and ' '
\r
1372 if (*def == _T('?')) { // [?f ?o ?o]
\r
1373 keybinds.push_back(ParseKey(++def));
\r
1377 for (int i = 0; i < MAX_KEYNAME; i++) {
\r
1378 size_t keylen = _tcslen(KeyNames[i].name);
\r
1379 if (!_tcsncmp(def, KeyNames[i].name, keylen)) {
\r
1380 KeyBind keybind = {NONE, KeyNames[i].bVk};
\r
1381 keybinds.push_back(keybind);
\r
1390 BOOL bM_x = FALSE;
\r
1391 TCHAR szPath[MAX_PATH] = {'\0'};
\r
1392 unsigned int index = 0;
\r
1393 BOOL bInitialized = FALSE;
\r
1394 UINT before = GetModifierState(FALSE);
\r
1396 for (auto p = keybinds.begin(); p != keybinds.end(); p++) {
\r
1397 const int nCommandType = p->nCommandType;
\r
1398 const BYTE bVk = p->bVk;
\r
1399 int (*fCommand)() = nCommandType < MAX_COMMAND_TYPE ? Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][bVk]].fCommand : NULL;
\r
1401 if (fCommand == CCommands::ExecuteExtendedCommand)
\r
1403 else if (!bInitialized) {
\r
1404 SetModifierState(0, before);
\r
1405 bInitialized = TRUE;
\r
1407 // CUtils::Log("CallFunction: Command Name: %s", Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][bVk]].szCommandName);
\r
1408 while (fCommand() == GOTO_RECURSIVE)
\r
1413 if (bVk == VK_RETURN)
\r
1414 InvokeM_x(szPath);
\r
1415 else if (bVk != 0) {
\r
1418 if (a2v(++nAscii) == bVk && ((nCommandType & SHIFT) != 0) == IsShift(nAscii)) {
\r
1419 // CUtils::Log("M-x: %#X (%c), %#X (%c)", bVk, bVk, nAscii, nAscii);
\r
1420 szPath[index++] = nAscii;
\r
1423 } while (nAscii != 127);
\r
1427 if (!bInitialized) {
\r
1428 SetModifierState(0, before);
\r
1429 bInitialized = TRUE;
\r
1431 if (nCommandType & WIN_WIN)
\r
1432 DepressKey(VK_LWIN);
\r
1433 if (nCommandType & WIN_CTRL)
\r
1434 DepressKey(VK_CONTROL);
\r
1435 if (nCommandType & WIN_ALT)
\r
1436 DepressKey(VK_MENU);
\r
1437 if (nCommandType & SHIFT)
\r
1438 DepressKey(VK_SHIFT);
\r
1440 const int nNextType = (p + 1) != keybinds.end() ? (p + 1)->nCommandType : 0;
\r
1441 if (nCommandType & SHIFT && !(nNextType & SHIFT))
\r
1442 ReleaseKey(VK_SHIFT);
\r
1443 if (nCommandType & WIN_ALT && !(nNextType & WIN_ALT))
\r
1444 ReleaseKey(VK_MENU);
\r
1445 if (nCommandType & WIN_CTRL && !(nNextType & WIN_CTRL))
\r
1446 ReleaseKey(VK_CONTROL);
\r
1447 if (nCommandType & WIN_WIN && !(nNextType & WIN_WIN))
\r
1448 ReleaseKey(VK_LWIN);
\r
1452 // If this lines is invoked on M-x, a window transition does not work well.
\r
1453 SetModifierState(before, 0);
\r
1457 KeyBind CXkeymacsDll::ParseKey(LPCTSTR& def)
\r
1459 KeyBind keybind = {NONE};
\r
1460 if (*def == _T('\\')) { // set modifiers
\r
1463 for (int i = 0; i < MAX_MODIFIER; i++) {
\r
1464 size_t len = _tcslen(Modifiers[i].name);
\r
1465 if (!_tcsncmp(def, Modifiers[i].name, len)) {
\r
1466 keybind.nCommandType |= Modifiers[i].id;
\r
1472 if (IsShift(*def) && !(keybind.nCommandType & (WIN_CTRL | WIN_ALT | WIN_WIN)))
\r
1473 keybind.nCommandType |= SHIFT;
\r
1475 for (; i < MAX_KEYNAME; i++) {
\r
1476 size_t len = _tcslen(KeyNames[i].name);
\r
1477 if (!_tcsncmp(def, KeyNames[i].name, len)) {
\r
1482 keybind.bVk = i < MAX_KEYNAME ? KeyNames[i].bVk : a2v(*def++);
\r
1486 BOOL CXkeymacsDll::IsShift(TCHAR nAscii)
\r
1499 return m_Config.b106Keyboard;
\r
1509 case _T('0'): case _T('1'): case _T('2'): case _T('3'): case _T('4'): case _T('5'): case _T('6'): case _T('7'): case _T('8'): case _T('9'):
\r
1512 return !m_Config.b106Keyboard;
\r
1518 return m_Config.b106Keyboard;
\r
1523 return !m_Config.b106Keyboard;
\r
1524 case _T('A'): case _T('B'): case _T('C'): case _T('D'): case _T('E'): case _T('F'): case _T('G'): case _T('H'): case _T('I'): case _T('J'):
\r
1525 case _T('K'): case _T('L'): case _T('M'): case _T('N'): case _T('O'): case _T('P'): case _T('Q'): case _T('R'): case _T('S'): case _T('T'):
\r
1526 case _T('U'): case _T('V'): case _T('W'): case _T('X'): case _T('Y'): case _T('Z'):
\r
1533 return !m_Config.b106Keyboard;
\r
1537 return m_Config.b106Keyboard;
\r
1538 case _T('a'): case _T('b'): case _T('c'): case _T('d'): case _T('e'): case _T('f'): case _T('g'): case _T('h'): case _T('i'): case _T('j'):
\r
1539 case _T('k'): case _T('l'): case _T('m'): case _T('n'): case _T('o'): case _T('p'): case _T('q'): case _T('r'): case _T('s'): case _T('t'):
\r
1540 case _T('u'): case _T('v'): case _T('w'): case _T('x'): case _T('y'): case _T('z'):
\r
1552 BYTE CXkeymacsDll::a2v(TCHAR nAscii)
\r
1560 return m_Config.b106Keyboard ? '2' : (BYTE) 0xde; // VK_OEM_7
\r
1568 return m_Config.b106Keyboard ? '6' : '7';
\r
1570 return m_Config.b106Keyboard ? '7' : (BYTE) 0xde; // VK_OEM_7
\r
1572 return m_Config.b106Keyboard ? '8' : '9';
\r
1574 return m_Config.b106Keyboard ? '9' : '0';
\r
1576 return m_Config.b106Keyboard ? (BYTE) 0xba : '8'; // VK_OEM_1
\r
1578 return 0xbb; // VK_OEM_PLUS
\r
1580 return 0xbc; // VK_OEM_COMMA
\r
1582 return 0xbd; // VK_OEM_MINUS
\r
1584 return 0xbe; // VK_OEM_PERIOD
\r
1586 return 0xbf; // VK_OEM_2
\r
1587 case _T('0'): case _T('1'): case _T('2'): case _T('3'): case _T('4'): case _T('5'): case _T('6'): case _T('7'): case _T('8'): case _T('9'):
\r
1590 return 0xba; // VK_OEM_1
\r
1592 return m_Config.b106Keyboard ? (BYTE) 0xbb : (BYTE) 0xba; // VK_OEM_PLUS VK_OEM_1
\r
1594 return 0xbc; // VK_OEM_COMMA
\r
1596 return m_Config.b106Keyboard ? (BYTE) 0xbd : (BYTE) 0xbb; // VK_OEM_MINUS VK_OEM_PLUS
\r
1598 return 0xbe; // VK_OEM_PERIOD
\r
1600 return 0xbf; // VK_OEM_2
\r
1602 return m_Config.b106Keyboard ? (BYTE) 0xc0 : '2';
\r
1603 case _T('A'): case _T('B'): case _T('C'): case _T('D'): case _T('E'): case _T('F'): case _T('G'): case _T('H'): case _T('I'): case _T('J'):
\r
1604 case _T('K'): case _T('L'): case _T('M'): case _T('N'): case _T('O'): case _T('P'): case _T('Q'): case _T('R'): case _T('S'): case _T('T'):
\r
1605 case _T('U'): case _T('V'): case _T('W'): case _T('X'): case _T('Y'): case _T('Z'):
\r
1608 return 0xdb; // VK_OEM_4
\r
1610 return 0xdc; // VK_OEM_5
\r
1612 return 0xdd; // VK_OEM_6
\r
1614 return m_Config.b106Keyboard ? (BYTE) 0xde : '6'; // VK_OEM_7
\r
1616 return m_Config.b106Keyboard ? (BYTE) 0xe2 : (BYTE) 0xbd; // VK_OEM_102 VK_OEM_MINUS
\r
1618 return 0xc0; // VK_OEM_3
\r
1619 case _T('a'): case _T('b'): case _T('c'): case _T('d'): case _T('e'): case _T('f'): case _T('g'): case _T('h'): case _T('i'): case _T('j'):
\r
1620 case _T('k'): case _T('l'): case _T('m'): case _T('n'): case _T('o'): case _T('p'): case _T('q'): case _T('r'): case _T('s'): case _T('t'):
\r
1621 case _T('u'): case _T('v'): case _T('w'): case _T('x'): case _T('y'): case _T('z'):
\r
1622 return (BYTE) (nAscii - (_T('a') - _T('A')));
\r
1624 return 0xdb; // VK_OEM_4
\r
1626 return 0xdc; // VK_OEM_5
\r
1628 return 0xdd; // VK_OEM_6
\r
1630 return m_Config.b106Keyboard ? (BYTE) 0xde : (BYTE) 0xc0; // VK_OEM_7 VK_OEM_3
\r
1636 BOOL CXkeymacsDll::IsMatchWindowText(CString szWindowText)
\r
1638 BOOL bIsMatchWindowText = TRUE;
\r
1640 TCHAR szCurrentWindowText[WINDOW_TEXT_LENGTH] = {'\0'};
\r
1641 GetWindowText(GetForegroundWindow(), szCurrentWindowText, sizeof(szCurrentWindowText));
\r
1643 switch (CUtils::GetWindowTextType(szWindowText)) {
\r
1644 case IDS_WINDOW_TEXT_MATCH: // *foo*
\r
1645 szWindowText.Delete(0); // Delete first '*'
\r
1646 szWindowText.Delete(szWindowText.GetLength() - 1); // Delete last '*'
\r
1647 bIsMatchWindowText = 0 <= CString(szCurrentWindowText).Find(szWindowText);
\r
1649 case IDS_WINDOW_TEXT_MATCH_FORWARD: // foo*
\r
1650 szWindowText.Delete(szWindowText.GetLength() - 1); // Delete last '*'
\r
1651 bIsMatchWindowText = 0 == CString(szCurrentWindowText).Find(szWindowText);
\r
1653 case IDS_WINDOW_TEXT_MATCH_BACKWARD: // *foo
\r
1654 szWindowText.Delete(0); // Delete first '*'
\r
1655 bIsMatchWindowText = 0 <= CString(szCurrentWindowText).Find(szWindowText, CString(szCurrentWindowText).GetLength() - szWindowText.GetLength());
\r
1657 case IDS_WINDOW_TEXT_MATCH_FULL: // foo
\r
1658 bIsMatchWindowText = szWindowText == CString(szCurrentWindowText);
\r
1660 case IDS_WINDOW_TEXT_IGNORE: // *
\r
1661 bIsMatchWindowText = TRUE;
\r
1668 // CUtils::Log(_T("IsMatchWindowText: %d, _%s_, _%s_"), bIsMatchWindowText, szCurrentWindowText, szWindowText);
\r
1669 return bIsMatchWindowText;
\r
1672 void CXkeymacsDll::SetAccelerate(int nAccelerate)
\r
1674 m_nAccelerate = nAccelerate;
\r
1677 int CXkeymacsDll::GetAccelerate()
\r
1679 return m_nAccelerate;
\r
1682 void CXkeymacsDll::SetKeyboardSpeed(int nKeyboardSpeed)
\r
1684 m_nKeyboardSpeed = nKeyboardSpeed;
\r
1687 unsigned int CXkeymacsDll::GetMaxKeyInterval()
\r
1689 // m_nKeyboardSpeed == 0: slowest repeat rate; approximately 2 characters per second
\r
1690 // m_nKeyboardSpeed == 31: fastest repeat rate; approximately 30 characters per second
\r
1691 // 47 ms is max on my machine w/ KeyboardSpeed 31.
\r
1692 // 1000 / 2 + 50 = 550
\r
1693 // 1000 / 30 + 50 = 83
\r
1694 return (unsigned int) (1000.0 / (2.0 + m_nKeyboardSpeed % 32 * 28.0 / 31.0) + 50.0);
\r
1697 void CXkeymacsDll::SetCursorData(HCURSOR hEnable, HCURSOR hDisableTMP, HCURSOR hDisableWOCQ, HICON hDisable, BOOL bEnable)
\r
1699 m_hCursor[STATUS_ENABLE] = hEnable;
\r
1700 m_hCursor[STATUS_DISABLE_TMP] = hDisableTMP;
\r
1701 m_hCursor[STATUS_DISABLE_WOCQ] = hDisableWOCQ;
\r
1702 m_hCursor[STATUS_DISABLE] = hDisable;
\r
1703 m_bCursor = bEnable;
\r
1706 void CXkeymacsDll::DoSetCursor()
\r
1708 if (m_bCursor && m_hCurrentCursor) {
\r
1709 ::SetCursor(m_hCurrentCursor);
\r
1713 void CXkeymacsDll::Set326Compatible(int nApplicationID, BOOL b326Compatible)
\r
1715 m_Config.b326Compatible[nApplicationID] = b326Compatible;
\r
1718 BOOL CXkeymacsDll::Get326Compatible()
\r
1720 return m_Config.b326Compatible[m_nApplicationID];
\r
1723 void CXkeymacsDll::InvokeM_x(const TCHAR *const szPath)
\r
1725 // CUtils::Log("M-x: szPath=_%s_", szPath);
\r
1726 int (*fCommand)() = NULL;
\r
1728 for (int i = 0; i < MAX_COMMAND; ++i) {
\r
1729 if (_tcsicmp(szPath, Commands[i].szCommandName) == 0) {
\r
1730 fCommand = Commands[i].fCommand;
\r
1736 // CUtils::Log("M-x: Command: _%s_", Commands[i].szCommandName);
\r
1739 // CUtils::Log("M-x: Path: _%s_", szPath);
\r
1740 ShellExecute(NULL, NULL, szPath, NULL, NULL, SW_SHOWNORMAL);
\r
1744 void CXkeymacsDll::SetM_xTip(const TCHAR *const szPath)
\r
1746 _tcscpy_s(m_M_xTip, "M-x LED");
\r
1747 if (szPath && _tcslen(szPath) < 128 - 5)
\r
1748 _stprintf_s(m_M_xTip, "M-x %s", szPath);
\r
1751 BOOL CXkeymacsDll::SendIconMessage(ICONMSG *pMsg, DWORD num)
\r
1754 return CallNamedPipe(ICON_PIPE, pMsg, sizeof(ICONMSG) * num, &ack, sizeof(DWORD), &read, NMPWAIT_NOWAIT);
\r