Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-homyakov committed Oct 22, 2014
1 parent 926bf7f commit 6c2125f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
Binary file modified dist/recaps.exe
Binary file not shown.
64 changes: 35 additions & 29 deletions recaps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,19 +320,31 @@ HKL GetCurrentLayout() {
return GetKeyboardLayout(threadId);
}

///////////////////////////////////////////////////////////////////////////////
// Activate the selected language
HKL ChangeInputLanguage(UINT newLanguage) {
HWND hwnd = RemoteGetFocus();
g_keyboardInfo.current = newLanguage;
PostMessage(hwnd, WM_INPUTLANGCHANGEREQUEST, 0, (LPARAM)(g_keyboardInfo.hkls[g_keyboardInfo.current]));
#ifdef _DEBUG
PrintDebugString("Language set to %S", g_keyboardInfo.names[g_keyboardInfo.current]);
#endif
return g_keyboardInfo.hkls[g_keyboardInfo.current];
}

///////////////////////////////////////////////////////////////////////////////
// Switches the current language
HKL SwitchLayout() {
HKL currentLayout = GetCurrentLayout();

// Find the current keyboard layout's index
UINT i;
for (i = 0; i < g_keyboardInfo.count; i++) {
UINT currentLanguageIndex = 0;
for (UINT i = 0; i < g_keyboardInfo.count; i++) {
if (g_keyboardInfo.hkls[i] == currentLayout) {
currentLanguageIndex = i;
break;
}
}
UINT currentLanguageIndex = i;

// Find the next active layout
BOOL found = FALSE;
Expand All @@ -345,18 +357,7 @@ HKL SwitchLayout() {
}
}

// Activate the selected language
if (found) {
HWND hwnd = RemoteGetFocus();
g_keyboardInfo.current = newLanguage;
PostMessage(hwnd, WM_INPUTLANGCHANGEREQUEST, 0, (LPARAM)(g_keyboardInfo.hkls[g_keyboardInfo.current]));
#ifdef _DEBUG
PrintDebugString("Language set to %S", g_keyboardInfo.names[g_keyboardInfo.current]);
#endif
return g_keyboardInfo.hkls[g_keyboardInfo.current];
}

return NULL;
return found ? ChangeInputLanguage(newLanguage) : NULL;
}

///////////////////////////////////////////////////////////////////////////////
Expand All @@ -368,6 +369,12 @@ void SwitchAndConvert(void*) {
ConvertSelectedTextInActiveWindow(sourceLayout, targetLayout);
}

///////////////////////////////////////////////////////////////////////////////
// Checks key state
BOOL IsKeyPressed(int nVirtKey) {
return (GetKeyState(nVirtKey) & 0x80000000) > 0;
}

///////////////////////////////////////////////////////////////////////////////
// A LowLevelHookProc implementation that captures the CapsLock key
LRESULT CALLBACK LowLevelHookProc(int nCode, WPARAM wParam, LPARAM lParam) {
Expand All @@ -377,21 +384,20 @@ LRESULT CALLBACK LowLevelHookProc(int nCode, WPARAM wParam, LPARAM lParam) {

// ignore injected keystrokes
if ((data->flags & LLKHF_INJECTED) == 0) {
BOOL ctrl = (GetKeyState(VK_CONTROL) & 0x80000000) > 0;
BOOL caps = data->vkCode == VK_CAPITAL && wParam == WM_KEYDOWN;

if (caps && !ctrl) {
// Handle CapsLock - only switch current layout
SwitchLayout();
return 1;
} else if (caps && ctrl) {
// Handle Ctrl-CapsLock - switch current layout and convert text in current field
// We start SwitchLayoutAndConvertSelected in another thread since it simulates
// keystrokes to copy and paste the teset which call back into this hook.
// That isn't good..
_beginthread(SwitchAndConvert, 0, NULL);
BOOL caps = wParam == WM_KEYDOWN && data->vkCode == VK_CAPITAL;
if (caps) {
if (IsKeyPressed(VK_CONTROL)) {
// Handle Ctrl-CapsLock - switch current layout and convert text in current field
// We start SwitchLayoutAndConvertSelected in another thread since it simulates
// keystrokes to copy and paste the teset which call back into this hook.
// That isn't good.
_beginthread(SwitchAndConvert, 0, NULL);
} else {
// Handle CapsLock - only switch current layout
SwitchLayout();
}
return 1; // prevent windows from handling the keystroke
}
}
}

return CallNextHookEx(g_hHook, nCode, wParam, lParam);
Expand Down

0 comments on commit 6c2125f

Please sign in to comment.