-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainForm.cs
More file actions
252 lines (215 loc) · 8.79 KB
/
MainForm.cs
File metadata and controls
252 lines (215 loc) · 8.79 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
namespace InputAutoSwitch
{
public partial class MainForm : Form
{
private NotifyIcon? trayIcon;
private ContextMenuStrip? trayMenu;
private KeyboardHook? keyboardHook;
private System.Windows.Forms.Timer? idleTimer;
private bool isEnabled = true;
private const int IDLE_MILLISECONDS = 2000; // 2秒 = 2000毫秒
public MainForm()
{
InitializeComponent();
InitializeTrayIcon();
InitializeKeyboardHook();
InitializeTimer();
// 隐藏主窗体,只显示托盘图标
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
this.Visible = false;
}
private void InitializeTrayIcon()
{
// 创建右键菜单
trayMenu = new ContextMenuStrip();
var enableItem = new ToolStripMenuItem("启用自动切换");
enableItem.Checked = isEnabled;
enableItem.Click += (s, e) => ToggleEnable();
var autoStartItem = new ToolStripMenuItem("开机自启动");
autoStartItem.Checked = AutoStartManager.IsAutoStartEnabled();
autoStartItem.Click += (s, e) => ToggleAutoStart();
var testItem = new ToolStripMenuItem("测试输入法检测");
testItem.Click += (s, e) => TestInputMethod();
var exitItem = new ToolStripMenuItem("退出");
exitItem.Click += (s, e) => ExitApplication();
trayMenu.Items.Add(enableItem);
trayMenu.Items.Add(autoStartItem);
trayMenu.Items.Add(new ToolStripSeparator());
trayMenu.Items.Add(testItem);
trayMenu.Items.Add(new ToolStripSeparator());
trayMenu.Items.Add(exitItem);
// 创建托盘图标
trayIcon = new NotifyIcon
{
Text = "输入法自动切换 - 运行中",
Visible = true,
ContextMenuStrip = trayMenu
};
// 设置图标 (优先使用应用程序图标,否则使用系统图标)
try
{
trayIcon.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
}
catch
{
trayIcon.Icon = SystemIcons.Application;
}
// 双击托盘图标显示状态
trayIcon.DoubleClick += (s, e) => ShowStatus();
}
private void InitializeKeyboardHook()
{
keyboardHook = new KeyboardHook();
keyboardHook.KeyPressed += OnKeyPressed;
keyboardHook.Start();
}
private void InitializeTimer()
{
// 创建定时器但不启动
idleTimer = new System.Windows.Forms.Timer();
idleTimer.Interval = IDLE_MILLISECONDS; // 2000毫秒 = 2秒
idleTimer.Tick += OnTimerTick;
// 不自动启动,等待第一次按键
}
private void OnKeyPressed(object? sender, EventArgs e)
{
if (!isEnabled)
return;
// 停止当前定时器(如果正在运行)
idleTimer?.Stop();
// 重新启动2秒定时器
idleTimer?.Start();
}
private void OnTimerTick(object? sender, EventArgs e)
{
// 定时器触发,说明2秒内没有新的按键
// 停止定时器(单次触发)
idleTimer?.Stop();
if (!isEnabled)
return;
// 检查输入法状态
bool isChinese = InputMethodManager.IsChineseInputMode();
if (isChinese)
{
// 可选:显示切换提示(如果不想看到提示可以注释掉)
// trayIcon?.ShowBalloonTip(500, "自动切换", "切换到英文", ToolTipIcon.Info);
// 设置标志,防止循环触发
keyboardHook?.SetAutoSwitchingFlag(true);
// 发送 Ctrl+Space
InputMethodManager.SendCtrlSpace();
// 延迟后重置标志
Task.Delay(100).ContinueWith(_ =>
{
try
{
this.Invoke(() =>
{
keyboardHook?.SetAutoSwitchingFlag(false);
});
}
catch { }
});
}
}
private void ToggleEnable()
{
isEnabled = !isEnabled;
UpdateTrayMenu();
string status = isEnabled ? "已启用" : "已禁用";
trayIcon!.Text = $"输入法自动切换 - {status}";
trayIcon.ShowBalloonTip(1000, "状态更改", $"自动切换功能{status}", ToolTipIcon.Info);
}
private void ToggleAutoStart()
{
AutoStartManager.ToggleAutoStart();
UpdateTrayMenu();
string status = AutoStartManager.IsAutoStartEnabled() ? "已启用" : "已禁用";
trayIcon!.ShowBalloonTip(1000, "设置更改", $"开机自启动{status}", ToolTipIcon.Info);
}
private void UpdateTrayMenu()
{
if (trayMenu != null && trayMenu.Items.Count >= 2)
{
((ToolStripMenuItem)trayMenu.Items[0]).Checked = isEnabled;
((ToolStripMenuItem)trayMenu.Items[1]).Checked = AutoStartManager.IsAutoStartEnabled();
}
}
private void ShowStatus()
{
string enableStatus = isEnabled ? "运行中" : "已暂停";
string autoStartStatus = AutoStartManager.IsAutoStartEnabled() ? "已启用" : "未启用";
MessageBox.Show(
$"输入法自动切换工具\n\n" +
$"当前状态: {enableStatus}\n" +
$"开机自启: {autoStartStatus}\n" +
$"空闲检测: {IDLE_MILLISECONDS / 1000}秒\n\n" +
$"功能说明:\n" +
$"- 检测到键盘空闲{IDLE_MILLISECONDS / 1000}秒后\n" +
$"- 如果输入法为中文模式\n" +
$"- 自动切换到英文模式",
"状态信息",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
private void TestInputMethod()
{
bool isChinese = InputMethodManager.IsChineseInputMode();
string info = InputMethodManager.GetInputMethodInfo();
MessageBox.Show(
$"输入法检测测试\n\n" +
$"是否为中文模式: {(isChinese ? "是" : "否")}\n" +
$"详细信息: {info}\n\n" +
$"请先在任意文本框中切换到中文输入法\n" +
$"然后点击此菜单测试",
"输入法检测",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
}
private void ExitApplication()
{
var result = MessageBox.Show(
"确定要退出输入法自动切换工具吗?",
"确认退出",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question
);
if (result == DialogResult.Yes)
{
keyboardHook?.Stop();
keyboardHook?.Dispose();
idleTimer?.Stop();
idleTimer?.Dispose();
trayIcon!.Visible = false;
trayIcon?.Dispose();
Application.Exit();
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
// 防止直接关闭,最小化到托盘
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
base.OnFormClosing(e);
}
private void InitializeComponent()
{
this.SuspendLayout();
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(0, 0);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "MainForm";
this.Opacity = 0;
this.ShowInTaskbar = false;
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.ResumeLayout(false);
}
}
}