Skip to content

Commit befe718

Browse files
committed
V1.1.1.20241102 code files update
1 parent c927418 commit befe718

8 files changed

Lines changed: 399 additions & 88 deletions

File tree

LogWindow.Designer.cs

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LogWindow.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ public LogWindow()
1717
InitializeComponent();
1818
}
1919

20-
private void LogWindow_Load(object sender, EventArgs e)
20+
private void LogWindow_Shown(object sender, EventArgs e)
2121
{
2222
foreach (string log in Logger.logs)
2323
{
2424
LogList.Items.Add(log);
25+
Application.DoEvents();
2526
}
2627
Logger.LogUpdateToUI += LogUpdateToUI;
2728
LogListAutoScroll();
@@ -60,8 +61,11 @@ public struct Logger
6061
/// <param name="message">日志内容</param>
6162
public static void LogAdd(string message)
6263
{
63-
logs.Add(GetTime() + message);
64-
LogUpdateToUI?.Invoke(logs[^1]);//如果委托不为null,则调用。参数为数组的最后一位
64+
Thread t = new(() =>
65+
{
66+
logs.Add(GetTime() + message);
67+
LogUpdateToUI?.Invoke(logs[^1]);//如果委托不为null,则调用。参数为数组的最后一位
68+
});t.Start();//日志编写另起线程进行
6569
}
6670
static string GetTime()
6771
{

Main.Designer.cs

Lines changed: 18 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Main.cs

Lines changed: 142 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,62 @@ public Main()
1616
{
1717
InitializeComponent();
1818
}
19+
20+
21+
/// <summary>
22+
/// 重写
23+
/// </summary>
24+
protected override void WndProc(ref Message m)
25+
{
26+
switch (m.Msg)
27+
{
28+
case 0x0312://WM_HOTKEY
29+
switch (m.WParam.ToInt32())
30+
{
31+
case 0x001:
32+
if (Main_ToolStrip_Script_Run.Enabled)
33+
Main_ToolStrip_Script_Run_Click(null!, null!);
34+
else if (Main_ToolStrip_Script_Stop.Enabled)
35+
Main_ToolStrip_Script_Stop_Click(null!, null!);
36+
break;
37+
}
38+
break;
39+
}
40+
base.WndProc(ref m);
41+
}
1942
private void Main_Load(object sender, EventArgs e)
2043
{
2144
Logger.LogAdd("程序启动");
2245

2346
if (Program.programArgs!.Length > 0)
2447
if (File.Exists(Program.programArgs[0]))
2548
OpenFileToScriptEditBox(Program.programArgs[0]);
49+
50+
if (!HotKey.RegisterHotKey(Handle, 0x001, HotKey.FsModifiers.MOD_CONTROL, Keys.OemPipe))
51+
MessageBox.Show("错误: 快捷键存在冲突", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
52+
}
53+
private void Main_FormClosed(object sender, FormClosedEventArgs e)
54+
{
55+
HotKey.UnregisterHotKey(Handle, 0x001);
56+
}
57+
private void Main_KeyDown(object sender, KeyEventArgs e)
58+
{
59+
if (e.Control && e.KeyCode == Keys.O)
60+
{
61+
Main_ToolStrip_Script_Open_Click(null!, null!);
62+
}
63+
else if(e.Control && e.KeyCode == Keys.S)
64+
{
65+
Main_ToolStrip_Script_SaveAs_Click(null!, null!);
66+
}
67+
else if (e.Control && e.KeyCode == Keys.N)
68+
{
69+
Main_ToolStrip_Script_New_Click(null!, null!);
70+
}
71+
else if(e.Control && e.KeyCode == Keys.F)
72+
{
73+
Main_ToolStrip_Edit_Format_Click(null!, null!);
74+
}
2675
}
2776

2877
IReadOnlyList<WindowInfo>? windowsList;
@@ -238,6 +287,70 @@ struct ScriptRunFramework
238287
/// 主要目标句柄
239288
/// </summary>
240289
public IntPtr mainTarget;
290+
/// <summary>
291+
/// 执行按键操作
292+
/// </summary>
293+
/// <param name="actionType">按键操作类型</param>
294+
/// <param name="keyStr">按键ID</param>
295+
/// <param name="keyType">按键类型</param>
296+
/// <param name="intervalStr">press类型的等待时间</param>
297+
/// <param name="mousePos">鼠标进行操作的位置坐标</param>
298+
private readonly void KeyActionRun(string actionType, string keyStr, string keyType, string intervalStr = "", string mousePos = "")
299+
{
300+
Keys keyCode = (Keys)Enum.Parse(typeof(Keys), keyStr.ToLower(), true);
301+
int interval = 100;//按下和抬起的间隔时间
302+
{
303+
string GetIntervalTime = intervalStr;
304+
if (GetIntervalTime != "")
305+
interval = int.Parse(GetIntervalTime);
306+
}
307+
308+
if (keyType.ToLower() == "key" || keyType == "") //(keyCode.ToString().ToLower().IndexOf("button") == -1)
309+
{
310+
if (actionType == "up") goto keyUp;
311+
KeyInput.SendAction(keyCode, KeyInput.KeyAction.KeyDown, mainTarget);
312+
if (actionType == "down") goto keyActionOver;
313+
Thread.Sleep(interval);
314+
keyUp:;
315+
KeyInput.SendAction(keyCode, KeyInput.KeyAction.KeyUp, mainTarget);
316+
keyActionOver:;
317+
}
318+
else if (keyType.ToLower() == "mouse")//判断是否为鼠标按键,如果是鼠标按键则另外执行
319+
{
320+
Point mousePosPoint;
321+
if (mousePos == "") mousePosPoint = new(0, 0);
322+
else mousePosPoint = new(int.Parse(mousePos.Split(',')[0]), int.Parse(mousePos.Split(',')[1]));
323+
324+
if (actionType == "up") goto mouseUp;
325+
KeyInput.SendAction((KeyInput.MouseButton)keyCode, KeyInput.MouseAction.MouseDown, mainTarget, mousePosPoint);
326+
if (actionType == "down") goto mouseActionOver;
327+
Thread.Sleep(interval);
328+
mouseUp:;
329+
KeyInput.SendAction((KeyInput.MouseButton)keyCode, KeyInput.MouseAction.MouseUp, mainTarget, mousePosPoint);
330+
mouseActionOver:;
331+
}
332+
333+
Thread logT = new(() =>
334+
{
335+
string logStr = "";
336+
logStr += "执行按键";
337+
switch (actionType)
338+
{
339+
case "press":
340+
logStr += "按压"; break;
341+
case "down":
342+
logStr += "按下"; break;
343+
case "up":
344+
logStr += "抬起"; break;
345+
}
346+
logStr += $"。按键: {keyCode}";
347+
if (actionType == "press")
348+
logStr += $"; 间隔时间: {interval}ms";
349+
if (keyType == "mouse" && mousePos != "")
350+
logStr += $"; 位置: [x: {mousePos.Split(',')[0]}; y: {int.Parse(mousePos.Split(',')[1])}]";
351+
Logger.LogAdd(logStr);
352+
}); logT.Start();
353+
}
241354
public void Run(XmlNodeList xmlNL)
242355
{
243356
foreach (XmlNode xn in xmlNL)
@@ -247,53 +360,43 @@ public void Run(XmlNodeList xmlNL)
247360
switch (xmlE.Name)
248361
{
249362
case "press":
250-
{
251-
int interval = 100;//按下和抬起的间隔时间
252-
{
253-
string GetIntervalTime = xmlE.GetAttribute("ms");
254-
if (GetIntervalTime != "")
255-
interval = int.Parse(GetIntervalTime);
256-
}
257-
Keys readKeyCode = (Keys)Enum.Parse(typeof(Keys), xmlE.GetAttribute("key").ToLower(), true);
258-
if (xmlE.GetAttribute("type").ToLower() == "key" || xmlE.GetAttribute("type") == "") //(readKeyCode.ToString().ToLower().IndexOf("button") == -1)
259-
{
260-
KeyInput.SendAction(readKeyCode, KeyInput.KeyAction.KeyDown, mainTarget);
261-
Thread.Sleep(interval);
262-
KeyInput.SendAction(readKeyCode, KeyInput.KeyAction.KeyUp, mainTarget);
263-
}
264-
else if (xmlE.GetAttribute("type").ToLower() == "mouse")//判断是否为鼠标按键,如果是鼠标按键则另外执行
265-
{
266-
KeyInput.SendAction((KeyInput.MouseButton)readKeyCode, KeyInput.MouseAction.MouseDown, mainTarget);
267-
Thread.Sleep(interval);
268-
KeyInput.SendAction((KeyInput.MouseButton)readKeyCode, KeyInput.MouseAction.MouseUp, mainTarget);
269-
}
270-
Logger.LogAdd($"执行按键按压。按键: {readKeyCode}; 间隔时间: {interval}ms");
271-
}
363+
KeyActionRun(xmlE.Name, xmlE.GetAttribute("key"), xmlE.GetAttribute("type"), xmlE.GetAttribute("ms"), xmlE.GetAttribute("mouse_pos"));
272364
break;
273365
case "down":
366+
case "up":
367+
KeyActionRun(xmlE.Name, xmlE.GetAttribute("key"), xmlE.GetAttribute("type"), mousePos: xmlE.GetAttribute("mouse_pos"));
368+
break;
369+
case "mouse_move":
274370
{
275-
Keys readKeyCode = (Keys)Enum.Parse(typeof(Keys), xmlE.GetAttribute("key").ToLower(), true);
276-
if (xmlE.GetAttribute("type").ToLower() == "key" || xmlE.GetAttribute("type") == "") //(readKeyCode.ToString().ToLower().IndexOf("button") == -1)
277-
KeyInput.SendAction(readKeyCode, KeyInput.KeyAction.KeyDown, mainTarget);
278-
else if (xmlE.GetAttribute("type").ToLower() == "mouse")
279-
KeyInput.SendAction((KeyInput.MouseButton)readKeyCode, KeyInput.MouseAction.MouseDown, mainTarget);
280-
Logger.LogAdd($"执行按键按下。按键: {readKeyCode}");
371+
string posStr = xmlE.GetAttribute("pos");
372+
/*string keyStr = xmlE.GetAttribute("key").ToLower();*/
373+
Point pos = new(int.Parse(posStr.Split(',')[0]), int.Parse(posStr.Split(',')[1]));
374+
KeyInput.MouseButton inputMouseButton = KeyInput.MouseButton.none;
375+
/*if (keyStr != "")
376+
inputMouseButton = (KeyInput.MouseButton)Enum.Parse(typeof(Keys),keyStr, true); */
377+
378+
KeyInput.SendAction(inputMouseButton, KeyInput.MouseAction.MouseMove, mainTarget, pos);
379+
Logger.LogAdd($"执行鼠标移动。位置: [x: {pos.X}; y: {pos.Y}]");
281380
}
282381
break;
283-
case "up":
382+
case "mouse_wheel":
284383
{
285-
Keys readKeyCode = (Keys)Enum.Parse(typeof(Keys), xmlE.GetAttribute("key").ToLower(), true);
286-
if (xmlE.GetAttribute("type").ToLower() == "key" || xmlE.GetAttribute("type") == "")
287-
KeyInput.SendAction(readKeyCode, KeyInput.KeyAction.KeyUp, mainTarget);
288-
else if (xmlE.GetAttribute("type").ToLower() == "mouse")
289-
KeyInput.SendAction((KeyInput.MouseButton)readKeyCode, KeyInput.MouseAction.MouseUp, mainTarget);
290-
Logger.LogAdd($"执行按键抬起。按键: {readKeyCode}");
384+
string wheelValue = xmlE.GetAttribute("wheel");
385+
string posStr = xmlE.GetAttribute("pos");
386+
Point pos;
387+
if (posStr == "")
388+
pos = new(0, 0);
389+
else
390+
pos = new(int.Parse(posStr.Split(',')[0]), int.Parse(posStr.Split(',')[1]));
391+
KeyInput.SendAction(KeyInput.MouseButton.none, KeyInput.MouseAction.MouseWheel, mainTarget, pos, int.Parse(wheelValue));
392+
Logger.LogAdd($"执行鼠标滚轮滚动。距离: {wheelValue}; 位置: [x: {pos.X}; y: {pos.Y}]");
291393
}
292394
break;
293395
case "text":
294396
{
295-
KeyInput.SendAction(xmlE.GetAttribute("txt"), mainTarget);
296-
Logger.LogAdd($"执行输入文本。文本内容: {xmlE.GetAttribute("txt")}");
397+
string targetText = xmlE.GetAttribute("txt");
398+
KeyInput.SendAction(targetText, mainTarget);
399+
Logger.LogAdd($"执行输入文本。文本内容: {targetText}");
297400
}
298401
break;
299402
case "loop":
@@ -446,5 +549,7 @@ private void Main_ToolStrip_help_helpDoc_Click(object sender, EventArgs e)
446549
{
447550
System.Diagnostics.Process.Start("explorer.exe", "https://github.com/Hgnim/KeyInputMacro/wiki/%E5%B8%AE%E5%8A%A9%E6%96%87%E6%A1%A3");
448551
}
552+
553+
449554
}
450555
}

Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace KeyInputMacro
66
{
77
internal static class Program
88
{
9-
public const string version = "1.0.1.20241025";
9+
public const string version = "1.1.1.20241102";
1010

1111
internal static string[]? programArgs;
1212
/// <summary>

0 commit comments

Comments
 (0)