Files
cursor-lang/CursorLang/Interop/KeyboardLayoutNative.cs
T
2026-08-08 21:24:02 +05:00

29 lines
1.0 KiB
C#

using System.Runtime.InteropServices;
namespace CursorLang.Interop;
/// <summary>
/// Win32 API для определения раскладки активного окна.
/// </summary>
internal static class KeyboardLayoutNative
{
[DllImport("user32.dll")]
internal static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);
[DllImport("user32.dll")]
private static extern IntPtr GetKeyboardLayout(uint idThread);
/// <summary>
/// Идентификатор локали для окна. Раскладка в Windows привязана к потоку,
/// поэтому так её видно у любого приложения, а не только у своего.
/// </summary>
internal static int GetLocaleIdOf(IntPtr hWnd)
{
uint threadId = GetWindowThreadProcessId(hWnd, IntPtr.Zero);
return (int)GetKeyboardLayout(threadId).ToInt64() & 0xFFFF;
}
}