使Autohotkey忽略字符串中的任何特殊字符
我们正在使用解析一系列计算机生成的邮件的解析器程序(我无法访问它),但需要一些帮助来决定特定的操作。因此,员工可以使用主题行来获取其他命令。由于每天有超过500封电子邮件供我们使用,并且所有的命令都看起来类似于:Ba,Vi;#TD*; x0003
,所以不可能手动编写它们。于是我编写了一个小型的C#脚本,创建了一个可以完成90%工作的Autohotkey脚本。理论上。它的工作原理,但只要我不使用任何特殊字符,如, : & % etc
。使Autohotkey忽略字符串中的任何特殊字符
我想:
clipboard := Ba{,}Vi{;}{#}TD{*}{;} x0003
clipboard := "Ba,Vi;#TD*; x0003"
clipboard := Ba',Vi';'#TD'*'; x0003
clipboard := {raw} Ba,Vi;#TD*; x0003
(plus some others that I probably forgot here)
下面是带有注释的整个AHK脚本。你开始它,而在Outlook中在选择邮件:
;Win+z -> start script
#z::
;Currently only one iteration
loop,1
{
;CTRL+F to forward selected mail,
;which then automatically selects the "To:" line
Send, {CTRLDOWN}f{CTRUP}
Sleep, 500
Send, [email protected]
Sleep, 500
;Initialize GUI
Gui, +AlwaysOnTop
Gui, Font, S5, Verdana, bold
Gui, Add, Text,, SCANNING-BOOSTER:
Gui, Color, F4A460
;Example for the C# generated buttons below (they all do the same thing):
;Clicking the Button "Google" will run the following script
;Start:
;clipboard := www.Google.com
;return
;This is the part where AHK fails because instead
;of www.Google.com I have codes like "Ba,Vi;#TD*; x0003" which crash AHK
Gui,add,Button,gLabel,Google
Gui,add,Button, ......
Gui,add,Button, ......
Gui,add,Button, ......
Gui,add,Button, ......
Gui,add,Button, ......
..... (around 60 more auto-generated buttons)
Gui,show
Return
Label:
;run the script that has the same name as the Button
;in this case it would be Google.ahk
Run, % A_GuiControl ".ahk"
GuiClose:
Gui, Hide
Sleep, 1000
;after the user has pressed a button and the according code
;has been copied to the clipboard, the GUI closes, the
;mail window becomes active again and we can continue to paste
;the code into the subject line
;Go to subject line
Send, {ALTDOWN}r{ALTUP}
Sleep, 500
;CTRL+a
Send, {CTRLDOWN}a{CTRUP}
Sleep, 500
;Write text from your clipboard to the subject line
Send, %clipboard%
Sleep, 500
return
}
显然,这是目前无法将(或多或少)随机字符串复制到AutoHotkey的剪贴板,然后将其粘贴在其他地方没有收到这么多的错误值得追求它。除了用C#编写所有东西外,我别无选择。以下是我做的:
首先,启动一个新的控制台应用程序,然后将其更改为Windows应用程序
How to change a console application to a windows form application?
这是为了使程序完全对用户不可见的最简单方法。
接下来,我们需要一个关键的监听器。这是我用的一个:
https://blogs.msdn.microsoft.com/toub/2006/05/03/low-level-keyboard-hook-in-c/
我会建议你把它放在一个新班级。无论如何,该代码需要稍微改变,因为它将每次按键都打印到控制台。我们不希望这样,我们想从中调用一个方法。更改HookCallback
到:
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
if (KeyPressed == vkCode)
DoWork();
}
return CallNextHookEx(_HookID, nCode, wParam, lParam);
}
对于上面的代码工作,我们需要添加一个新的委托和新的int变量来我们班:
private static int KeyPressed;
public delegate void SomethingToDo();
private static SomethingToDo DoWork;
SetHook也需要一些小的改动:
private static IntPtr SetHook(KeyboardProc _proc, int KeyCode, SomethingToDo GiveMeWork)
{
DoWork = GiveMeWork;
KeyPressed = KeyCode;
...(leave the rest as it is)...
}
该程序现在完全不可见,可以对按键作出反应。让我们做相反的事,模拟键!
Ctrl key kept down after simulating a ctrl key down event and ctrl key up event
这简单得多。我做了三种方法,PressKey,ReleaseKey和TapKey。请记住,ALT和F10是可能无法使用的特殊系统键。
[DllImport("user32.dll", SetLastError = true)]
static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private const int KeyPressCode = 0x0001;
private const int KeyReleaseCode = 0x0002;
public static void PressKey(System.Windows.Forms.Keys Key)
{
keybd_event((byte)Key, 0, KeyPressCode | 0, 0);
Thread.Sleep(10);
}
public static void ReleaseKey(System.Windows.Forms.Keys Key)
{
keybd_event((byte)Key, 0, KeyPressCode | KeyReleaseCode, 0);
Thread.Sleep(10);
}
public static void TapKey(System.Windows.Forms.Keys Key)
{
PressKey(Key);
ReleaseKey(Key);
}
就是这样,一个基本的Autohokey-Clone可以处理字符串。如果你想走得更远,并为一试图标:
https://translate.google.de/translate?sl=de&tl=en&js=y&prev=_t&hl=de&ie=UTF-8&u=https%3A%2F%2Fdotnet-snippets.de%2Fsnippet%2Fvorlage-fuer-tray-notifyicon-anwendung%2F541&edit-text=&act=url
显示带有按钮的界面更是出奇的简单:
using (Form _form = new Form())
{
_form.size = ...
//....
Button MyButton = new Button();
//....
//closing is also pretty simple, just like your everyday Windows Forms Application:
MyButton.Click += new EventHandler((sender, e) => { Application.Exit(); });
_form.ShowDialog();
}
在main方法放在一起的一切:
private static NotifyIcon TrayIcon = new NotifyIcon();
[STAThread]
static void Main(string[] args)
{
bool WindowOpen = true;
try
{
//Make a new method above or below Main() and replace DoSomething with it.
//It will be executed everytime the F2 key is pressed.
KeyListener._HookID = KeyListener.SetHook(proc, System.Windows.Forms.Keys.F2, DoSomething);
System.Windows.Forms.ContextMenu SmallMenu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem MenuElement;
int MenuIndex = 0;
MenuElement = new System.Windows.Forms.MenuItem();
MenuElement.Index = ++MenuIndex;
MenuElement.Text = "Close";
MenuElement.Click += new EventHandler((sender, e) => { WindowOpen = false; System.Windows.Forms.Application.Exit(); });
SmallMenu.MenuItems.Add(MenuElement);
TrayIcon.Icon = new System.Drawing.Icon("Ghost.ico");
TrayIcon.Text = "String Compatible AHK";
TrayIcon.Visible = true;
TrayIcon.ContextMenu = SmallMenu;
while (WindowOpen)
System.Windows.Forms.Application.Run();
}
finally
{
TrayIcon.Dispose();
KeyListener.UnhookWindowsHookEx(KeyListener._HookID);
}
}
你的脚本是做什么的?它应该做什么?请提供您的代码并指出哪些方面无效。无论如何,请查看[#EscapeChar](https://autohotkey.com/docs/commands/_EscapeChar.htm)并转义每个具有特殊含义的字符。 – MCL
整个脚本已添加注释。这显然不是最终的,但我需要在剪辑板部分能够继续完成之前完成剪贴板部分。转义字符的问题是,我不完全知道将使用哪些字符,因为将来会添加和删除参数。 –
问题是什么?你有错误或不需要的行为?请解释什么解释什么不起作用以及如何工作。 – MCL