Skip to content

Commit 9636e1a

Browse files
committed
Refactor KeyboardLayoutHelper and KeyboardLayout
Most changes come from: cairoshell#54
1 parent 65bc47b commit 9636e1a

File tree

3 files changed

+38
-30
lines changed

3 files changed

+38
-30
lines changed
Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,40 @@
11
using ManagedShell.Common.Structs;
22
using ManagedShell.Interop;
33
using System.Collections.Generic;
4-
using System.Globalization;
5-
using System.Linq;
4+
using System;
65

76
namespace ManagedShell.Common.Helpers
87
{
98
public static class KeyboardLayoutHelper
109
{
11-
public static KeyboardLayout GetKeyboardLayout(bool currentThread = false)
10+
public static KeyboardLayout GetKeyboardLayout()
1211
{
13-
uint threadId = 0;
14-
if (!currentThread)
15-
threadId = NativeMethods.GetWindowThreadProcessId(NativeMethods.GetForegroundWindow(), out _);
12+
uint threadId = NativeMethods.GetWindowThreadProcessId(NativeMethods.GetForegroundWindow(), out _);
1613
var layout = NativeMethods.GetKeyboardLayout(threadId);
1714

18-
return new KeyboardLayout()
19-
{
20-
HKL = layout,
21-
NativeName = CultureInfo.GetCultureInfo((short)layout).NativeName,
22-
ThreeLetterName = CultureInfo.GetCultureInfo((short)layout).ThreeLetterISOLanguageName.ToUpper()
23-
};
15+
return new KeyboardLayout((uint)layout);
2416
}
2517

2618
public static List<KeyboardLayout> GetKeyboardLayoutList()
2719
{
20+
var keyboardLayouts = new List<KeyboardLayout>();
21+
2822
var size = NativeMethods.GetKeyboardLayoutList(0, null);
29-
var result = new long[size];
30-
NativeMethods.GetKeyboardLayoutList(size, result);
23+
var layoutIds = new IntPtr[size];
24+
NativeMethods.GetKeyboardLayoutList(layoutIds.Length, layoutIds);
3125

32-
return result.Select(x => new KeyboardLayout()
26+
foreach (var layoutId in layoutIds)
3327
{
34-
HKL = (int)x,
35-
NativeName = CultureInfo.GetCultureInfo((short)x).NativeName,
36-
ThreeLetterName = CultureInfo.GetCultureInfo((short)x).ThreeLetterISOLanguageName.ToUpper()
37-
}).ToList();
28+
var keyboardLayout = new KeyboardLayout((uint)layoutId);
29+
keyboardLayouts.Add(keyboardLayout);
30+
}
31+
32+
return keyboardLayouts;
3833
}
3934

40-
public static bool SetKeyboardLayout(int layoutId)
35+
public static bool SetKeyboardLayout(uint layoutId)
4136
{
42-
return NativeMethods.PostMessage(0xffff,
43-
(uint) NativeMethods.WM.INPUTLANGCHANGEREQUEST,
44-
0,
45-
NativeMethods.LoadKeyboardLayout(layoutId.ToString("x8"), (uint)(NativeMethods.KLF.SUBSTITUTE_OK | NativeMethods.KLF.ACTIVATE)));
37+
return NativeMethods.PostMessage(NativeMethods.GetForegroundWindow(), (int)NativeMethods.WM.INPUTLANGCHANGEREQUEST, IntPtr.Zero, new IntPtr(layoutId));
4638
}
4739
}
48-
}
40+
}
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1-
namespace ManagedShell.Common.Structs
1+
using System.Globalization;
2+
3+
namespace ManagedShell.Common.Structs
24
{
35
public struct KeyboardLayout
46
{
5-
public int HKL { get; set; }
7+
public ushort LanguageId { get; set; }
8+
public ushort KeyboardId { get; set; }
9+
610
public string NativeName { get; set; }
711
public string ThreeLetterName { get; set; }
12+
public string DisplayName { get; set; }
13+
14+
public KeyboardLayout(uint layoutId)
15+
{
16+
LanguageId = (ushort)(layoutId & 0xFFFF);
17+
KeyboardId = (ushort)(layoutId >> 16);
18+
19+
var cultureInfo = CultureInfo.GetCultureInfo(LanguageId);
20+
NativeName = cultureInfo.NativeName;
21+
ThreeLetterName = cultureInfo.ThreeLetterWindowsLanguageName;
22+
DisplayName = cultureInfo.DisplayName;
23+
}
824
}
9-
}
25+
}

src/ManagedShell.Interop/NativeMethods.User32.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3000,10 +3000,10 @@ public static extern IntPtr CreateWindowEx(
30003000
IntPtr lpParam);
30013001

30023002
[DllImport(User32_DllName)]
3003-
public static extern int GetKeyboardLayout(uint idThread);
3003+
public static extern IntPtr GetKeyboardLayout(uint idThread);
30043004

30053005
[DllImport(User32_DllName)]
3006-
public static extern int GetKeyboardLayoutList(int nBuff, long[] lpList);
3006+
public static extern uint GetKeyboardLayoutList(int nBuff, IntPtr[] lpList);
30073007

30083008
[DllImport(User32_DllName)]
30093009
public static extern int LoadKeyboardLayout(string pwszKLID, uint flags);

0 commit comments

Comments
 (0)