mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Implement auto-switching to English when the option is enabled
This commit is contained in:
parent
dda008f80b
commit
81a4632722
2 changed files with 107 additions and 1 deletions
95
Flow.Launcher/Helper/KeyboardLayoutHelper.cs
Normal file
95
Flow.Launcher/Helper/KeyboardLayoutHelper.cs
Normal file
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@ using System.Windows.Threading;
|
|||
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;
|
||||
|
|
@ -1373,6 +1374,11 @@ namespace Flow.Launcher.ViewModel
|
|||
MainWindowOpacity = 1;
|
||||
MainWindowVisibilityStatus = true;
|
||||
VisibilityChanged?.Invoke(this, new VisibilityChangedEventArgs { IsVisible = true });
|
||||
|
||||
if (StartWithEnglishMode)
|
||||
{
|
||||
KeyboardLayoutHelper.SetEnglishKeyboardLayout();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -1441,7 +1447,12 @@ namespace Flow.Launcher.ViewModel
|
|||
// 📌 Apply DWM Cloak (Completely hide the window)
|
||||
Win32Helper.DWMSetCloakForWindow(mainWindow, true);
|
||||
}
|
||||
|
||||
|
||||
if (StartWithEnglishMode)
|
||||
{
|
||||
KeyboardLayoutHelper.SetPreviousKeyboardLayout();
|
||||
}
|
||||
|
||||
await Task.Delay(50);
|
||||
|
||||
// Update WPF properties
|
||||
|
|
|
|||
Loading…
Reference in a new issue