-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathConsoleFixture.cs
More file actions
103 lines (84 loc) · 3.19 KB
/
ConsoleFixture.cs
File metadata and controls
103 lines (84 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Threading;
using Xunit;
namespace Test
{
public class WindowsConsoleFixtureHelper
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr LoadKeyboardLayout(string pwszKLID, uint Flags);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr GetKeyboardLayout(uint idThread);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetKeyboardLayoutList(int nBuff, [Out] IntPtr[] lpList);
// For set:
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
// For get:
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetForegroundWindow();
const int WM_INPUTLANGCHANGEREQUEST = 0x0050;
private static string GetLayoutNameFromHKL(IntPtr hkl)
{
var lcid = (int)((uint)hkl & 0xffff);
return (new CultureInfo(lcid)).Name;
}
public static IEnumerable<string> GetKeyboardLayoutList()
{
int cnt = GetKeyboardLayoutList(0, null);
var list = new IntPtr[cnt];
GetKeyboardLayoutList(list.Length, list);
foreach (var layout in list)
{
yield return GetLayoutNameFromHKL(layout);
}
}
public static string GetKeyboardLayout()
{
var layout = GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow(), out var processId));
return GetLayoutNameFromHKL(layout);
}
public static IntPtr SetKeyboardLayout(string lang)
{
var layoutId = (new CultureInfo(lang)).KeyboardLayoutId;
var layout = LoadKeyboardLayout(layoutId.ToString("x8"), 0x80);
// Hacky, but tests are probably running in a console app and the layout change
// is ignored, so post the layout change to the foreground window.
PostMessage(GetForegroundWindow(), WM_INPUTLANGCHANGEREQUEST, 0, layoutId);
Thread.Sleep(500);
return layout;
}
}
public class ConsoleFixture : IDisposable
{
public KeyboardLayout KbLayout { get; private set; }
public string Lang { get; private set; }
public string Os { get; private set; }
public ConsoleFixture()
{
Lang = "";
Os = "";
}
public void Initialize(string lang, string os)
{
if (!string.Equals(lang, Lang) || !string.Equals(os, Os))
{
Lang = lang;
Os = os;
KbLayout = new KeyboardLayout(lang, "windows");
}
}
public void Dispose()
{
}
public override string ToString()
{
return Lang + "-" + Os;
}
}
}