Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Ability to hide the pop-up and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
erryox committed Jan 19, 2020
1 parent 5eca8fe commit 099f575
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 56 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# switchy
# Switchy
Switches languages by the Caps Lock key.

Just put [switchy.exe](https://github.com/erryox/switchy/releases) in the startup folder (to open it press **Win+R** and type **shell:startup**).
Just put [Switchy.exe](https://github.com/erryox/Switchy/releases) in the startup folder (to open it press **Win+R** and type **shell:startup**).
If you want to hide the pop-up in Windows 10, put in this folder a shortcut with **nopopup** parameter instead of the file itself.
2 changes: 1 addition & 1 deletion switchy.sln → Switchy.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29306.81
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "switchy", "switchy\switchy.vcxproj", "{16A46215-C3FC-4F84-966D-22B97CADE8C0}"
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Switchy", "Switchy\Switchy.vcxproj", "{16A46215-C3FC-4F84-966D-22B97CADE8C0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
16 changes: 8 additions & 8 deletions switchy/switchy.vcxproj → Switchy/Switchy.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<ProjectGuid>{16A46215-C3FC-4F84-966D-22B97CADE8C0}</ProjectGuid>
<RootNamespace>switchy</RootNamespace>
<RootNamespace>Switchy</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
Expand Down Expand Up @@ -76,7 +76,8 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>./</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>
</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -88,7 +89,7 @@
<Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>./;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -102,7 +103,8 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>./</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>
</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -119,7 +121,8 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>./</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>
</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
Expand All @@ -131,9 +134,6 @@
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="version.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,4 @@
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="version.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
63 changes: 45 additions & 18 deletions switchy/main.cpp → Switchy/main.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
#include <iostream>
#include <Windows.h>
#include <version.h>

typedef NTSTATUS(WINAPI* RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);

HHOOK hHook = 0;
bool isWindows10 = GetRealOSVersion().dwMajorVersion >= 10;
bool showPopup = false;
bool wasPressed = false;

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*)lParam;

if (p->vkCode == VK_CAPITAL) {
if (wParam == WM_KEYDOWN) {
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
if (GetKeyState(VK_CAPITAL)) {
UnhookWindowsHookEx(hHook);
keybd_event(VK_CAPITAL, 0x3a, 0, 0);
keybd_event(VK_CAPITAL, 0x3a, KEYEVENTF_KEYUP, 0);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, 0, 0);
}
if (isWindows10 && !wasPressed) {

if (!wasPressed) {
wasPressed = true;
keybd_event(VK_LWIN, 0x3a, 0, 0);
keybd_event(VK_SPACE, 0x3a, 0, 0);

if (showPopup) {
keybd_event(VK_LWIN, 0x3a, 0, 0);
keybd_event(VK_SPACE, 0x3a, 0, 0);
} else {
HWND hWnd = GetForegroundWindow();
if (hWnd) {
hWnd = GetAncestor(hWnd, GA_ROOTOWNER);
PostMessage(hWnd, WM_INPUTLANGCHANGEREQUEST, 0, (LPARAM)HKL_NEXT);
}
}
}
}

if (wParam == WM_KEYUP) {
if (isWindows10) {
if (wParam == WM_KEYUP || wParam == WM_SYSKEYUP) {
wasPressed = false;

if (showPopup) {
keybd_event(VK_LWIN, 0x3a, KEYEVENTF_KEYUP, 0);
keybd_event(VK_SPACE, 0x3a, KEYEVENTF_KEYUP, 0);
wasPressed = false;
} else {
HWND hWnd = GetForegroundWindow();
if (hWnd) {
hWnd = GetAncestor(hWnd, GA_ROOTOWNER);
PostMessage(hWnd, WM_INPUTLANGCHANGEREQUEST, 0, (LPARAM)HKL_NEXT);
}
}
}

Expand All @@ -50,14 +56,35 @@ void ShowError(LPCSTR s) {
MessageBox(NULL, s, "Error", MB_OK | MB_ICONERROR);
}

int main () {
DWORD GetOSVersion() {
HMODULE hMod = ::GetModuleHandleW(L"ntdll.dll");
RTL_OSVERSIONINFOW osvi = { 0 };

if (hMod) {
RtlGetVersionPtr p = (RtlGetVersionPtr)::GetProcAddress(hMod, "RtlGetVersion");

if (p) {
osvi.dwOSVersionInfoSize = sizeof(osvi);
p(&osvi);
}
}

return osvi.dwMajorVersion;
}

int main(int argc, char* argv[]) {
showPopup = GetOSVersion() >= 10;
if (argc > 1) {
if (_stricmp("nopopup", argv[1]) == 0)
showPopup = false;
}

HANDLE hMutex = CreateMutex(0, 0, "Switchy");

if (GetLastError() == ERROR_ALREADY_EXISTS) {
ShowError("Another instance is already running!");
return 1;
}


hHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, 0, 0);

Expand Down
22 changes: 0 additions & 22 deletions switchy/version.h

This file was deleted.

0 comments on commit 099f575

Please sign in to comment.