Skip to content

Implement auto-switching to English when the option is enabled #3366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
81a4632
Implement auto-switching to English when the option is enabled
Yusyuriv Mar 21, 2025
aa3ad10
When looking for English keyboard layout, use pre-defined IDs instead…
Yusyuriv Mar 22, 2025
023ab45
Use PInvoke instead of DllImport & Several adjustments
Jack251970 Mar 22, 2025
465108a
Fix keyboard layout fetch issue
Jack251970 Mar 22, 2025
67be335
Rename methods to make their purpose more obvious; slight code style …
Yusyuriv Mar 22, 2025
c39079b
Revert accidental change
Yusyuriv Mar 22, 2025
4146f4d
Use focus events to trigger
Jack251970 Mar 22, 2025
f83e8ed
Revert "Use focus events to trigger"
Yusyuriv Mar 22, 2025
6ad4b23
Don't switch to English when IME can be disabled instead
Yusyuriv Mar 22, 2025
ca04823
Remove generic language code
Yusyuriv Mar 22, 2025
747f958
Fix keyboard restore issue when window is deactivated
Jack251970 Mar 23, 2025
cd28c09
Fix the issue with not being able to switch back to the original keyb…
Yusyuriv Mar 23, 2025
bf011f1
Revert "Fix keyboard restore issue when window is deactivated"
Yusyuriv Mar 23, 2025
382d0c2
Don't broadcast language change
Yusyuriv Mar 23, 2025
48aff32
Clarify why not switch keyboard layout for languages that have IME mode
Yusyuriv Mar 23, 2025
4df42a0
Add doc comments and additional error handling in keyboard layout swi…
Yusyuriv Mar 23, 2025
1bf5733
Fix incorrect error handling logic in keyboard layout change
Yusyuriv Mar 23, 2025
5be88dd
Remove blank line
Jack251970 Mar 23, 2025
c63debe
Add foreground window check
Jack251970 Mar 23, 2025
4fc7f70
Adjust formats
Jack251970 Mar 23, 2025
d827d0a
Use language tag instead of language id
Jack251970 Mar 23, 2025
4f2a951
Small code style changes in keyboard change logic
Yusyuriv Mar 23, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions Flow.Launcher/Helper/KeyboardLayoutHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Flow.Launcher.Helper;

public static class KeyboardLayoutHelper
{
#region Windows API

[DllImport("user32.dll")]
private static extern IntPtr GetKeyboardLayout(uint idThread);

[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);

[DllImport("user32.dll")]
private static extern IntPtr ActivateKeyboardLayout(IntPtr hkl, uint flags);

[DllImport("user32.dll")]
private static extern int GetKeyboardLayoutList(int nBuff, [Out] IntPtr[] lpList);

[DllImport("kernel32.dll")]
private static extern int GetLocaleInfoA(uint Locale, uint LCType, StringBuilder lpLCData, int cchData);

[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

// Used to get the language name of the keyboard layout
private const uint LOCALE_SLANGUAGE = 0x00000002;

// The string to search for in the language name of a keyboard layout
private const string LAYOUT_ENGLISH_SEARCH = "english";

private static IntPtr FindEnglishKeyboardLayout()
{
// Get the number of keyboard layouts
var count = GetKeyboardLayoutList(0, null);
if (count <= 0) return IntPtr.Zero;

// Get all keyboard layouts
var keyboardLayouts = new IntPtr[count];
GetKeyboardLayoutList(count, keyboardLayouts);

// Look for any English keyboard layout
foreach (var layout in keyboardLayouts)
{
// The lower word contains the language identifier
var langId = (uint)layout.ToInt32() & 0xFFFF;

// Get language name for the layout
var sb = new StringBuilder(256);
GetLocaleInfoA(langId, LOCALE_SLANGUAGE, sb, sb.Capacity);
var langName = sb.ToString().ToLowerInvariant();

// Check if it's an English layout
if (langName.Contains(LAYOUT_ENGLISH_SEARCH))
{
return layout;
}
}

return IntPtr.Zero;
}

#endregion

// Query textbox keyboard layout
private static IntPtr _previousLayout;

public static void SetEnglishKeyboardLayout()
{
// Find an installed English layout
var englishLayout = FindEnglishKeyboardLayout();

// No installed English layout found
if (englishLayout == IntPtr.Zero) return;

var hwnd = GetForegroundWindow();
var threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);

// Store current keyboard layout
_previousLayout = GetKeyboardLayout(threadId) & 0xFFFF;

// Switch to English layout
ActivateKeyboardLayout(englishLayout, 0);
}

public static void SetPreviousKeyboardLayout()
{
if (_previousLayout == IntPtr.Zero) return;
ActivateKeyboardLayout(_previousLayout, 0);
_previousLayout = IntPtr.Zero;
}
}
13 changes: 12 additions & 1 deletion Flow.Launcher/ViewModel/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.Image;
Expand Down Expand Up @@ -1373,6 +1374,11 @@ public void Show()
MainWindowOpacity = 1;
MainWindowVisibilityStatus = true;
VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = true });

if (StartWithEnglishMode)
{
KeyboardLayoutHelper.SetEnglishKeyboardLayout();
}
});
}

Expand Down Expand Up @@ -1441,7 +1447,12 @@ public async void Hide()
// 📌 Apply DWM Cloak (Completely hide the window)
Win32Helper.DWMSetCloakForWindow(mainWindow, true);
}


if (StartWithEnglishMode)
{
KeyboardLayoutHelper.SetPreviousKeyboardLayout();
}

await Task.Delay(50);

// Update WPF properties
Expand Down
Loading