【问题标题】:Call Autohotkey script from C#从 C# 调用 Autohotkey 脚本
【发布时间】:2021-05-03 19:22:12
【问题描述】:

我是 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

你能给我看个简单的例子吗,这样我就可以理解怎么做。

【问题讨论】:

  • "如何在 c# 中使用 AHK 脚本?" - 好吧,开始的一种方法是明确定义“使用” - 我可以想出很多方法来“使用”该脚本而不做任何合理的事情。例如,我可以使用它来播种随机数生成器。这就是你想要做的吗?
  • 实际上,当您按住鼠标时,它会快速点击鼠标。

标签: c# autohotkey


【解决方案1】:

这是一个可以通过AutoHotkey.dll(具有 COM 接口)到达。

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

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);
         }
    }
}

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

【讨论】:

  • 对于Windows 7 64位用户,您必须通过以下方式注册DLL:1)在cmd中,第一张CD到%systemroot%\syswow64:cd %systemroot%\syswow64 2)regsvr32 C:\Windows\System32\AutoHotkey.dll
  • 对于任何仍在寻找的人,我在 c# 中有一个 AutoHotkey.dll 的包装器,不需要您将其注册为 com 对象。相反,它直接通过 p/invoke 使用 DLL。 github.com/amazing-andrew/AutoHotkey.Interop
  • 在您的 c# 项目中,您可能必须确保它是 32 位应用程序?
【解决方案2】:

我知道这是一篇相当老的帖子,但我自己也遇到了这个问题,并且很难找到解决方案,因为我无法在 C# 中使用任何可用的 AHK 包装器项目。

如果注册 dll 或使用包装器对您来说也是个问题,您可以使用 ahk 论坛 by basi 上这篇帖子中的方法。

基本上你只需要将dll放在你的项目文件夹中,将其包含到项目中,并在属性中将“复制到输出目录”设置为“如果较新则复制”。

然后像这样导入dll函数:

        [DllImport(
        "AutoHotkey.dll", 
        CallingConvention = CallingConvention.Cdecl, 
        CharSet = CharSet.Unicode, 
        EntryPoint = "ahkdll")]
    private static extern int ahkdll(
        [MarshalAs(UnmanagedType.LPWStr)] string scriptFilePath,
        [MarshalAs(UnmanagedType.LPWStr)] string parameters = "",
        [MarshalAs(UnmanagedType.LPWStr)] string title = "");

    [DllImport(
        "AutoHotkey.dll",
        CallingConvention = CallingConvention.Cdecl,
        CharSet = CharSet.Unicode,
        EntryPoint = "ahktextdll")]
    private static extern int ahktextdll(
        [MarshalAs(UnmanagedType.LPWStr)] string script,
        [MarshalAs(UnmanagedType.LPWStr)] string parameters =  "",
        [MarshalAs(UnmanagedType.LPWStr)] string title = "");

ahkdll 允许从文件运行脚本,ahktextdll 允许直接将脚本作为字符串插入。

我只使用来自HotKeyIt 的 v1 dll 对此进行了测试(我使用了 win32w 文件夹中的那个)。

【讨论】:

  • 感谢分享!有用。我使用 Visual Studio 2019 确认。
【解决方案3】:

睡觉很简单:

   System.Threading.Thread.Sleep(25);

当您的应用程序不处于活动状态时,获取 Key 和 MouseDown 事件(ins:: 和 Lbutton::) 要复杂得多。它可以通过使用全局钩子来实现。看看这篇 CodeProject 文章A Simple C# Global Low Level Keyboard Hook

最终,这取决于您为什么要使用 C#,而 AHK 为您提供了一个更简单的环境来实现类似的事情。

我想不出任何简单的例子来完成这项工作。

【讨论】:

    【解决方案4】:

    您将要使用发送密钥

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

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

    【讨论】:

      猜你喜欢
      • 2011-06-01
      • 1970-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 2016-06-17
      • 2018-05-07
      相关资源
      最近更新 更多