6

I am complete beginner with C#, but advanced user with autohotkey.

If I have this script, how could I call that from 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

Could you show me simple example, so I can get to understand how to do it.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Valters Tomsons
  • 109
  • 1
  • 1
  • 8
  • "How can I use AHK script in c#?" - well, one way to start would be a clear definition of "use" - I can think of a lot of ways to "use" that script and not do anything reasonable. For example I could use it to seed a random number generator. Is that what you're trying to do? – Jasmine Apr 11 '13 at 22:53
  • Actaully, it's doing mouseclicks rapidly, when you are holding down the mouse. – Valters Tomsons Apr 12 '13 at 13:16

4 Answers4

8

This is a may be reached through AutoHotkey.dll (that has COM Interface).

You to need download this library, move in c:\Windows\System32.
And register for the system (Run, % "regsvr32.exe AutoHotkey.dll", % "c:\Windows\System32").
Then in VS create a console application project, and choose Project tab/Add reference.
In opened window find AutoHotkey library, click on "Add" button, then close the window.
So now you have connected this library in your project, and this you'll see in reference folder.
Select all in Program.cs and replace on this code:

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

Open project property, in Application tab change it Output type to Windows Application.

Felix D.
  • 4,811
  • 8
  • 38
  • 72
Grey
  • 339
  • 1
  • 4
  • For Windows 7 64bit users, you have to register the DLL the following way: 1) In cmd, first CD to %systemroot%\syswow64: `cd %systemroot%\syswow64` 2) `regsvr32 C:\Windows\System32\AutoHotkey.dll` – Adam Szabo Aug 06 '13 at 16:04
  • 3
    for anyone still looking around here, I have a wrapper in c# for the AutoHotkey.dll that doesn't require you to register it as a com object. Instead it uses the DLL directly through p/invoke. https://github.com/amazing-andrew/AutoHotkey.Interop – Andrew Smith Oct 14 '14 at 20:07
  • In your c# project you might have to make sure it is a 32bit application? – bgmCoder Nov 04 '14 at 21:17
3

I know this is quite an old post but I just had this issue myself and struggled quite a bit to find a solution, since I couldn´t use any of the available wrapper projects for AHK in C#.

If it´s an issue for you as well to register a the dll or to use a wrapper you can use the approach from this post on the ahk forums by basi.

Essentially you only need to place the dll in your project folder, include it into the project and set "Copy to Output Directory" to "Copy if newer" in the properties.

Then import the dll functions like this:

        [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 allows to run a script from a file, ahktextdll allows to directly insert a script as a string.

I only tested this with the v1 dll from HotKeyIt (i used the one in the win32w folder).

noel
  • 531
  • 2
  • 7
1

Sleep is easy:

   System.Threading.Thread.Sleep(25);

Getting the Key and MouseDown events (ins:: and Lbutton::) when your application is not the active one is a lot more complicated. It can be achieved by using global hooks. Have a look at this CodeProject article A Simple C# Global Low Level Keyboard Hook

Ultimately it depends on why you want to use C# when AHK offers you a much simpler environment to achieve similar things.

I cant think of any simple example which does the job.

0

Your going to want to use send keys

this video is a great example!

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

Micah Armantrout
  • 6,781
  • 4
  • 40
  • 66