Skip to content

Commit 81a4632

Browse files
committed
Implement auto-switching to English when the option is enabled
1 parent dda008f commit 81a4632

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
using System.Text;
4+
5+
namespace Flow.Launcher.Helper;
6+
7+
public static class KeyboardLayoutHelper
8+
{
9+
#region Windows API
10+
11+
[DllImport("user32.dll")]
12+
private static extern IntPtr GetKeyboardLayout(uint idThread);
13+
14+
[DllImport("user32.dll")]
15+
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);
16+
17+
[DllImport("user32.dll")]
18+
private static extern IntPtr ActivateKeyboardLayout(IntPtr hkl, uint flags);
19+
20+
[DllImport("user32.dll")]
21+
private static extern int GetKeyboardLayoutList(int nBuff, [Out] IntPtr[] lpList);
22+
23+
[DllImport("kernel32.dll")]
24+
private static extern int GetLocaleInfoA(uint Locale, uint LCType, StringBuilder lpLCData, int cchData);
25+
26+
[DllImport("user32.dll")]
27+
private static extern IntPtr GetForegroundWindow();
28+
29+
// Used to get the language name of the keyboard layout
30+
private const uint LOCALE_SLANGUAGE = 0x00000002;
31+
32+
// The string to search for in the language name of a keyboard layout
33+
private const string LAYOUT_ENGLISH_SEARCH = "english";
34+
35+
private static IntPtr FindEnglishKeyboardLayout()
36+
{
37+
// Get the number of keyboard layouts
38+
var count = GetKeyboardLayoutList(0, null);
39+
if (count <= 0) return IntPtr.Zero;
40+
41+
// Get all keyboard layouts
42+
var keyboardLayouts = new IntPtr[count];
43+
GetKeyboardLayoutList(count, keyboardLayouts);
44+
45+
// Look for any English keyboard layout
46+
foreach (var layout in keyboardLayouts)
47+
{
48+
// The lower word contains the language identifier
49+
var langId = (uint)layout.ToInt32() & 0xFFFF;
50+
51+
// Get language name for the layout
52+
var sb = new StringBuilder(256);
53+
GetLocaleInfoA(langId, LOCALE_SLANGUAGE, sb, sb.Capacity);
54+
var langName = sb.ToString().ToLowerInvariant();
55+
56+
// Check if it's an English layout
57+
if (langName.Contains(LAYOUT_ENGLISH_SEARCH))
58+
{
59+
return layout;
60+
}
61+
}
62+
63+
return IntPtr.Zero;
64+
}
65+
66+
#endregion
67+
68+
// Query textbox keyboard layout
69+
private static IntPtr _previousLayout;
70+
71+
public static void SetEnglishKeyboardLayout()
72+
{
73+
// Find an installed English layout
74+
var englishLayout = FindEnglishKeyboardLayout();
75+
76+
// No installed English layout found
77+
if (englishLayout == IntPtr.Zero) return;
78+
79+
var hwnd = GetForegroundWindow();
80+
var threadId = GetWindowThreadProcessId(hwnd, IntPtr.Zero);
81+
82+
// Store current keyboard layout
83+
_previousLayout = GetKeyboardLayout(threadId) & 0xFFFF;
84+
85+
// Switch to English layout
86+
ActivateKeyboardLayout(englishLayout, 0);
87+
}
88+
89+
public static void SetPreviousKeyboardLayout()
90+
{
91+
if (_previousLayout == IntPtr.Zero) return;
92+
ActivateKeyboardLayout(_previousLayout, 0);
93+
_previousLayout = IntPtr.Zero;
94+
}
95+
}

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using CommunityToolkit.Mvvm.DependencyInjection;
1515
using CommunityToolkit.Mvvm.Input;
1616
using Flow.Launcher.Core.Plugin;
17+
using Flow.Launcher.Helper;
1718
using Flow.Launcher.Infrastructure;
1819
using Flow.Launcher.Infrastructure.Hotkey;
1920
using Flow.Launcher.Infrastructure.Image;
@@ -1373,6 +1374,11 @@ public void Show()
13731374
MainWindowOpacity = 1;
13741375
MainWindowVisibilityStatus = true;
13751376
VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = true });
1377+
1378+
if (StartWithEnglishMode)
1379+
{
1380+
KeyboardLayoutHelper.SetEnglishKeyboardLayout();
1381+
}
13761382
});
13771383
}
13781384

@@ -1441,7 +1447,12 @@ public async void Hide()
14411447
// 📌 Apply DWM Cloak (Completely hide the window)
14421448
Win32Helper.DWMSetCloakForWindow(mainWindow, true);
14431449
}
1444-
1450+
1451+
if (StartWithEnglishMode)
1452+
{
1453+
KeyboardLayoutHelper.SetPreviousKeyboardLayout();
1454+
}
1455+
14451456
await Task.Delay(50);
14461457

14471458
// Update WPF properties

0 commit comments

Comments
 (0)