Repository navigation
Expand file tree
/
Copy pathInputModes.cs
More file actions
75 lines (69 loc) · 2.78 KB
/
Copy pathInputModes.cs
File metadata and controls
75 lines (69 loc) · 2.78 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
using System;
using System.Threading.Tasks;
namespace KeyParcel
{
internal interface IVirtualKeyboard : IDisposable
{
bool IsReady { get; }
bool NeedsInstallConfirmation { get; }
Task<string> Start();
void Close();
void Send(IntPtr window, string unit);
}
internal sealed class InputModes : IDisposable
{
private readonly IVirtualKeyboard keyboard;
private readonly string folder;
internal bool Virtual { get; private set; }
internal InputModes(IVirtualKeyboard keyboard, string folder = null) { this.keyboard = keyboard; this.folder = folder; }
internal async Task<string> Toggle(Func<bool> confirmInstall)
{
if (Virtual)
{
keyboard.Close();
Virtual = false;
return null;
}
if (keyboard == null) return "此版本缺少虚拟键盘组件,仍使用模拟输入。";
if (!keyboard.IsReady)
{
if (keyboard.NeedsInstallConfirmation && !confirmInstall()) return "已取消切换,仍使用模拟输入。";
string error = await keyboard.Start();
if (error != null) return error;
if (!keyboard.IsReady) return "虚拟键盘未就绪。";
}
Virtual = true;
return null;
}
internal string Prepare(string text, IntPtr window, out Action<string> send, out RimeInput rime)
{
send = null;
rime = null;
if (!Virtual) return null;
if (!keyboard.IsReady) return "虚拟键盘驱动未就绪。请检查驱动,或手动切回模拟输入。";
send = unit => keyboard.Send(window, unit);
bool unicode = false;
for (int i = 0; i < text.Length; i++)
{
char c = text[i];
if (c == '\r' || c == '\n' || (c >= ' ' && c <= '~')) continue;
if (Char.IsControl(c) || Char.IsLowSurrogate(c) ||
(Char.IsHighSurrogate(c) && (i + 1 == text.Length || !Char.IsLowSurrogate(text[i + 1]))))
{
send = null;
return "这段文字含有不支持的控制字符或不完整的 Unicode 字符。";
}
if (Char.IsHighSurrogate(c)) i++;
unicode = true;
}
if (unicode)
{
if (folder == null) { send = null; return "虚拟键盘中文输入需要 KeyParcel 小狼毫扩展。"; }
rime = new RimeInput(folder, window, send);
send = rime.Send;
}
return null;
}
public void Dispose() { if (keyboard != null) keyboard.Dispose(); }
}
}