从C#调用Autohotkey脚本

Val*_*ons 6 c# autohotkey

我是C#的初学者,但是使用autohotkey的高级用户.

如果我有这个脚本,我怎么能从C#调用它?

    ins::suspend
    SendMode Input
    Lbutton::
    Loop
    {
    GetKeyState, state, Lbutton, P
    if state=U
    break
    Sendinput {Click down}
    Sleep 25
    Sendinput {Click up}
    Sleep 25
    }
    return
Run Code Online (Sandbox Code Playgroud)

你能告诉我一些简单的例子,这样我就能理解如何去做.

小智 8

这可以通过AutoHotkey.dll(具有COM接口)来实现.

您需要下载此库,请进入c:\Windows\System32.
并注册系统(运行,% "regsvr32.exe AutoHotkey.dll",% "c:\Windows\System32").
然后在VS创建一个控制台应用程序项目,选择项目选项卡/添加引用.
在打开的窗口中找到AutoHotkey的库中,单击"添加"按钮,然后关闭窗口.
所以,现在你有在项目中连接了这个库,这将在参考文件夹中看到.
在Program.cs中选择all并替换此代码:

using System.Threading;
using AutoHotkey;

namespace work_with_AHK_object
{
    class Program
    {
        static void Main()
        {
            /// write content for ahk script (thread)
            string scriptContent=""
            //+"#NoTrayIcon\n"
            +"#KeyHistory, 0\n"
            +"#NoEnv\n"
            //+"ListLines, Off\n"
            //+"DetectHiddenWindows, On\n"
            //+"Process, Priority,, High\n"
            +"SetBatchLines, -1\n"
            +"SetMouseDelay, 25\n"
            //+"Menu, Tray, Icon, % \"shell32.dll\", -153\n"
            //+"WinSet, AlwaysOnTop, On, % \"ahk_id\"A_ScriptHwnd\n"
            //+"WinSet, Style, -0xC00000, % \"ahk_id\"A_ScriptHwnd\n"
            //+"WinMove, % \"ahk_id\"A_ScriptHwnd,, 888, 110, 914, 812\n"
            //+"ListLines\n"
            //+"ListLines, On\n"
            +"TrayTip,, % \"Ready to use!\"\n" /// some notice
            +""
            +"Ins::\n"
            +"   Suspend\n"
            +"   Loop, % A_IsSuspended ? 1:2\n"
            +"      SoundBeep, 12500, 50\n"
            +"   KeyWait, % A_ThisHotkey\n"
            +"   Return\n"
            +""
            +"LButton::\n"
            +"   Loop\n"
            +"      Send, {Click}\n"
            +"   Until, !GetKeyState(\"LButton\", \"P\")\n"
            +"   Return\n"
            +""
            +"Space::\n"
            +"   Suspend, Off\n"
            +"   ExitApp";

            /// initialize instance
            CoCOMServer ahkThread=new CoCOMServer();

            /// launch a script in a separate thread
            ahkThread.ahktextdll(scriptContent);

            /// wait for exit
            while (ahkThread.ahkReady()!=0) Thread.Sleep(1000);
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

打开项目属性,在"应用程序"选项卡中将其输出类型更改为Windows应用

  • 对于仍然在这里寻找的人,我有一个用于AutoHotkey.dll的c#包装器,它不需要你将它注册为com对象.相反,它直接通过p/invoke使用DLL.https://github.com/amazing-andrew/AutoHotkey.Interop (3认同)

Mic*_*out 0

您将需要使用发送键

这个视频就是一个很好的例子!

http://www.youtube.com/watch?v=FyDEelZbmEc